1. 程式人生 > >制作一個只運行bash的系統及破壞mbr表並修復

制作一個只運行bash的系統及破壞mbr表並修復

initramfs ldd tro 新的 else vim 剛才 cee naconda

制作一個只運行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目錄

[root@203 ~]# mount /dev/sdb1 /mnt/boot

[root@203 ~]# ls /mnt/boot

lost+found

[root@203 ~]# 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

[root@203 ~]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /mnt/boot/vmlinuz

[root@203 ~]# cp /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/initramfs.img

# 復制vmlinuz文件和intramfs.img文件至新硬盤boot目錄

[root@203 ~]# ls /mnt/boot

grub initramfs.img lost+found vmlinuz

[root@203 ~]# vim /mnt/boot/grub/grub.conf

[root@203 ~]# 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

[root@203 ~]# mount /dev/sdb3 /mnt/sysroot

[root@203 ~]# ls /mnt/sysroot

lost+found

[root@203 ~]# cd /mnt/sysroot/

[root@203 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"

[root@203 sysroot]# ls

bin dev etc home lib lib64 lost+found media mnt proc root sbin sys tmp usr

[root@203 sysroot]# cp /bin/bash ./bin/ #復制bash程序至新硬盤,目錄要與原bash路徑一致

[root@203 sysroot]# ls ./bin/

bash

[root@203 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)

[root@203 sysroot]# cd /lib64

[root@203 lib64]# cp libtinfo.so.5 libdl.so.2 libc.so.6 ld-linux-x86-64.so.2 /mnt/sysroot/lib64

[root@203 lib64]# ls /mnt/sysroot/lib64

ld-linux-x86-64.so.2 libc.so.6 libdl.so.2 libtinfo.so.5

[root@203 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

[root@203 lib64]# init 0


找到剛剛添加的硬盤的路徑,新創建一個虛擬機使用剛才添加的硬盤。

添加完成後即可啟動系統進入bash



破壞mbr表並修復:


方法一:磁盤中可以沒有boot目錄

[root@localhost ~]# 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

[root@localhost ~]# 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

[root@localhost ~]# sync #把剛才的操作同步至磁盤

[root@localhost ~]# 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文件

[root@localhost ~]# 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

[root@localhost ~]# sync

[root@localhost ~]# 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

[root@localhost ~]# sync

方法三:光盤引修復

bash-4.1# chroot /mnt/sysimage/

sh-4.1# ls #查看系統中的文件

sh-4.1# grub-install --root-directory=/ /dev/sda #安裝mbr


制作一個只運行bash的系統及破壞mbr表並修復