增加新硬盘,在其上制作能单独运行 kernel 和 bash 的系统

1、CentOS 6

  • 添加一块容量为 20G 的新硬盘;
[root@localhost ~]# lsblk | grep sdb
sdb      8:16   0   20G  0 disk
  • 创建分区( /boot:1G , / :19G )
[root@localhost ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (133-2610, default 133): 
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-2610, default 2610): 
Using default value 2610

Command (m for help): p

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: 0xb88c7dc6

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux
/dev/sdb2             133        2610    19904535   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 格式化分区
[root@localhost ~]# mkfs.ext4 /dev/sdb1
[root@localhost ~]# mkfs.ext4 /dev/sdb2
[root@localhost ~]# blkid | grep sdb
/dev/sdb1: UUID="2b5cb878-92f0-4b5f-8c95-eae73fa9b152" TYPE="ext4" 
/dev/sdb2: UUID="d96656d9-97e3-4459-88aa-f6d5dbff1300" TYPE="ext4" 
  • 挂载/boot分区
[root@localhost ~]# mkdir /mnt/boot
[root@localhost ~]# mount /dev/sdb1 /mnt/boot
  • 安装 grub
[root@localhost ~]# 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
  • 复制内核文件
cp /boot/vmlinuz-2.6.32-754.el6.x86_64 /boot/initramfs-2.6.32-754.el6.x86_64.img /mnt/boot
  • 复制(创建)grub.conf文件
mkdir /mnt/boot/grub
vim /mnt/boot/grub/grub.conf

## 添加以下内容,保存后重启
default=0
timeout=3
title linux 6.10
kernel /vmlinuz-2.6.32-754.el6.x86_64 ro root=/dev/sda2 selinux=0 init=/bin/bash
initrd /initramfs-2.6.32-754.el6.x86_64.img
  • 挂载根目录
mkdir /mnt/sysroot
mount /dev/sdb2 /mnt/sysroot
  • 创建根下一级目录
mkdir –pv /mnt/sysroot/{etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}
  • 复制bash和相关库文件

    脚本

#!/bin/bash
dest_root=/mnt/sysroot
cmd_name=$1
cmd_path=$(which --skip-alias $cmd_name)
[ -e $dest_root/$cmd_path ] || cp --parent -v $cmd_path $dest_root
ldd $cmd_path | grep -oE "/.* " | while read libfile;do
        lib_dir=$(dirname $libfile)
        while true;do
                [ -e $dest_root/$libfile ] || cp -d -v --parent $libfile $dest_root
                libfile=$lib_dir/$(readlink $libfile)
                [ $? -ne 0 ] && break
        done
done

​ 复制:bash,sh,ls,ifconfig,ping,hostname,cat,vi,vim,mount,umount,ps,pstree,mv,touch,cp目录结构和内容如下:

[root@localhost ~]# chmod +x cpcmd.sh
[root@localhost ~]# ./cpcmd.sh bash
[root@localhost ~]# ./cpcmd.sh sh
[root@localhost ~]# ./cpcmd.sh ls
[root@localhost ~]# ./cpcmd.sh ifconfig
[root@localhost ~]# ./cpcmd.sh ping
[root@localhost ~]# ./cpcmd.sh hostname
[root@localhost ~]# ./cpcmd.sh cat
[root@localhost ~]# ./cpcmd.sh vi
[root@localhost ~]# ./cpcmd.sh vim
[root@localhost ~]# ./cpcmd.sh mount
[root@localhost ~]# ./cpcmd.sh umount
[root@localhost ~]# ./cpcmd.sh ps
[root@localhost ~]# ./cpcmd.sh pstree
[root@localhost ~]# ./cpcmd.sh mv
[root@localhost ~]# ./cpcmd.sh touch
[root@localhost ~]# ./cpcmd.sh cp
[root@localhost ~]# ./cpcmd.sh tree
  • 复制网卡驱动
# 查看驱动名称
ethtool -i eth0
    
# 查看驱动文件
modinfo e1000
    
# 复制驱动
cp /lib/modules/2.6.32-754.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib64
  • 切根查看软件
chroot /mnt/sysroot
tree /
  • 取消挂载
umount /dev/sdb1 /dev/sdb2
  • 从虚拟机取消硬盘,然后新建虚拟机,用这个制作好的硬盘,开机后自动进入sh 命令行。

2、CentOS 7

  • 添加一块容量为 20G 的新硬盘;
[root@localhost ~]# lsblk | grep sdb
sdb      8:16   0   20G  0 disk 
  • 创建分区( /boot:1G , / :19G )
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x6d818231.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (2099200-41943039, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Linux and of size 19 GiB is set

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6d818231

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200    41943039    19921920   83  Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 格式化分区
[root@localhost ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              isize=512    agcount=4, agsize=1245120 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=4980480, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
  • 挂载 boot
[root@localhost ~]# mkdir /mnt/sysimage
[root@localhost ~]# mount /dev/sdb2 /mnt/sysimage
[root@localhost ~]# mkdir /mnt/sysimage/boot
[root@localhost ~]# mount /dev/sdb1 /mnt/sysimage/boot
  • 安装 GRUB
[root@localhost ~]# grub2-install --boot-directory=/mnt/sysimage/boot /dev/sdb
Installing for i386-pc platform.
Installation finished. No error reported.
  • 复制 grub.cfg 文件
[root@localhost ~]# cp /boot/grub2/grub.cfg /mnt/sysimage/boot/grub2/
  • 修改 grub.cfg 文件中的 UUID (不要直接复制下面的命令,以实际的值为准)
[root@localhost ~]# sed -i "s/`blkid -s UUID -o value /dev/sda1`/`blkid -s UUID -o value /dev/sdb1`/" /mnt/sysimage/boot/grub2/grub.cfg
[root@localhost ~]# sed -i "s/`blkid -s UUID -o value /dev/sda2`/`blkid -s UUID -o value /dev/sdb2`/" /mnt/sysimage/boot/grub2/grub.cfg
  • 在 grub.cfg 文件的第一个 linux 16 这行添加 init=/bin/bash,详情如下:
linux16 /vmlinuz-3.10.0-1062.el7.x86_64 root=UUID=d1f045bd-0ca2-4cc0-b188-de756ba98246 ro net.ifnames=0 crashkernel=auto rhgb quiet LANG=en_US.UTF-8 init=/bin/bash
  • 在根目录下创建一级目录和二级目录
[root@localhost ~]# mkdir -p /mnt/sysimage/{etc,dev,proc,sys,home,var,run,mnt,root,tmp,usr/{bin,sbin,lib,lib64}}
  • 创建软链接
[root@localhost ~]# cd /mnt/sysimage
[root@localhost sysimage]# ln -s usr/bin bin
[root@localhost sysimage]# ln -s usr/sbin sbin
[root@localhost sysimage]# ln -s usr/lib lib
[root@localhost sysimage]# ln -s usr/lib64 lib64
  • 复制内核和 initramfs 文件
[root@localhost ~]# cp /boot/vmlinuz-* /mnt/sysimage/boot/
[root@localhost ~]# cp /boot/initramfs-* /mnt/sysimage/boot/
  • 创建版本信息文件
[root@localhost ~]# cat /etc/system-release > /mnt/sysimage/etc/os-release
  • 复制bash和相关库文件

    脚本

#!/bin/bash
dest_root=/mnt/sysimage
cmd_name=$1
cmd_path=$(which --skip-alias $cmd_name)
[ -e $dest_root/$cmd_path ] || cp --parent -v $cmd_path $dest_root
ldd $cmd_path | grep -oE "/.* " | while read libfile;do
        lib_dir=$(dirname $libfile)
        while true;do
                [ -e $dest_root/$libfile ] || cp -d -v --parent $libfile $dest_root
                libfile=$lib_dir/$(readlink $libfile)
                [ $? -ne 0 ] && break
        done
done

​ 复制:bash,sh,ls,ifconfig,ping,hostname,cat,vi,vim,mount,umount,ps,pstree,mv,touch,cp目录结构和内容如下:

[root@localhost ~]# chmod +x cpcmd.sh
[root@localhost ~]# ./cpcmd.sh bash
[root@localhost ~]# ./cpcmd.sh sh
[root@localhost ~]# ./cpcmd.sh ls
[root@localhost ~]# ./cpcmd.sh ifconfig
[root@localhost ~]# ./cpcmd.sh ping
[root@localhost ~]# ./cpcmd.sh hostname
[root@localhost ~]# ./cpcmd.sh cat
[root@localhost ~]# ./cpcmd.sh vi
[root@localhost ~]# ./cpcmd.sh vim
[root@localhost ~]# ./cpcmd.sh mount
[root@localhost ~]# ./cpcmd.sh umount
[root@localhost ~]# ./cpcmd.sh ps
[root@localhost ~]# ./cpcmd.sh pstree
[root@localhost ~]# ./cpcmd.sh mv
[root@localhost ~]# ./cpcmd.sh touch
[root@localhost ~]# ./cpcmd.sh cp
  • 取消挂载
umount /dev/sdb1 /dev/sdb2
  • 从虚拟机取消硬盘,然后新建虚拟机,用这个制作好的硬盘,开机后自动进入命令行。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值