1、自定义 Linux
- 创建新硬盘,用
lsblk
命令可以看到设备名称为 /dev/sdb
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 50G 0 part /
└─sda3 8:3 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
sr0 11:0 1 1024M 0 rom
- 创建分区(/boot:500M,/:剩余空间)
[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 0x6922026b.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response 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): +500M
Partition 1 of type Linux and of size 500 MiB is set
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (1026048-41943039, default 1026048):
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-41943039, default 41943039):
Using default value 41943039
Partition 2 of type Linux and of size 19.5 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: 0x6922026b
Device Boot Start End Blocks Id System
/dev/sdb1 2048 1026047 512000 83 Linux
/dev/sdb2 1026048 41943039 20458496 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=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=855, 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=1278656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5114624, 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-1127.el7.x86_64 root=UUID=1db7b83d-dce4-4fa4-a6e9-86fd20d1dceb ro net.ifnames=0 crashkernel=auto rhgb quiet LANG=en_US.UTF-8 init=/bin/bash
- 复制内核和 initramfs 文件
[root@localhost ~]# cp /boot/vmlinuz-* /mnt/sysimage/boot/
[root@localhost ~]# cp /boot/initramfs-* /mnt/sysimage/boot/
- 创建版本信息文件
[root@localhost ~]# mkdir /mnt/sysimage/etc
[root@localhost ~]# cat /etc/system-release > /mnt/sysimage/etc/os-release
2、编译 busybox(如事先已完成,无需单独编译)
- 安装编译必须的软件
[root@localhost ~]# yum install -y gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel zlib-devel libmcrypt-devel glibc-static ncurses-devel
- 下载 busybox 安装包
[root@localhost ~]# wget https://busybox.net/downloads/busybox-1.31.1.tar.bz2
- 解压
[root@localhost ~]# tar xvf busybox-1.31.1.tar.bz2
- 编译
[root@localhost ~]# cd busybox-1.31.1
[root@localhost busybox-1.31.1]# make menuconfig
- 按下面选择,把 busybox 编译也静态二进制、不用共享库
Settings --> Build options --> Build static binary (no shared libs)
(1)进入后默认选中 Settings,按回车进入
(2)光标下移至”Build static binary (no shared libs) “,按空格选中
(3)按 tab 键选中"exit"按回车(退出两次),提出是否保存,选中"yes",直接按回车保存
[root@localhost busybox-1.31.1]# make && make install
- 将编译后的文件复制到新硬盘
[root@localhost busybox-1.31.1]# cp -a _install/* /mnt/sysimage/
3、开机运行
- 配置IP,测试网络
ip link set eth0 up
ip a a 10.10.10.100/24 dev eth0
ping 10.10.10.2
- 测试应用程序(各种命令运行正常,此处不列出)