制作一个只运行bash的系统
1、新增一块硬盘,分三个区。
分区1:boot目录100M
分区2:2G,swap分区
分区3:5G,根目录。
其中分区1、3格式化为ext4文件系统,分区2格式化为swap。
2、/mnt下新建两个目录boot和sysroot。
分区1挂载至boot目录,分区3挂载至sysroot目录
grub安装至分区1,复制内核和initramfs至boot目录
[[email protected] ~]# mount /dev/sdb1 /mnt/boot
[[email protected] ~]# ls /mnt/boot
lost+found
[[email protected] ~]# grub-install --root-directory=/mnt /dev/sdb
#安装grub至新增的sdb硬盘,指明目录--root-directory为/mnt目录,grub会自动寻找boot目录
#指明安装的硬盘/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
[[email protected] ~]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /mnt/boot/vmlinuz
[[email protected] ~]# cp /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/initramfs.img
# 复制vmlinuz文件和intramfs.img文件至新硬盘boot目录
[[email protected] ~]# ls /mnt/boot
grub initramfs.img lost+found vmlinuz
[[email protected] ~]# vim /mnt/boot/grub/grub.conf
[[email protected] ~]# cat /mnt/boot/grub/grub.conf #在grub目录下创建grub.conf文件,增加内容
default=0
timeout=5
title Centos64 (mylinux)
root (hd0,0)
kernel /vmlinuz selinux=0 ro root=/dev/sda3 init=/bin/bash #kernel的路径和根目录的路径
initrd /initramfs.img
[[email protected] ~]# mount /dev/sdb3 /mnt/sysroot
[[email protected] ~]# ls /mnt/sysroot
lost+found
[[email protected] ~]# cd /mnt/sysroot/
[[email protected] sysroot]# mkdir -pv etc bin sbin lib lib64 dev proc sys tmp usr home root mnt media
# 创建根文件系统
mkdir: 已创建目录 "etc"
mkdir: 已创建目录 "bin"
mkdir: 已创建目录 "sbin"
mkdir: 已创建目录 "lib"
mkdir: 已创建目录 "lib64"
mkdir: 已创建目录 "dev"
mkdir: 已创建目录 "proc"
mkdir: 已创建目录 "sys"
mkdir: 已创建目录 "tmp"
mkdir: 已创建目录 "usr"
mkdir: 已创建目录 "home"
mkdir: 已创建目录 "root"
mkdir: 已创建目录 "mnt"
mkdir: 已创建目录 "media"
[[email protected] sysroot]# ls
bin dev etc home lib lib64 lost+found media mnt proc root sbin sys tmp usr
[[email protected] sysroot]# cp /bin/bash ./bin/ #复制bash程序至新硬盘,目录要与原bash路径一致
[[email protected] sysroot]# ls ./bin/
bash
[[email protected] sysroot]# ldd /bin/bash #查看bash所依赖的库,并复制到新硬盘对应目录下
linux-vdso.so.1 => (0x00007fff2b3f4000)
libtinfo.so.5 => /lib64/libtinfo.so.5 (0x0000003532a00000)
libdl.so.2 => /lib64/libdl.so.2 (0x0000003530a00000)
libc.so.6 => /lib64/libc.so.6 (0x0000003530600000)
/lib64/ld-linux-x86-64.so.2 (0x0000003530200000)
[[email protected] sysroot]# cd /lib64
[[email protected] lib64]# cp libtinfo.so.5 libdl.so.2 libc.so.6 ld-linux-x86-64.so.2 /mnt/sysroot/lib64
[[email protected] lib64]# ls /mnt/sysroot/lib64
ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libtinfo.so.5
[[email protected] lib64]# chroot /mnt/sysroot/ #测试bash是否正常
bash-4.1# type type #bash内置命令可用,外置命令不可用。
type is a shell builtin
bash-4.1# type cat
bash: type: cat: not found
bash-4.1# exit
exit
[[email protected] lib64]# init 0
找到刚刚添加的硬盘的路径,新创建一个虚拟机使用刚才添加的硬盘。
添加完成后即可启动系统进入bash
破坏mbr表并修复:
方法一:磁盘中可以没有boot目录
[[email protected] ~]# dd if=/dev/sda of=/root/mbr.bak count=1 bs=512 #备份mbr表
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000419863 s, 1.2 MB/s
[[email protected] ~]# dd if=/dev/zero of=/dev/sda count=1 bs=200 #破坏mbr表前200字节
1+0 records in
1+0 records out
200 bytes (200 B) copied, 0.000233899 s, 855 kB/s
[[email protected] ~]# sync #把刚才的操作同步至磁盘
[[email protected] ~]# grub-install --root-directory=/ /dev/sda #安装新的
Installation finished. No error reported.
This is the contents of the device map //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'.
# this device map was generated by anaconda
(hd0) /dev/sda
方法二:磁盘中要有boot目录,并且包含stage1,stage1_5,stage2文件
[[email protected] ~]# dd if=/dev/zero of=/dev/sda count=1 bs=200
1+0 records in
1+0 records out
200 bytes (200 B) copied, 0.000163216 s, 1.2 MB/s
[[email protected] ~]# sync
[[email protected] ~]# grub
Probing devices to guess BIOS drives. This may take a long time.
GNU GRUB version 0.97 (640K lower / 3072K upper memory)
[ Minimal BASH-like line editing is supported. For the first word, TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename.]
grub> root (hd0,0) #指明根所在的分区
root (hd0,0)
Filesystem type is ext2fs, partition type 0x83
grub> setup (hd0) #指明安装的磁盘
setup (hd0)
Checking if "/boot/grub/stage1" exists... no
Checking if "/grub/stage1" exists... yes
Checking if "/grub/stage2" exists... yes
Checking if "/grub/e2fs_stage1_5" exists... yes
Running "embed /grub/e2fs_stage1_5 (hd0)"... 27 sectors are embedded.
succeeded
Running "install /grub/stage1 (hd0) (hd0)1+27 p (hd0,0)/grub/stage2 /grub/grub.conf"... succeeded
Done.
grub> quit
quit
[[email protected] ~]# sync
方法三:光盘引修复
bash-4.1# chroot /mnt/sysimage/
sh-4.1# ls #查看系统中的文件
sh-4.1# grub-install --root-directory=/ /dev/sda #安装mbr
原文地址:http://blog.51cto.com/11975865/2063367