自制Linux
本章描述在VM上自制Linux系统的过程。
1. 在运行的虚拟机上加一块新的硬盘20G
分两个分区,如下
[root@CentOS63 rc.d]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 5.8G 0 rom
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 100G 0 part /
├─sda2 8:2 0 9.8G 0 part /data
└─sda3 8:3 0 2G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 203.9M 0 part
└─sdb2 8:18 0 19.8G 0 part
2. 做文件系统
mkfs.ext4 /dev/sdb2
mkfs.ext4 /dev/sdb1
[root@CentOS63 rc.d]# blkid
/dev/sda1: UUID="cb5f4231-e447-4b59-ba38-5357be9b67b1" TYPE="ext4"
/dev/sda2: UUID="07ac6d5a-d706-4401-9f85-7fdb0b7f4ce6" TYPE="ext4"
/dev/sda3: UUID="be67d9c3-4756-4903-b225-cd17e8db2b28" TYPE="swap"
/dev/sdb1: UUID="c94613a4-28b0-4570-b830-7e470ea5847d" TYPE="ext4"
/dev/sdb2: UUID="32c5566f-440e-49ba-91f0-7d32285e3f7a" TYPE="ext4"
3. 挂载boot
mkdir /mnt/boot 子目录必须为boot
mount /dev/sdb1 /mnt/boot
4. 安装grub
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
查看MBR的bootloader信息:
hexdump -C /dev/sdb -n 512 -v
5. 编辑grub.cfg
vim /mnt/boot/grub.conf
default=0
timeout=5
title Linux
kernel /vmlinuz root=UUID=32c5566f-440e-49ba-91f0-7d32285e3f7a selinux=0 init=/bin/bash
initrd /initramfs.img
6. copy内核文件以及ramfts initramfs文件
cp /boot/vmlinuz /mnt/boot/
cp /boot/initramfs /mnt/boot/
7. 创建一级目录
mkdir /mnt/sysroot
mount /dev/sdb2 /mnt/sysroot
用脚本copy相应的命令以及依赖的库文件
8. 生成rootfs下的一级目录
mkdir –pv /mnt/sysroot/{etc,lib,lib64,bin,sbin,tmp,var,usr,sys,proc,opt,home,root,boot,dev,mnt,media}
同时copy网卡的驱动文件
modinfo e1000
cp /lib/modules/2.6.32-696.el6.x86_64/kernel/drivers/net/e1000/e1000.ko /mnt/sysroot/lib/
9. 测试自制Linux
把sdb硬盘安装到新的VM上,启动
加载网卡模块insmod /lib/e1000.ko
配置IP, ifconfig eth0 192.168.30.44/24
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # #******************************************************************** #Author: Liang Jindong #QQ: 570878199 #Date: 2018-05-14 #FileName: copycmd.sh #URL: http://www.magedu.com #Description: The test script #Copyright (C): 2018 All rights reserved #******************************************************************** ROOTFS_DIR="/mnt/sysroot" FUNC_CP_BIN() { if which $1 &> /dev/null; then SOURCE_BIN=`which --skip-alias $1` SOURCE_DIR=`dirname $SOURCE_BIN` [ -d ${ROOTFS_DIR}${SOURCE_DIR} ] || mkdir -p ${ROOTFS_DIR}${SOURCE_DIR} [ -f ${ROOTFS_DIR}${SOURCE_BIN} ] || cp ${SOURCE_BIN} ${ROOTFS_DIR}${SOURCE_DIR} return 0 else echo "Command not found." return 1 fi } FUNC_CP_SO(){ ldd ${SOURCE_BIN} | egrep '=> /' > SO_FILE while read LINES; do SOURCE_SO=`echo $LINES | sed -r 's@.*=> (\/.*) .*@\1@g'` SO_DIR=`dirname $SOURCE_SO` [ -d ${ROOTFS_DIR}${SO_DIR} ] || mkdir -p ${ROOTFS_DIR}${SO_DIR} [ -f ${ROOTFS_DIR}${SOURCE_SO} ] || cp $SOURCE_SO ${ROOTFS_DIR}${SO_DIR} done < /app/test/script/Linux_selfmade/SO_FILE } read -p "Please input a command:" ANS until [ "$ANS" == "quit" ]; do FUNC_CP_BIN $ANS FUNC_CP_SO read -p "Please input a command:" ANS done