我们自行使用创建一个可以正常开机的linux系统,但是功能不健全,仅为其能正常引导开机:步骤如下:
第一步:在虚拟机添加一块新硬盘
第二步:在新硬盘上创建分区,1个/boot ,1个swap分区,一个根分区:
[root@www ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280bytes Sector size (logical/physical): 512 bytes /512 bytes I/O size (minimum/optimal): 512 bytes / 512bytes Disk identifier: 0x950bee79 Device Boot Start End Blocks Id System /dev/sdb1 1 26 208813+ 83 Linux /dev/sdb2 27 158 1060290 82 Linux swap / Solaris ##swap分区类型需要调整为82 Linux swap / Solaris /dev/sdb3 159 812 5253255 83 Linux [root@www ~]# partx -a /dev/sdb ##通知内核重读新分区 BLKPG: Device or resource busy error adding partition 1 BLKPG: Device or resource busy error adding partition 2 BLKPG: Device or resource busy error adding partition 3
第三步:为新分区创建文件系统
[root@www ~]# mkfs.ext3 /dev/sdb1 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 52208 inodes, 208812 blocks 10440 blocks (5.00%) reserved for the superuser First data block=1 Maximum filesystem blocks=67371008 26 block groups 8192 blocks per group, 8192 fragments pergroup 2008 inodes per group Superblock backups stored on blocks: 8193,24577, 40961, 57345, 73729, 204801 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystemaccounting information: done This filesystem will be automaticallychecked every 37 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@www ~]# mkfs.ext3 /dev/sdb3 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 328656 inodes, 1313313 blocks 65665 blocks (5.00%) reserved for the superuser First data block=0 Maximum filesystem blocks=1346371584 41 block groups 32768 blocks per group, 32768 fragments pergroup 8016 inodes per group Superblock backups stored on blocks: 32768,98304, 163840, 229376, 294912, 819200, 884736 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystemaccounting information: done This filesystem will be automaticallychecked every 27 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@www ~]# mkswap /dev/sdb2 ##创建交换分区 Setting up swapspace version 1, size =1060284 KiB no label,UUID=9e278322-0033-4bdc-8739-a621149f0179 [root@www ~]# blkid /dev/sdb1 ##显示分区属性,boot分区格式化成功 /dev/sdb1: LABEL="Boot Part"UUID="ce836e56-3bbf-47b0-a646-846e1b5d23d6" TYPE="ext3"SEC_TYPE="ext2" [root@www ~]# blkid /dev/sdb2 ##显示分区属性,swap分区格式化成功 /dev/sdb2:UUID="9e278322-0033-4bdc-8739-a621149f0179" TYPE="swap" [root@www ~]# blkid /dev/sdb3 ##显示分区属性,根分区格式化成功 /dev/sdb3: LABEL="root"UUID="02ec22df-7f8c-4cc4-82ec-79bb7f6806a2" SEC_TYPE="ext2"TYPE="ext3"
第四步:在/mnt目录下,创建boot 和rootfs两个分区,并将sdb1挂载至/mnt/boot,将sdb3挂载至/mnt/rootfs
[root@www ~]# mount /dev/sdb1 /mnt/boot/ [root@www ~]# mount /dev/sdb3 /mnt/rootfs/
第五步:在rootfs目录下创建mnt etc dev lib lib64 bin sbin proc sys root home目录,并使用grub-install命令安装grub到该硬盘/dev/sdb:
[root@www ~]# cd /mnt/rootfs/ [root@www rootfs]# mkdir -pv mnt etc dev lib lib64 bin sbin proc sys roothome var usr mkdir: created directory `mnt' mkdir: created directory `etc' mkdir: created directory `dev' mkdir: created directory `lib' mkdir: created directory `lib64' mkdir: created directory `bin' mkdir: created directory `sbin' mkdir: created directory `proc' mkdir: created directory `sys' mkdir: created directory `root' mkdir: created directory `home' mkdir: created directory `var' mkdir: created directory `usr' [root@www rootfs]# grub-install--root-directory=/mnt /dev/sdb Probing devices to guess BIOS drives. Thismay take a long time. Installation finished. No error reported. This is the contents of the device map/mnt/boot/grub/device.map. Check if this is correct or not. If any ofthe lines is incorrect, fix it and re-run the script`grub-install'. (fd0) /dev/fd0 (hd0) /dev/sda (hd1) /dev/sdb
第六步:复制内核及initrd文件到/mnt/boot目录,为配置方便,此处将内核及initramfs版本号修改为1.1.1,并编写grub配置文件:
[root@www mnt]# cp/boot/vmlinuz-2.6.32-504.el6.x86_64 boot/vmlinuz-1.1.1 [root@www mnt]# cp/boot/initramfs-2.6.32-504.el6.x86_64.img boot/initramfs-1.1.1.img ##initramfs和vmlinux版本号必须完全一致
创建grub的配置文件
[root@www mnt]# vim boot/grub/grub.conf default=0 timeout=5 title Test-linux root (hd0,0) kernel /vmlinuz-1.1.1 ro selinux=0 root=/dev/sda3 init=/bin/bash ##内核的初始化任务之一是激活selinux,因为我们创建的小系统功能不完善,所以需关闭selinux,否则无法正常引导开机,init将用户空间的第一个程序指向bash initrd /initramfs-1.1.1.img
第七步:拷贝bash程序以及其所依赖的库文件拷贝到新硬盘上:
[root@www ~]# cp -R `which bash`/mnt/rootfs/bin/ [root@www rootfs]# ldd `which bash` linux-vdso.so.1=> (0x00007fffda72a000) libtinfo.so.5=> /lib64/libtinfo.so.5 (0x0000003bc6200000) libdl.so.2=> /lib64/libdl.so.2 (0x0000003bbb600000) libc.so.6=> /lib64/libc.so.6 (0x0000003bbba00000) /lib64/ld-linux-x86-64.so.2(0x0000003bbb200000) [root@www rootfs]# cp /lib64/libtinfo.so.5 /mnt/rootfs/lib64/ [root@www rootfs]# cp /lib64/libdl.so.2/mnt/rootfs/lib64/ [root@www rootfs]# cp /lib64/libc.so.6/mnt/rootfs/lib64/ [root@www rootfs]# cp/lib64/ld-linux-x86-64.so.2 /mnt/rootfs/lib64/ [root@www rootfs]# sync ##将内存中数据同步到磁盘
执行完以上操作,便可将新硬盘插到新设备上启动了:如下:
第一步:
第二步:
第三步:
第四步:
第五步:
第六步:
第七步:
第八步:
第九步:保持默认:
第十步:
第十一部:
创建完成,开机测试,需事先关闭以挂载了新磁盘的那台主机!:
Grub菜单以显示出来了。激动人心的时刻――――――――――――
实验成功