设为首页 收藏本站
查看: 806|回复: 0

[经验分享] 制作一个只运行bash的系统及破坏mbr表并修复

[复制链接]

尚未签到

发表于 2018-1-22 11:32:15 | 显示全部楼层 |阅读模式
                                                制作一个只运行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目录
[iyunv@203 ~]# mount /dev/sdb1 /mnt/boot
[iyunv@203 ~]# ls /mnt/boot
lost+found
[iyunv@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
[iyunv@203 ~]# cp /boot/vmlinuz-2.6.32-696.el6.x86_64 /mnt/boot/vmlinuz
[iyunv@203 ~]# cp /boot/initramfs-2.6.32-696.el6.x86_64.img /mnt/boot/initramfs.img
# 复制vmlinuz文件和intramfs.img文件至新硬盘boot目录
[iyunv@203 ~]# ls /mnt/boot
grub  initramfs.img  lost+found  vmlinuz
[iyunv@203 ~]# vim /mnt/boot/grub/grub.conf
[iyunv@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
[iyunv@203 ~]# mount /dev/sdb3 /mnt/sysroot
[iyunv@203 ~]# ls /mnt/sysroot
lost+found
[iyunv@203 ~]# cd /mnt/sysroot/
[iyunv@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"
[iyunv@203 sysroot]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr
[iyunv@203 sysroot]# cp /bin/bash ./bin/                #复制bash程序至新硬盘,目录要与原bash路径一致
[iyunv@203 sysroot]# ls ./bin/
bash
[iyunv@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)
[iyunv@203 sysroot]# cd /lib64
[iyunv@203 lib64]# cp libtinfo.so.5 libdl.so.2 libc.so.6 ld-linux-x86-64.so.2 /mnt/sysroot/lib64
[iyunv@203 lib64]# ls /mnt/sysroot/lib64
ld-linux-x86-64.so.2  libc.so.6  libdl.so.2  libtinfo.so.5
[iyunv@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
[iyunv@203 lib64]# init 0

找到刚刚添加的硬盘的路径,新创建一个虚拟机使用刚才添加的硬盘。
添加完成后即可启动系统进入bash


破坏mbr表并修复:

方法一:磁盘中可以没有boot目录
[iyunv@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
[iyunv@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
[iyunv@localhost ~]# sync                                                                                        #把刚才的操作同步至磁盘
[iyunv@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文件
[iyunv@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
[iyunv@localhost ~]# sync
[iyunv@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
[iyunv@localhost ~]# sync
方法三:光盘引修复

bash-4.1# chroot /mnt/sysimage/
sh-4.1# ls                                                                        #查看系统中的文件
sh-4.1# grub-install --root-directory=/ /dev/sda        #安装mbr


                                       


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-435994-1-1.html 上篇帖子: linux中的buffer和cache的区别 下篇帖子: btrfs文件系统的简单使用方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表