首先我们在宿主主机中,先格式化一块硬盘,两个主分区
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000984dd
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 287 2097152 83 Linux
Partition 2 does not end on cylinder boundary.
/dev/sda3 287 389 819200 82 Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/sda4 389 2611 17849344 5 Extended
/dev/sda5 389 2611 17848320 83 Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x504ad509.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
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, +cylinders or +size{K,M,G} (1-2610, default 2610): +20M
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (5-2610, default 5):
Using default value 5
Last cylinder, +cylinders or +size{K,M,G} (5-2610, default 2610): +512M
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#
把修改的传递给内核
[root@localhost ~]# partprobe /dev/sdb
格式化硬盘
[root@localhost ~]# mke2fs -j /dev/hdb1
mke2fs 1.41.12 (17-May-2010)
Could not stat /dev/hdb1 --- No such file or directory
The device apparently does not exist; did you specify it correctly?
[root@localhost ~]# mke2fs -j /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
8032 inodes, 32096 blocks
1604 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33030144
4 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
8193, 24577
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -j /dev/sdb2
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
33200 inodes, 132536 blocks
6626 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=138412032
5 block groups
32768 blocks per group, 32768 fragments per group
6640 inodes per group
Superblock backups stored on blocks:
32768, 98304
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
创建文件,并挂载这两块硬盘
[root@localhost ~]# mkdir /mnt/{boot,sysroot}
[root@localhost ~]# mount /dev/sdb1 /mnt/boot/
[root@localhost ~]# mount /dev/sdb2 /mnt/sysroot/
[root@localhost ~]# mount
/dev/sda5 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda2 on /home type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/sdb1 on /mnt/boot type ext3 (rw)
/dev/sdb2 on /mnt/sysroot type ext3 (rw)
[root@localhost ~]#
定义grub,
GNU GRUB(GRand Unified Bootloader)是一个来自GNU项目的多操作系统启动程序。GRUB是多启动规范的实现,它允许用户可以在计算机内同时拥有多个操作系统,并在计算机启动时选择希望运行的操作系统。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
[root@localhost ~]#
查看已经有grub了
[root@localhost ~]# cp /boot/vmlinuz-2.6.32-642.6.1.el6.x86_64 /mnt/boot/vmlinux
[root@localhost ~]# ls /mnt/boot/
grub lost+found vmlinux
[root@localhost ~]#
这里,我的initrd是initramfs,所以采用直接复制的方法
那么如果文件为initramfs,则直接把内核和文件复制过来
cp /boot/initramfs-2.6.32-642.6.1.el6.x86_64.img /mnt/boot/
cp /boot/vmlinux-2.6.32-642.6.1.el6.x86_64.img /mnt/boot/
在当前目录中创建如下目录
[root@localhost sysroot]# tree
.
├── bin
├── boot
├── dev
├── etc
│ └── rc.d
├── home
├── lib
├── lost+found
├── proc
├── root
├── sbin
├── sys
├── tmp
├── usr
│ ├── bin
│ └── sbin
└── var
└── log
18 directories, 0 files
[root@localhost sysroot]# cp /sbin/in
init initctl insmod insmod.static install-info installkernel
[root@localhost sysroot]# cp /sbin/init /mnt/sysroot/sbin/
[root@localhost sysroot]# cp /bin/bas
basename bash
[root@localhost sysroot]# cp /bin/bash /mnt/sysroot/bin/
[root@localhost sysroot]#
编辑并在,mnt/boot/grub/grub.conf中增加如下
[root@localhost boot]# vim grub/grub.conf
default=0
timeout=5
title test Linux(Magedu Team)
root (hd0,0)
kernel /vmlinux ro selinux=0 root=/dev/sda2 init=/bin/bash
initrd /initramfs-2.6.32-642.6.1.el6.x86_64.img
"grub/grub.conf" 6L, 130C written
编辑并增加下面这两个文件,一个是启动级别
[root@localhost ~]# vim /mnt/sysroot/etc/inittab
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
vim /mnt/sysroot/etc/rc.d/rc.sysinit
[root@localhost ~]# vim /mnt/sysroot/etc/rc.d/rc.sysinit
#!/bin/bash
#
echo -e "\tWelcome to \033[31mMageEdu Team\033[0m Linux."
/bin/bash
检查依赖的包,并且把依赖的包复制过来
[root@localhost sysroot]# ldd /sbin/in
init initctl insmod insmod.static install-info installkernel
[root@localhost sysroot]# ldd /sbin/init
linux-vdso.so.1 => (0x00007ffffa1a5000)
libnih.so.1 => /lib64/libnih.so.1 (0x00007f0cc62d7000)
libnih-dbus.so.1 => /lib64/libnih-dbus.so.1 (0x00007f0cc60cd000)
libdbus-1.so.3 => /lib64/libdbus-1.so.3 (0x00007f0cc5e8b000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f0cc5c6e000)
librt.so.1 => /lib64/librt.so.1 (0x00007f0cc5a66000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f0cc584f000)
libc.so.6 => /lib64/libc.so.6 (0x00007f0cc54bb000)
/lib64/ld-linux-x86-64.so.2 (0x00000035b3c00000)
[root@localhost sysroot]# cp /lib64/libnih.so.1 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/libnih-dbus.so.1 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/libdbus-1.so.3 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/libpthread.so.0 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/librt.so.1 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/libgcc_s.so.1 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/libc.so.6 /mnt/sysroot/lib/
[root@localhost sysroot]# cp /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib/
[root@localhost sysroot]#
最后用sync同步一下。
#!/bin/bash
#
DIZHI=/mnt/sysroot
cpbin(){
CPPATH=${1%/*}
[ ! -d $DIZHI$CPPATH ] && mkdir -p $DIZHI$CPPATH
[ ! -e $DIZHI$1 ] && cp $1 $DIZHI$CPPATH && echo "copy $DIZHI$1 success."
}
zhenli(){
MINLIN=${1%/*}
[ ! -d $DIZHI$MINLIN ] && mkdir -p $DIZHI$MINLIN
[ ! -e $DIZHI$1 ] && cp $1 $DIZHI$MINLIN
for i in `ldd $1|grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do
cpbin $i
done
}
read -p "input value:" value
until [ $value == 'q' ]; do
which $value &> /dev/null
[ $? -ne 0 ] && echo "Wrong coomand" && continue
BINVALUE=`which $value |grep -o "/[^[:space:]]\{1,\}"` &> /dev/null
zhenli $BINVALUE
echo "copy $BINVALUE success."
read -p "Continue:" value
done
上面的步骤做完之后,
我的是虚拟主机,那么新建主机的时候,新建的主机需要替换为这个块硬盘
就可以达到如下效果