以当前虚拟机里的系统为宿主机,为其增加一块新的虚拟硬盘(后缀为vmdk的)创建为单个文件
用fdisk命令查看新的硬盘信息
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 25 200781 83 Linux /dev/sda2 26 89 514080 82 Linux swap / Solaris /dev/sda3 90 2610 20249932+ 83 Linux Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System
3.为新硬盘进行分区,格式化
[root@RedHat mnt]# fdisk /dev/sdb Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-130, default 1): Using default value 1 Last cylinder or +size or +sizeM or +sizeK (1-130, default 130): +200M Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (26-130, default 26): Using default value 26 Last cylinder or +size or +sizeM or +sizeK (26-130, default 130): +700M Command (m for help): p Disk /dev/sdb: 1073 MB, 1073741824 bytes 255 heads, 63 sectors/track, 130 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System /dev/sdb1 1 25 200781 83 Linux /dev/sdb2 26 111 690795 83 Linux Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@RedHat mnt]# partprobe /dev/sdb [root@RedHat mnt]# sync [root@RedHat mnt]# sync [root@RedHat mnt]# sync
[root@RedHat mnt]# mke2fs -j /dev/sdb1 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 50200 inodes, 200780 blocks 10039 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 25 block groups 8192 blocks per group, 8192 fragments per group 2008 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 35 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@RedHat mnt]# mke2fs -j /dev/sdb2 mke2fs 1.39 (29-May-2006) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 86400 inodes, 172698 blocks 8634 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=180355072 6 block groups 32768 blocks per group, 32768 fragments per group 14400 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840 Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 30 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
4.在/mnt目录下新建挂载目录
[root@RedHat /]# mkdir /mnt/{boot,sysroot} -pv mkdir: created directory `/mnt/boot‘ mkdir: created directory `/mnt/sysroot‘ [root@RedHat /]# cd /mnt/ [root@RedHat mnt]# ls -a . .. boot sysroot [root@RedHat mnt]#
5.挂载
[root@RedHat mnt]# mount /dev/sdb1 /mnt/boot/ [root@RedHat mnt]# mount /dev/sdb2 /mnt/sysroot/
/dev/sdb1 on /mnt/boot type ext3 (rw) /dev/sdb2 on /mnt/sysroot type ext3 (rw)
6.手动安装grub
[root@RedHat mnt]# 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
7.复制宿主下的内核到/mnt/boot/下
[root@RedHat boot]# cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz [root@RedHat boot]# ls -a . .. grub lost+found vmlinuz
8.制作initrd.gz文件
第一方法 在/mnt/boot/下新建临时目录tmp
[root@RedHat boot]# cd tmp [root@RedHat tmp]# ls -a . .. [root@RedHat tmp]# zcat /boot/initrd-2.6.18-308.el5.img | cpio -id 11812 blocks [root@RedHat tmp]# ls -a . .. bin dev etc init lib proc sbin sys sysroot [root@RedHat tmp]#
9.编辑init文件
修改第一行和第三行
#resume LABEL=SWAP-sda2 echo Creating root device. mkrootdev -t ext3 -o defaults,ro /dev/sdb2
10.重新打包制作成initrd.gz文件
一定要在当前展开的目录下 /mnt/boot/tmp/这里
[root@RedHat tmp]# find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz [root@RedHat tmp]# cd /mnt/boot/ [root@RedHat boot]# ls -a . .. grub initrd.gz lost+found tmp vmlinuz [root@RedHat boot]#
11.手动提供grub.conf文件
[root@RedHat boot]# vim /mnt/boot/grub/grub.conf
default=0 timeout=5 title This is luochen Linux(Test version) root (hd0,0) kernel /vmlinuz initrd /initrd.gz
12.在根文件系统下创建目录
[root@RedHat sysroot]# mkdir proc sys etc/rc.d dev home root usr/{local,sbin,bin} bin sbin var/log tmp lib boot -pv mkdir: created directory `proc‘ mkdir: created directory `sys‘ mkdir: created directory `etc‘ mkdir: created directory `etc/rc.d‘ mkdir: created directory `dev‘ mkdir: created directory `home‘ mkdir: created directory `root‘ mkdir: created directory `usr‘ mkdir: created directory `usr/local‘ mkdir: created directory `usr/sbin‘ mkdir: created directory `usr/bin‘ mkdir: created directory `bin‘ mkdir: created directory `sbin‘ mkdir: created directory `var‘ mkdir: created directory `var/log‘ mkdir: created directory `tmp‘ mkdir: created directory `lib‘ mkdir: created directory `boot‘ [root@RedHat sysroot]# ls -a . bin dev home lost+found root sys usr .. boot etc lib proc sbin tmp var
13.复制init和bash文件
[root@RedHat sysroot]# cp /bin/bash /mnt/sysroot/bin/ [root@RedHat sysroot]# cp /sbin/init /mnt/sysroot/sbin/
复制其依赖的库文件
用ldd /sbin/init 和 ldd /bin/bash 可以查看依赖那些库文件
[root@RedHat sysroot]# ldd /sbin/init linux-gate.so.1 => (0x0026e000) libsepol.so.1 => /lib/libsepol.so.1 (0x0042d000) libselinux.so.1 => /lib/libselinux.so.1 (0x00475000) libc.so.6 => /lib/libc.so.6 (0x0026f000) libdl.so.2 => /lib/libdl.so.2 (0x003ea000) /lib/ld-linux.so.2 (0x00244000) [root@RedHat sysroot]# ldd /bin/bash linux-gate.so.1 => (0x00e6b000) libtermcap.so.2 => /lib/libtermcap.so.2 (0x003f1000) libdl.so.2 => /lib/libdl.so.2 (0x003ea000) libc.so.6 => /lib/libc.so.6 (0x00263000) /lib/ld-linux.so.2 (0x00244000)
复制相应的文件到相应的目录下即可 宿主机的根对应/mnt/sysroot/目录
这里写了脚本用来完成库文件的复制
[root@RedHat sysroot]# ./libcopy.sh Your command: bash copy lib /lib/libtermcap.so.2 finished. copy lib /lib/libdl.so.2 finished. copy lib /lib/libc.so.6 finished. copy lib /lib/ld-linux.so.2 finished. copy /bin/bash finished. Continue: init copy lib /lib/libsepol.so.1 finished. copy lib /lib/libselinux.so.1 finished. copy /sbin/init finished.
复制库文件脚本内容
[root@RedHat sysroot]# cat libcopy.sh #!/bin/bash # DEST=/mnt/sysroot libcp() { LIBPATH=${1%/*} [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished." } bincp() { CMDPATH=${1%/*} [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH for LIB in `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do libcp $LIB done } read -p "Your command: " CMD until [ $CMD == ‘q‘ ]; do ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again:" CMD && continue COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"` bincp $COMMAND echo "copy $COMMAND finished." read -p "Continue: " CMD done
14.用chroot命令切换根检测
[root@RedHat sysroot]# chroot /mnt/sysroot/ bash-3.2#
用exit退出 回到宿主机系统
15.手动提供inittab文件
[root@RedHat etc]# vim ./inittab [root@RedHat etc]# cat ./inittab id:3:initdefault: si::sysinit:/etc/rc.d/rc.sysinit [root@RedHat etc]#
16.手动提供/mnt/sysroot/etc/rc.d/sysinit文件
[root@RedHat rc.d]# vim rc.sysinit [root@RedHat rc.d]# cat rc.sysinit #!/bin/bash # # echo -e "\t Welcome to \033[32m珞辰\033[0m Linux " /bin/bash [root@RedHat rc.d]#
[root@RedHat rc.d]# chmod +x rc.sysinit [root@RedHat rc.d]# sync [root@RedHat rc.d]# sync [root@RedHat rc.d]# sync
17.创建内核模块存放目录
[root@RedHat sysroot]# mkdir /mnt/sysroot/lib/modules/
查看两个模块信息
[root@RedHat lib]# modinfo mii filename: /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko license: GPL description: MII hardware support library author: Jeff Garzik <jgarzik@pobox.com> srcversion: 16DCEDEE4B5629C222C352D depends: vermagic: 2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1 module_sig: 883f3504f23273dc995cc0b59af12111211c809f6f79961e4a84bc133b97d3d32bb217be64af3d1c09d11dbf04e53225fd698ce3def9448666b2a5a31 [root@RedHat lib]# modinfo pcnet32 filename: /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko license: GPL description: Driver for PCnet32 and PCnetPCI based ethercards author: Thomas Bogendoerfer srcversion: F81443556AAE169CBF80F55 alias: pci:v00001023d00002000sv*sd*bc02sc00i* alias: pci:v00001022d00002000sv*sd*bc*sc*i* alias: pci:v00001022d00002001sv*sd*bc*sc*i* depends: mii vermagic: 2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1 parm: debug:pcnet32 debug level (int) parm: max_interrupt_work:pcnet32 maximum events handled per interrupt (int) parm: rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int) parm: tx_start_pt:pcnet32 transmit start point (0-3) (int) parm: pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int) parm: options:pcnet32 initial option setting(s) (0-15) (array of int) parm: full_duplex:pcnet32 full duplex setting(s) (1) (array of int) parm: homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int) module_sig: 883f3504f2326fedc995cc0b59af121112c9240a0888050bfd72bf9d5ba34fa2b2d35ad9c599dc2b509f6fc0ce174c811a747cc9292d8a4d837983e5b14
复制内核模块文件到/mnt/sysroot/lib/modules/目录下
[root@RedHat lib]# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules/ [root@RedHat lib]# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules/
18.在编辑/mnt/sysroot/etc/rc.d/rc.sysinit文件 在系统初始化时自动装载网卡
[root@RedHat sysroot]# cat /mnt/sysroot/etc/rc.d/rc.sysinit #!/bin/bash # # echo -e "\t Welcome to \033[32m珞辰\033[0m Linux " insmod /lib/modules/mii.ko insmod /lib/modules/pcnet32.ko ifconfig eth0 192.168.3.45/24 ifconfig lo 127.0.0.1/8 /bin/bash
大功告成 贴图
本文出自 “珞辰的博客” 博客,请务必保留此出处http://luochen2015.blog.51cto.com/9772274/1652613
原文地址:http://luochen2015.blog.51cto.com/9772274/1652613