Linux系统裁剪与定制

本文以虚机中新加入一块硬盘,安装grub以及/bin/bash程序模拟裁剪系统

新加一块硬盘,提供单独运行的bash系统

新增一块20G硬盘

在这里插入图片描述

创建分区以及文件系统

fdisk /dev/sdb  后进入创建分区并写入

mkfs.ext4 /dev/sdb1  # /boot路径
mkfs.ext4 /dev/sdb2  

在这里插入图片描述

挂载文件系统并制作grub

[root@host70 ~]# mount /dev/sdb1 /mnt/boot
[root@host70 ~]# cd /mnt/boot/
[root@host70 boot]# ls
lost+found
[root@host70 boot]# grub-install -h
Usage: grub-install [OPTION] install_device
Install GRUB on your drive.

  -h, --help              print this message and exit
  -v, --version           print the version information and exit
  --root-directory=DIR    install GRUB images under the directory DIR
                          instead of the root directory
  --grub-shell=FILE       use FILE as the grub shell
  --no-floppy             do not probe any floppy drive
  --force-lba             force GRUB to use LBA mode even for a buggy
                          BIOS
  --recheck               probe a device map even if it already exists
                          This flag is unreliable and its use is
                          strongly discouraged.

INSTALL_DEVICE can be a GRUB device name or a system device filename.

grub-install copies GRUB images into the DIR/boot directory specfied by
--root-directory, and uses the grub shell to install grub into the boot
sector.

Report bugs to <bug-grub@gnu.org>.
[root@host70 boot]# 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

# 上传 内核文件和init至/mnt/boot下
# 上传 grub.conf文件t至/mnt/boot/grub下,并修改grub参数
#boot=/dev/sda
default=0
timeout=5
title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64)
        root (hd1,0)
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sdb2 selinux=0  init=/bin/bash
        initrd /initramfs-2.6.32-431.el6.x86_64.img

制作/bin/bash

[root@host70 boot]# mount /dev/sdb2 /mnt/sysroot/
[root@host70 boot]# ls
grub  initramfs-2.6.32-431.el6.x86_64.img  lost+found  vmlinuz-2.6.32-431.el6.x86_64
[root@host70 boot]# cd /mnt/sysroot/

[root@host70 sysroot]# mkdir bin boot dev etc home lib lib64  media mnt opt proc root sbin selinux srv sys tmp usr var
[root@host70 sysroot]# which /bin/bash
/bin/bash
[root@host70 sysroot]# cp /bin/bash /mnt/sysroot/bin/
# 将/bin/bash 所依赖的库文件拷贝
[root@host70 sysroot]# ldd /bin/bash
        linux-vdso.so.1 =>  (0x00007fffc2ce0000)
        libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f9eba33e000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f9eba13a000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f9eb9da5000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9eba567000)
[root@host70 sysroot]# cp /lib64/libtinfo.so.5 /lib64/libdl.so.2 /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2 /mnt/sysroot/lib64
[root@host70 sysroot]# sync
[root@host70 bin]# chroot /mnt/sysroot/
bash-4.1#
# 正常进入Bash

在现有VM中以第二块硬盘加载/bin/bash系统

# grub.conf 中的参考 
#
default=0
timeout=5
title Red Hat Enterprise Linux (2.6.32-431.el6.x86_64)
        root (hd1,0) #表示第二块硬盘的第一个分区
        kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/sdb2 selinux=0  init=/bin/bash  # 第一个分区sdb1为boot 第二个分区sdb2为根目录
        initrd /initramfs-2.6.32-431.el6.x86_64.img

正常进入

在这里插入图片描述

模拟硬盘拆下来后插到一个新的机器上

1)还原至正常系统,进入grub命令行界面,输入如下命令

root (hd0,0)
kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=/dev/mapper/vg_host70-lv_root  selinx=0
initrd /initramfs-2.6.32-431.el6.x86_64.img
boot

2) 修改grub.conf 文件

在这里插入图片描述

3)新建一个VM,硬盘为上图中的disk 载入系统即可

在这里插入图片描述

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yolo2016

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值