forked from loaden/bash-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path63-grub-final.sh
executable file
·322 lines (280 loc) · 10.3 KB
/
63-grub-final.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/bin/bash
# QQ群:111601117、钉钉群:35948877
# 避免chroot后执行
id lfs >/dev/null 2>&1
if [ $? = 0 ]; then
source `dirname ${BASH_SOURCE[0]}`/lfs.sh
# 下载 UEFI Boot for GRUB 的依赖源码
# BLFS国内镜像源 https://mirrors.aliyun.com/blfs/
pushd $LFS/sources/$(getConf LFS_VERSION)
# 删除缓存
rm -fv wget-*
# 依赖1: https://linuxfromscratch.org/blfs/view/stable-systemd/postlfs/efivar.html
PKG_NAME=efivar
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
while [ -z $PKG_PATH ]
do
# wget https://github.com/rhboot/efivar/releases/download/38/efivar-38.tar.bz2
wget https://mirrors.aliyun.com/blfs/11.2/e/efivar-38.tar.bz2
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
sleep 1
done
# 依赖2:https://linuxfromscratch.org/blfs/view/stable-systemd/postlfs/efibootmgr.html
PKG_NAME=efibootmgr
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
while [ -z $PKG_PATH ]
do
# wget https://github.com/rhboot/efibootmgr/archive/18/efibootmgr-18.tar.gz
wget https://mirrors.aliyun.com/blfs/11.2/e/efibootmgr-18.tar.gz
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
sleep 1
done
# efibootmgr 依赖: https://linuxfromscratch.org/blfs/view/stable-systemd/general/popt.html
PKG_NAME=popt
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
while [ -z $PKG_PATH ]
do
wget http://ftp.rpm.org/popt/releases/popt-1.x/popt-1.18.tar.gz
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
sleep 1
done
# initramfs 依赖: https://www.linuxfromscratch.org/blfs/view/svn/general/cpio.html
PKG_NAME=cpio
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
while [ -z $PKG_PATH ]
do
wget https://ftp.gnu.org/gnu/cpio/cpio-2.13.tar.bz2
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
sleep 1
done
# dracut 生成 initramfs:https://github.com/dracutdevs/dracut
PKG_NAME=dracut
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
while [ -z $PKG_PATH ]
do
wget https://github.com/dracutdevs/dracut/archive/refs/tags/059.tar.gz
[ -f 059.tar.gz ] && mv -vf 059.tar.gz dracut-059.tar.gz
PKG_PATH=$(find . -maxdepth 1 -type f -name "$PKG_NAME-*.*")
sleep 1
done
popd
# 准备chroot
cp -v ${BASH_SOURCE[0]} $LFS/task.sh
sed "s/_LFS_VERSION/$(getConf LFS_VERSION)/g" -i $LFS/task.sh
sed "s/_LFS_BUILD_PROC/$LFS_BUILD_PROC/g" -i $LFS/task.sh
source `dirname ${BASH_SOURCE[0]}`/chroot.sh
rm -fv $LFS/task.sh
exit
fi
# 来自chroot之后的调用
# GRUB 引导,需要借助BLFS:https://linuxfromscratch.org/blfs/view/stable-systemd/
# 详见 Packages for UEFI Boot
pushd /sources/_LFS_VERSION
PKG_NAME=efivar
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
# First, fix an issue in Makefile causing the package to be rebuilt during installation
sed '/prep :/a\\ttouch prep' -i src/Makefile
# Now adapt this package for a change in glibc-2.36
sed '/sys\/mount\.h/d' -i src/util.h
sed '/unistd\.h/a#include <sys/mount.h>' -i src/gpt.c src/linux.c
# 不编译文档,移除对 mandoc 的依赖
sed '/SUBDIRS := /s/src docs/src/' -i Makefile
[ $? = 0 ] && make
[ $? = 0 ] && make install LIBDIR=/usr/lib
if [ $? = 0 ]; then
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
pushd /sources/_LFS_VERSION
PKG_NAME=popt
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
./configure --prefix=/usr --disable-static
[ $? = 0 ] && make
[ $? = 0 ] && make check && read -p "$PKG_NAME CHECK DONE..."
[ $? = 0 ] && make install
if [ $? = 0 ]; then
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
pushd /sources/_LFS_VERSION
PKG_NAME=efibootmgr
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
[ $? = 0 ] && make EFIDIR=LFS EFI_LOADER=grubx64.efi
[ $? = 0 ] && make install EFIDIR=LFS
if [ $? = 0 ]; then
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
pushd /sources/_LFS_VERSION
PKG_NAME=grub
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
unset {C,CPP,CXX,LD}FLAGS
./configure --prefix=/usr \
--sysconfdir=/etc \
--disable-efiemu \
--with-platform=efi \
--target=x86_64 \
--disable-werror &&
unset TARGET_CC &&
make
[ $? = 0 ] && make install
if [ $? = 0 ]; then
mv -v /etc/bash_completion.d/grub /usr/share/bash-completion/completions
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
pushd /sources/_LFS_VERSION
PKG_NAME=cpio
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
sed -i '/The name/,+2 d' src/global.c
./configure --prefix=/usr \
--enable-mt \
--with-rmt=/usr/libexec/rmt &&
make &&
makeinfo --html -o doc/html doc/cpio.texi &&
makeinfo --html --no-split -o doc/cpio.html doc/cpio.texi &&
makeinfo --plaintext -o doc/cpio.txt doc/cpio.texi
[ $? = 0 ] && make check && read -p "$PKG_NAME CHECK DONE..."
[ $? = 0 ] && make install
if [ $? = 0 ]; then
install -v -m755 -d /usr/share/doc/cpio-2.13/html &&
install -v -m644 doc/html/* \
/usr/share/doc/cpio-2.13/html &&
install -v -m644 doc/cpio.{html,txt} \
/usr/share/doc/cpio-2.13
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
pushd /sources/_LFS_VERSION
PKG_NAME=dracut
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
if [ -z $PKG_PATH ]; then
tar -xpvf $(find . -maxdepth 1 -type f -name "$PKG_NAME-*.tar.*") --directory stage5
PKG_PATH=$(find stage5 -maxdepth 1 -type d -name "$PKG_NAME-*")
fi
if [ ! -f $PKG_PATH/_BUILD_DONE ]; then
pushd $PKG_PATH
./configure \
--sysconfdir=/etc \
--sbindir=/sbin \
--disable-documentation
[ $? = 0 ] && make
[ $? = 0 ] && make install
if [ $? = 0 ]; then
read -p "$PKG_NAME ALL DONE..."
touch _BUILD_DONE
else
pwd
exit 1
fi
popd
fi
popd
# dracut 生成 initramfs
dracut /boot/initramfs.img \
--modules "base fs-lib udev-rules rootfs-block kernel-modules systemd systemd-initrd" \
--force --hostonly --fstab --zstd --kver 5.19.2
ls -lh /boot
# GRUB 安装与配置
# 启动入口
# If the system is booted with UEFI and systemd, efivarfs will be mounted automatically.
# However in the LFS chroot environment it still needs to be mounted manually.
mountpoint /sys/firmware/efi/efivars || mount -v -t efivarfs efivarfs /sys/firmware/efi/efivars
grub-install --bootloader-id=LFS --recheck
# 检查生成的启动项
efibootmgr | cut -f 1
# 编写启动菜单
cat > /boot/grub/grub.cfg << EOF
# Begin /boot/grub/grub.cfg
set timeout_style=menu
set timeout=5
insmod part_gpt
insmod fat
insmod btrfs
insmod zstd
insmod efi_gop
insmod efi_uga
set root=(hd0,6)
menuentry "Linux From Scratch" {
linux /@lfs/boot/vmlinuz root=/dev/nvme0n2 rootflags=subvol=@lfs loglevel=3 ro
initrd /@lfs/boot/initramfs.img
}
menuentry "Firmware Setup" {
fwsetup
}
EOF
# 系统发布信息
echo 11.2-systemd > /etc/lfs-release
cat > /etc/lsb-release << "EOF"
DISTRIB_ID="Linux From Scratch"
DISTRIB_RELEASE="11.2-systemd"
DISTRIB_CODENAME="loaden"
DISTRIB_DESCRIPTION="Linux From Scratch"
EOF
cat > /etc/os-release << "EOF"
NAME="Linux From Scratch"
VERSION="11.2-systemd"
ID=lfs
PRETTY_NAME="Linux From Scratch 11.2-systemd"
VERSION_CODENAME="loaden"
EOF