标签:linux
一、基于HOST(宿主机)制作一个简单的可启动的linux
1、给目标磁盘分区
两个:
宿主机上:/dev/sdb1,/dev/sdb2
/dev/sdb1挂载至/mnt/boot,/dev/sdb2挂载至/mnt/sysroot
2、安装grub至目标磁盘
# grub-install --root-directory=/mnt /dev/sdb
3、复制内核和initrd文件
# cp /boot/vmlinz-VERSION /mnt/boot/vmlinuz
# cp /boot/initramfs-VERSION.img /mnt/boot/initramfs.img
注意:复制到目标机的文件为方便在这里才没有写版本,建议写版本也要写上
4、创建目标主机的根文件系统
# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,srv,mnt,media,home,root}
接着移植bash,ls,cat等常用命令至目标主机的根文件系统
5、为grub提供配置文件
# vim /mnt/boot/grub/grub.conf
default=0
timeout=5
title Magedu Little linux
root (hd0,0)
kernel /vmlinuz ro root=/dev/sda2 quiet selinux=0 init=/bin/bash
initrd /initramfs.img
就完成了基于HOST制作一个简单的可启动的linux,将此硬盘拆下安装到别的机器上(因为没有驱动程序,只能在虚拟机上运行),测试启动
[root@Note3 ~]# fdisk -l /dev/sdb 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: 0xf3b882a4 Device Boot Start End Blocks Id System /dev/sdb1 1 500 4016218+ 83 Linux /dev/sdb2 501 632 1060290 83 Linux [root@Note3 ~]# mkdir /mnt/{boot,sysroot} [root@Note3 ~]# mount /dev/sdb1 /mnt/boot [root@Note3 ~]# mount /dev/sdb2 /mnt/sysroot [root@Note3 ~]# df -TH Filesystem Type Size Used Avail Use% Mounted on /dev/sda2 ext4 20G 4.9G 14G 27% / tmpfs tmpfs 981M 0 981M 0% /dev/shm /dev/sda1 ext4 508M 34M 448M 7% /boot /dev/sdc1 ext4 3.0T 211M 2.9T 1% /mnt /dev/sdb1 ext4 4.1G 76M 3.8G 2% /mnt/boot /dev/sdb2 ext4 1.1G 35M 980M 4% /mnt/sysroot [root@Note3 ~]# cp /boot/vmlinuz-2.6.32-431.el6.x86_64 /mnt/boot/vmlinuz [root@Note3 ~]# cp /boot/initramfs-2.6.32-431.el6.x86_64.img /mnt/boot/initramfs [root@Note3 ~]# 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 (hd2) /dev/sdc [root@Note3 ~]# ls /mnt/boot grub initramfs lost+found vmlinuz [root@Note3 ~]# vim /mnt/boot/grub/grub.conf [root@Note3 ~]# cat /mnt/boot/grub/grub.conf #boot分区就完成了 default=0 timeout=10 titile Anyfish Linux root (hd0,0) kernel /vmlinuz ro root=/dev/sda2 quiet selinux=0 init=/bin/bash initrd /initramfs #init=bash指定kernel不再去查询init,直接启动bash,不设置kernel找不到init就会报错 #下面开始准备根文件系统 [root@Note3 ~]# mkdir -pv /mnt/sysroot/{etc/rc.d,usr,var,proc,sys,dev,lib,lib64,bin,sbin,boot,home,mnt} mkdir: 已创建目录 "/mnt/sysroot/etc" mkdir: 已创建目录 "/mnt/sysroot/etc/rc.d" mkdir: 已创建目录 "/mnt/sysroot/usr" mkdir: 已创建目录 "/mnt/sysroot/var" mkdir: 已创建目录 "/mnt/sysroot/proc" mkdir: 已创建目录 "/mnt/sysroot/sys" mkdir: 已创建目录 "/mnt/sysroot/dev" mkdir: 已创建目录 "/mnt/sysroot/lib" mkdir: 已创建目录 "/mnt/sysroot/lib64" mkdir: 已创建目录 "/mnt/sysroot/bin" mkdir: 已创建目录 "/mnt/sysroot/sbin" mkdir: 已创建目录 "/mnt/sysroot/boot" mkdir: 已创建目录 "/mnt/sysroot/home" mkdir: 已创建目录 "/mnt/sysroot/mnt" [root@Note3 ~]# cp /bin/bash /mnt/sysroot/bin [root@Note3 ~]# chroot /mnt/sysroot chroot: failed to run command `/bin/bash‘: No such file or directory [root@Note3 ~]# ls /mnt/sysroot/bin/bash /mnt/sysroot/bin/bash [root@Note3 ~]# ldd /bin/bash linux-vdso.so.1 => (0x00007fff43b8e000) libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f08cb011000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f08cae0d000) libc.so.6 => /lib64/libc.so.6 (0x00007f08caa78000) /lib64/ld-linux-x86-64.so.2 (0x00007f08cb242000) [root@Note3 ~]# ldd /bin/bash|grep -o ‘/[[:alnum:]/.-]\+‘ /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 [root@Note3 ~]# for i in `ldd /bin/bash|grep -o ‘/[[:alnum:]/.-]\+‘|cut -d/ -f3`;do cp /lib64/$i /mnt/sysroot/lib64/;done [root@Note3 ~]# ls /mnt/sysroot/lib64/ ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libtinfo.so.5 [root@Note3 ~]# chroot /mnt/sysroot bash-4.1# ls bash: ls: command not found bash-4.1# bash-4.1# quit bash: quit: command not found bash-4.1# exit exit #这里需要注意:chroot切换根目录的目录下一定要有bin/bash才能切换成功,命令成功运行需要自身依赖的库文件 [root@Note3 ~]# cp /bin/ls /bin/cat /mnt/sysroot/bin [root@Note3 ~]# ls /mnt/sysroot/bin bash cat ls [root@Note3 ~]# ldd /bin/cat #cat依赖的库文件和bash一样,已经有了 linux-vdso.so.1 => (0x00007fff219ff000) libc.so.6 => /lib64/libc.so.6 (0x00007fcfd1c9b000) /lib64/ld-linux-x86-64.so.2 (0x00007fcfd203f000) [root@Note3 ~]# cp /lib64/libc.so.6 ^C [root@Note3 ~]# ldd /bin/ls linux-vdso.so.1 => (0x00007fffa578d000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fbac4e29000) librt.so.1 => /lib64/librt.so.1 (0x00007fbac4c21000) libcap.so.2 => /lib64/libcap.so.2 (0x00007fbac4a1c000) libacl.so.1 => /lib64/libacl.so.1 (0x00007fbac4814000) libc.so.6 => /lib64/libc.so.6 (0x00007fbac4480000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fbac427b000) /lib64/ld-linux-x86-64.so.2 (0x00007fbac5058000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fbac405e000) libattr.so.1 => /lib64/libattr.so.1 (0x00007fbac3e59000) [root@Note3 ~]# for i in `ldd /bin/ls|grep -o ‘/[[:alnum:]/.-]\+‘|cut -d/ -f3`;do cp /lib64/$i /mnt/sysroot/lib64/;done cp:是否覆盖"/mnt/sysroot/lib64/libc.so.6"? y cp:是否覆盖"/mnt/sysroot/lib64/libdl.so.2"? y cp:是否覆盖"/mnt/sysroot/lib64/ld-linux-x86-64.so.2"? y [root@Note3 ~]# ls /mnt/sysroot/lib64 ld-linux-x86-64.so.2 libcap.so.2 libpthread.so.0 libtinfo.so.5 libacl.so.1 libc.so.6 librt.so.1 libattr.so.1 libdl.so.2 libselinux.so.1 [root@Note3 ~]# chroot /mnt/sysroot bash-4.1# ls bin dev home lib64 mnt sbin usr boot etc lib lost+found proc sys var
boot + rootfs都完成了,然后新建虚拟机,使用硬盘当作启动盘启动:
标签:linux
原文地址:http://xiexiaojun.blog.51cto.com/2305291/1884429