目录
  1. 1. LFS系统构建06-定制LFS镜像
    1. 1.1. 打包lfs目录
    2. 1.2. 制作livecd
LFS系统构建06-定制LFS镜像

LFS系统构建06-定制LFS镜像

打包lfs目录

1
2
3
4
5
6
7
export LFS=/mnt/lfs 
#将根目录打包,用来制作系统分区
cd $LFS
tar jcvpf ~/lfs-6.3.tar.bz2 ./* --exclude boot # 如果命令不识别,就分开来,一个个的打包
#将boot目录打包,用来制作boot分区
cd $LFS/boot
tar jcvpf ~/boot.tar.bz2 ./*

制作livecd

  • 配置基本环境
1
2
3
export ISO=~/iso
mkdir $ISO
mkdir -pv $ISO/boot/grub
1
mv stage2_eltorito $LFS/boot/grub/
1
2
#下载initramfs.gz(我这里是用的gentoo的initrd启动辅助系统)
cp -rv initramfs.gz $ISO/boot/
  • 将你的基本系统中的boot目录中的内核复制过来
1
cp $LFS/boot/vmlinuz-4.20.12-lfs-8.4 $ISO/boot/
  • 创建grub的引导配置文件
1
2
3
4
5
6
7
default 0
timeout 30
color white/black black/light-gray
title Install rack-leen liveCD
root (cd)
kernel /boot/vmlinuz-4.20.12-lfs-8.4 # 你的引导内核的位置
initrd /boot/initramfs.gz # 你的辅助系统的位置
  • 创建安装脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#===============================================================================
#
# FILE: install.sh
#
# USAGE: ./install.sh
#
# DESCRIPTION:
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: rack-leen
# ORGANIZATION:
# CREATED: 2019年06月07日 18时19分09秒
# REVISION: ---
#===============================================================================
echo "start install ..."
#find the disk 找到存在的磁盘
DISK=`find /dev -type b | grep '.*[shv]d[a-d][0-9]' | sed -e 's/.$//' | head -1`
echo "Your DISK is $DISK"
DISK_PRE=`echo $DISK | sed -e 's/.$//'`
#set the partition 设置分区
echo "Input Your SWAP Partition for a number:"
read SWAP_NUM # swap分区号
DISK_SWAP=${DISK}${SWAP_NUM}
echo "Input Your BOOT Partition for a number:"
read BOOT_NUM # boot分区号
DISK_BOOT=${DISK}${BOOT_NUM}
echo "Input Your ROOT Partition for a number:"
read ROOT_NUM # /分区号
DISK_ROOT=${DISK}${ROOT_NUM}
#init swap partition 初始化swap分区
mkswap $DISK_SWAP

#swapon
$DISK_SWAP
#init root partition 初始化根分区
mkfs.ext4 $DISK_ROOT
mount $DISK_ROOT /mnt/lfs # 挂载根分区
cd /mnt/lfs
tar jxvpf /install/lfs-8.4.tar.bz2 #将根分区文件解压到根分区
cp ${DISK_PRE}* /mnt/lfs/dev/ -R #将dev目录下的设备全部复制到dev目录
echo "install ROOT finished"

#init boot partition 初始化boot分区
mkfs -t ext2 $DISK_BOOT
mount $DISK_BOOT /mnt/boot
cd /mnt/boot
tar jxvpf /install/boot.tar.bz2
echo "install BOOT finished"

#init fstab
echo "${DISK_ROOT} / ext3 defaults 1 1">/mnt/lfs/etc/fstab
echo "${DISK_SWAP} swap swap pri=1 0 0">>/mnt/lfs/etc/fstab
echo "${DISK_BOOT} /boot ext3 defaults 0 0">>/mnt/lfs/etc/fstab
echo "proc /proc proc defaults 0 0">>/mnt/lfs/etc/fstab
echo "sysfs /sys sysfs defaults 0 0">>/mnt/lfs/etc/fstab
echo "devpts /dev/pts devpts gid=4,mode=620 0 0">>/mnt/lfs/etc/fstab
echo "shm /dev/shm tmpfs defaults 0 0">>/mnt/lfs/etc/fstab

#init grub grub.cfg
if [ $BOOT_NUM =1 ] ; then
GRUB_BOOT=0
else
GRUB_BOOT=`expr $BOOT_NUM - 1`
fi

# 将grub配置写入配置文件中
echo "
default 0
timeout 30
color white/black black/light-gray
title rack-leen OS 1.0
root (hd0,$GRUB_BOOT)
kernel /boot/vmlinuz-4.20.12-lfs-8.4 root=${DISK_ROOT} vga=788
" >> /mnt/boot/grub/grub.cfg

# check ip address
check_ip() {
echo $1 | grep "^\(\(25[0-5]\|2[0-4][0-9]\|1[0-9]\{2\}\|[1-9][0-9]\|[0-9]\)\.\)\{3\}\(25[0-5]\|2[0-4][0-9]\|1[0-9]\{2\}\|[1-9][0-9]\|[0-9]\)$" > /dev/null
if [ $? -ne 0 ]; then
return 1
fi

ipaddr=$1
a=$(echo $ipaddr | awk -F'.' '{print $1}')
b=$(echo $ipaddr | awk -F'.' '{print $2}')
c=$(echo $ipaddr | awk -F'.' '{print $3}')
d=$(echo $ipaddr | awk -F'.' '{print $4}')

for n in $a $b $c $d; do
if [ $n -gt 255 ] || [ $n -lt 0 ]; then
return 2
fi
done

return 0
}

# init ip address
NETCARD=$(cat /proc/net/dev | grep "eth" | sed -e "s/^\s*\(eth.*\)\:.*$/\1/")

for NET in $NETCARD; do
IP=; read -p "Input IP address for $NET: " IP
check_ip $IP
while [ $? -ne 0 ]; do
read -p "Invalid input. ReInput IP address for $NET: " IP
check_ip $IP
done

GATEWAY=; read -p "Input GATEWAY address for $NET: " GATEWAY
check_ip $GATEWAY
while [ $? -ne 0 ]; do
read -p "Invalid input. ReInput GATEWAY address for $NET: " GATEWAY
check_ip $GATEWAY
done

BROADCAST=$(echo "$GATEWAY" | sed -e "s/^\([0-9]\{1,3\}\.[0-9]*\.[0-9]*\)\.[0-9]*$/\1.255/")

echo \
"ONBOOT=yes
IFACE=$NET
SERVICE=ipv4-static
IP=$IP
GATEWAY=$GATEWAY
PREFIX=24
BROADCAST=$BROADCAST
" > /mnt/lfs/etc/sysconfig/ifconfig.$NET

mkdir -p /mnt/lfs/etc/sysconfig/network-scripts
ln -s ../ifconfig.$NET /mnt/lfs/etc/sysconfig/network-scripts/ifcfg-$NET

echo "Init $NET ip finished"
done


#reset MAC address
rm -f /mnt/lfs/etc/udev/rules.d/70-persistent-net.rules

#chroot and MBR
cd /
umount /mnt/boot
mount $DISK_BOOT /mnt/lfs/boot
chroot /mnt/lfs /bin/bash -c "echo -e 'root (hd0,$GRUB_BOOT)\nsetup (hd0)\nquit' | /usr/sbin/grub"

echo "******************************************************************"
echo "******************** install finish now reboot *******************"
echo "******************************************************************"
chroot /mnt/lfs/ /bin/bash -c "reboot"
  • 创建install目录,用来存放打包的系统包(根分区包和boot分区包)和安装脚本
1
2
3
4
5
cd $ISO
mkdir -pv $ISO/install
cp boot.tar.bz2 ./install
cp lfs-8.4.tar.bz2 ./install
cp install.sh ./install
  • 创建镜像文件
1
2
cd $ISO
mkisofs -R -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o liveCD-lfs-8.4.iso /home/rackleen/iso
  • 最终的目录结构
1
2
3
4
5
6
7
8
9
10
11
├── boot
│   ├── grub
│   │   ├── menu.lst
│   │   └── stage2_eltorito
│   ├── initramfs.gz
│   └── vmlinuz-4.20.12-lfs-8.4
├── install
│   ├── boot.tar.bz2
│   ├── install.sh
│   └── lfs-8.4.tar.bz2
└── liveCD-lfs-8.4.iso
  • 效果图
    grub
文章作者: rack-leen
文章链接: http://yoursite.com/2019/06/07/LFS/LFS%E7%B3%BB%E7%BB%9F%E6%9E%84%E5%BB%BA06-%E5%AE%9A%E5%88%B6LFS%E9%95%9C%E5%83%8F/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 rack-leen's blog
打赏
  • 微信
  • 支付宝

评论