前言:

  • linux 版本 CentOS 5.5 64位

  • 虚拟机版本 Vmware Workstation 9.0

1.  在虚拟机上添加一块新硬盘到linux,大小20G (可以自定)

1

2. 打开终端,使用fdisk格式化分区刚才挂载的新硬盘/dev/sdb,划分出一个100M大小的主分区和一个1G大小的主分区

1). 查看新增加硬盘 fdisk –l

fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          33      265041   83  Linux
/dev/sda2              34        1308    10241437+  83  Linux
/dev/sda3            1309        1945     5116702+  83  Linux
/dev/sda4            1946        2610     5341612+   5  Extended
/dev/sda5            1946        2072     1020096   82  Linux swap / Solaris
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table

2). 分区

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-2610, default 2610): +100M
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (14-2610, default 14): 
Using default value 14
Last cylinder or +size or +sizeM or +sizeK (14-2610, default 2610): +1G
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#

3). 重读下分区表

[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]#

4). 格式化为ext3文件系统

[root@localhost ~]# mke2fs -j /dev/sdb1
[root@localhost ~]# mke2fs -j /dev/sdb2

5). 将刚格式化好的分区/dev/sdb1挂载到/mnt/boot  /dev/sdb2挂载到/mnt/sysroot

[root@localhost ~]# mkdir -p /mnt/boot
[root@localhost ~]# mkdir -p /mnt/sysroot
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/

6). 查看挂载分区

[root@localhost ~]# mount
/dev/sda2 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/sda3 on /data type ext3 (rw)
/dev/sda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/sdb1 on /mnt/boot type ext3 (rw)
/dev/sdb2 on /mnt/sysroot type ext3 (rw)
[root@localhost ~]#

3. 安装grub到/dev/sdb,并增加grub.conf配置文件

1). 安装grub

[root@localhost ~]# grub-install --root-directory=/mnt/ /dev/sdb Probing devices to guess BIOS drives. This may 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 of the lines is incorrect,
fix it and re-run the script `grub-install'.
(fd0)   /dev/fd0
(hd0)   /dev/sda
(hd1)   /dev/sdb

2). 修改/mnt/boot/grub/device/map

[root@localhost ~]# vim /mnt/boot/grub/device.map
(fd0)   /dev/fd0           #去掉这一行  fd指的是软盘
(hd0)   /dev/sda
(hd1)   /dev/sdb         #去掉这一行

4. 复制kernel和initrd到小linux中

[root@localhost ~]# cp /boot/vmlinuz-2.6.18-194.el5 /mnt/boot/vmlinuz
[root@localhost ~]# cp /boot/initrd-2.6.18-194.el5.img /mnt/boot/initrd
[root@localhost ~]#

5. 解压缩initrd, 编辑里面的init文件,然后重新压缩封装

1) .解压缩initrd

[root@localhost boot]# cd /mnt/boot/
[root@localhost boot]# ls
grub  initrd  lost+found  vmlinuz
[root@localhost boot]# file initrd 
initrd: gzip compressed data, from Unix, last modified: Wed May 22 21:03:38 2013, max compression #压缩文件
[root@localhost boot]# mv initrd /tmp/initrd.gz
[root@localhost boot]# cd /tmp/
[root@localhost tmp]# ls
initrd.gz
[root@localhost tmp]# gzip -d initrd.gz 
[root@localhost tmp]# file initrd 
initrd: ASCII cpio archive (SVR4 with no CRC) #cpio 打包文件
[root@localhost tmp]# cpio -id < initrd 
13942 blocks
[root@localhost tmp]# ls
bin  dev  etc  init  initrd  lib  proc  sbin  sys  sysroot

2). 修改init文件

[root@localhost tmp]# vim init
 87 #resume LABEL=SWAP-sda5 #将swap注释,我们这里没有swap分区
 88 echo Creating root device.
 89 mkrootdev -t ext3 -o defaults,ro sda2 #修改为mkrootdev -t ext3 -o defaults,ro /dev/sda2
[root@localhost tmp]# find . | cpio -H newc --quiet -o | gzip -9 >/mnt/boot/initrd.gz
[root@localhost boot]# ls
grub  initrd.gz  lost+found  vmlinuz

6. 增加grub.conf文件

[root@localhost grub]# pwd
/mnt/boot/grub
[root@localhost grub]# ls
device.map     iso9660_stage1_5   stage1           xfs_stage1_5
e2fs_stage1_5  jfs_stage1_5       stage2
fat_stage1_5   minix_stage1_5     ufs2_stage1_5
ffs_stage1_5   reiserfs_stage1_5  vstafs_stage1_5
[root@localhost grub]# vim grub.conf
default=0
timeout=3
title freeloda linux
        root (hd0,0)
        kernel /vmlinuz
        initrd /initrd.gz
:wq
[root@localhost grub]#

7. 建立真正的根目录/mnt/sysroot

1). 创建必须的文件目录

[root@localhost var]# mkdir -p sys etc/{rc.d,init.d} root boot bin sbin var/{log,lock,run} dev home tmp lib lib64 usr/{bin,sbin} proc
[root@localhost var]# ls
bin  boot  dev  etc  home  lib  lock  log  root  run  sbin  sys  tmp  usr  var
[root@localhost var]#

2). 配置系统启动文件etc/inittab

[root@localhost sysroot]# vim /mnt/sysroot/etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
:wq

3). 配置增加启动脚本/etc/rc.d/rc.sysinit

[root@localhost rc.d]# vim rc.sysinit
#!/bin/bash
#
echo "Welcome freeloda little linux !"
mount -n -o remount.rw / #以读写方式重新挂载根
mount -n –a #挂载所有设备,并不记录
/bin/bash #启动bash
:wq

4). 对脚本增加执行权限  

[root@localhost rc.d]# chmod +x rc.sysinit
[root@localhost rc.d]# ll
total 4
-rwxr-xr-x 1 root root 63 Jun 12 23:58 rc.sysinit

8. 复制命令到小型linux里去

1). 如何复制?

[root@localhost lib64]# which bash #使用which命令查看命令所在路径
/bin/bash
[root@localhost lib64]#
[root@localhost lib64]# ldd /bin/bash #ldd 加命令路径可以显示出这个命令所依赖的库文件
        libtermcap.so.2 => /lib64/libtermcap.so.2 (0x0000003bac600000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003bac200000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003babe00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003baba00000)
[root@localhost lib64]# cp -p /lib64/libtermcap.so.2 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64/ #将上面的库文件都复制到小linux对应的目录
[root@localhost lib64]# ls
ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libtermcap.so.2

2). 继续复制 init 和bash,mount , ls 这几个个命令去小linux,注意,init和bash必须复制

init 命令
[root@localhost lib64]# which init
/sbin/init
[root@localhost lib64]# ldd /sbin/init
        libsepol.so.1 => /lib64/libsepol.so.1 (0x0000003bace00000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003bad200000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003babe00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003bac200000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003baba00000)
[root@localhost lib64]# cp -p /lib64/libsepol.so.1 /lib64/libselinux.so.1 /lib64/libc.so.6 /lib64/libdl.so.2 /lib64/ld-linux-x86-64.so.2 ./
cp: overwrite `./libc.so.6'? y
cp: overwrite `./libdl.so.2'? y
cp: overwrite `./ld-linux-x86-64.so.2'? y
[root@localhost lib64]# ls
a.out  ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libselinux.so.1  libsepol.so.1  libtermcap.so.2
[root@localhost lib64]#
mount 命令
[root@localhost lib64]# which mount
/bin/mount
[root@localhost lib64]# ldd /bin/mount
        libblkid.so.1 => /lib64/libblkid.so.1 (0x0000003bada00000)
        libuuid.so.1 => /lib64/libuuid.so.1 (0x0000003bad600000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003bad200000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003babe00000)
        libdevmapper.so.1.02 => /lib64/libdevmapper.so.1.02 (0x0000003baca00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003baba00000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003bac200000)
        libsepol.so.1 => /lib64/libsepol.so.1 (0x0000003bace00000)
[root@localhost lib64]# cp -p /lib64/libblkid.so.1 /lib64/libuuid.so.1 /lib64/libselinux.so.1 /lib64/libc.so.6 /lib64/libdevmapper.so.1.02 /lib64/ld-linux-x86-64.so.2 /lib64/libdl.so.2 /lib64/libsepol.so.1 ./
cp: overwrite `./libselinux.so.1'? y
cp: overwrite `./libc.so.6'? y
cp: overwrite `./ld-linux-x86-64.so.2'? y
cp: overwrite `./libdl.so.2'? y
cp: overwrite `./libsepol.so.1'? y
[root@localhost lib64]# ls
a.out                 libblkid.so.1  libdevmapper.so.1.02  libselinux.so.1  libtermcap.so.2
ld-linux-x86-64.so.2  libc.so.6      libdl.so.2            libsepol.so.1    libuuid.so.1
ls 命令
[root@localhost lib64]# which ls
alias ls='ls --color=tty'
        /bin/ls
[root@localhost lib64]# ldd /bin/ls
        librt.so.1 => /lib64/librt.so.1 (0x0000003bada00000)
        libacl.so.1 => /lib64/libacl.so.1 (0x0000003bae600000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003bad200000)
        libc.so.6 => /lib64/libc.so.6 (0x0000003babe00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003bac600000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003baba00000)
        libattr.so.1 => /lib64/libattr.so.1 (0x0000003bad600000)
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003bac200000)
        libsepol.so.1 => /lib64/libsepol.so.1 (0x0000003bace00000)
[root@localhost lib64]# cp -p /lib64/librt.so.1 /lib64/libacl.so.1 /lib64/libselinux.so.1 /lib64/libc.so.6 /lib64/libpthread.so.0 /lib64/ld-linux-x86-64.so.2 /lib64/libattr.so.1 /lib64/libdl.so.2 /lib64/libsepol.so.1 ./
cp: overwrite `./libselinux.so.1'? y
cp: overwrite `./libc.so.6'? y
cp: overwrite `./ld-linux-x86-64.so.2'? y
cp: overwrite `./libdl.so.2'? y
cp: overwrite `./libsepol.so.1'? y
[root@localhost lib64]# ls
a.out                 libacl.so.1   libblkid.so.1  libdevmapper.so.1.02  libpthread.so.0  libselinux.so.1  libtermcap.so.2
ld-linux-x86-64.so.2  libattr.so.1  libc.so.6      libdl.so.2            librt.so.1       libsepol.so.1    libuuid.so.1
[root@localhost lib64]#

3). 复制相应的命令脚本 bash init mount ls

[root@localhost lib64]# which bash
/bin/bash
[root@localhost lib64]# cp -p /bin/bash /mnt/sysroot/bin/
[root@localhost lib64]# which init
/sbin/init
[root@localhost lib64]# cp -p /sbin/init /mnt/sysroot/sbin/
[root@localhost lib64]# which mount
/bin/mount
[root@localhost lib64]# cp -p /bin/mount /mnt/sysroot/bin/
[root@localhost lib64]# which ls
alias ls='ls --color=tty'
        /bin/ls
[root@localhost lib64]# cp -p /bin/ls /mnt//sysroot/bin/
[root@localhost lib64]#

9. 创建 /mnt/sysroot/etc/fstab文件

[root@localhost bin]#vim /mnt/sysroot/etc/fstab
/dev/sda2       /        ext3         defaults      0    0
/dev/sda1       /boot    ext3         defaults      0    0                        
sysfs           /sys     sysfs        defaults      0    0                         
proc            /proc    proc         defaults      0    0

好了,大功告成,新建一个虚拟机,选择CentOS 5 64 ,挂载上这块硬盘看下效果吧!

10. 效果图如下

1). 效果图1

效果图1

2). 效果图2

效果图2

3). 效果图3

效果图3