-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-iso.sh
545 lines (460 loc) · 15.2 KB
/
make-iso.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/bin/bash
# Script to make the Linux From Scratch Live ISO
if [[ ! -f /usr/bin/mksquashfs ]]; then
echo "ERROR: Squashfs-tools not found"
echo "Please build them from: https://github.com/plougher/squashfs-tools"
fi
if [[ ! -f /usr/bin/mmd ]]; then
echo "ERROR: mtools not found"
echo "Please build them from: https://www.gnu.org/software/mtools/"
fi
# Change these
# LFS Chroot
ROOT="/media/EXSTOR/lfs-iso/chroot"
# Where most of the temporary work will be done
TEMP_DIR="/media/EXSTOR/lfs-iso"
FIRMWARE_VERSION="20230625"
KERNEL_VER=6.4.4
cd $TEMP_DIR
# Install Linux Firmware
wget https://cdn.kernel.org/pub/linux/kernel/firmware/linux-firmware-$FIRMWARE_VERSION.tar.xz
tar -xvf linux-firmware-$FIRMWARE_VERSION.tar.xz
cd linux-firmware-$FIRMWARE_VERSION
# Install the compressed firmware files (needs support in kernel)
sudo make DESTDIR=$ROOT FIRMWAREDIR=/usr/lib/firmware install-xz
# Install mkinitramfs and make the initrd
cat > $ROOT/usr/sbin/mkinitramfs << "EOF"
#!/bin/bash
# This file based in part on the mkinitramfs script for the LFS LiveCD
# written by Alexander E. Patrakov and Jeremy Huntwork.
copy()
{
local file
if [ "$2" = "lib" ]; then
file=$(PATH=/usr/lib type -p $1)
else
file=$(type -p $1)
fi
if [ -n "$file" ] ; then
cp $file $WDIR/usr/$2
else
echo "Missing required file: $1 for directory $2"
rm -rf $WDIR
exit 1
fi
}
if [ -z $1 ] ; then
INITRAMFS_FILE=initrd.img-no-kmods
else
KERNEL_VERSION=$1
INITRAMFS_FILE=initrd.img-$KERNEL_VERSION
fi
if [ -n "$KERNEL_VERSION" ] && [ ! -d "/usr/lib/modules/$1" ] ; then
echo "No modules directory named $1"
exit 1
fi
printf "Creating $INITRAMFS_FILE... "
binfiles="sh cat cp dd killall ls mkdir mknod mount "
binfiles="$binfiles umount sed sleep ln rm uname"
binfiles="$binfiles readlink basename"
# Systemd installs udevadm in /bin. Other udev implementations have it in /sbin
if [ -x /usr/bin/udevadm ] ; then binfiles="$binfiles udevadm"; fi
sbinfiles="modprobe blkid switch_root"
# Optional files and locations
for f in mdadm mdmon udevd udevadm; do
if [ -x /usr/sbin/$f ] ; then sbinfiles="$sbinfiles $f"; fi
done
# Add lvm if present (cannot be done with the others because it
# also needs dmsetup
if [ -x /usr/sbin/lvm ] ; then sbinfiles="$sbinfiles lvm dmsetup"; fi
unsorted=$(mktemp /tmp/unsorted.XXXXXXXXXX)
DATADIR=/usr/share/mkinitramfs
INITIN=init.in
# Create a temporary working directory
WDIR=$(mktemp -d /tmp/initrd-work.XXXXXXXXXX)
# Create base directory structure
mkdir -p $WDIR/{dev,run,sys,proc,usr/{bin,lib/{firmware,modules},sbin}}
mkdir -p $WDIR/etc/{modprobe.d,udev/rules.d}
touch $WDIR/etc/modprobe.d/modprobe.conf
ln -s usr/bin $WDIR/bin
ln -s usr/lib $WDIR/lib
ln -s usr/sbin $WDIR/sbin
ln -s lib $WDIR/lib64
# Create necessary device nodes
mknod -m 640 $WDIR/dev/console c 5 1
mknod -m 664 $WDIR/dev/null c 1 3
# Install the udev configuration files
if [ -f /etc/udev/udev.conf ]; then
cp /etc/udev/udev.conf $WDIR/etc/udev/udev.conf
fi
for file in $(find /etc/udev/rules.d/ -type f) ; do
cp $file $WDIR/etc/udev/rules.d
done
# Install any firmware present
cp -a /usr/lib/firmware $WDIR/usr/lib
# Copy the RAID configuration file if present
if [ -f /etc/mdadm.conf ] ; then
cp /etc/mdadm.conf $WDIR/etc
fi
# Install the init file
install -m0755 $DATADIR/$INITIN $WDIR/init
if [ -n "$KERNEL_VERSION" ] ; then
if [ -x /usr/bin/kmod ] ; then
binfiles="$binfiles kmod"
else
binfiles="$binfiles lsmod"
sbinfiles="$sbinfiles insmod"
fi
fi
# Install basic binaries
for f in $binfiles ; do
ldd /usr/bin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
copy /usr/bin/$f bin
done
for f in $sbinfiles ; do
ldd /usr/sbin/$f | sed "s/\t//" | cut -d " " -f1 >> $unsorted
copy $f sbin
done
# Add udevd libraries if not in /usr/sbin
if [ -x /usr/lib/udev/udevd ] ; then
ldd /usr/lib/udev/udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
elif [ -x /usr/lib/systemd/systemd-udevd ] ; then
ldd /usr/lib/systemd/systemd-udevd | sed "s/\t//" | cut -d " " -f1 >> $unsorted
fi
# Add module symlinks if appropriate
if [ -n "$KERNEL_VERSION" ] && [ -x /usr/bin/kmod ] ; then
ln -s kmod $WDIR/usr/bin/lsmod
ln -s kmod $WDIR/usr/bin/insmod
fi
# Add lvm symlinks if appropriate
# Also copy the lvm.conf file
if [ -x /usr/sbin/lvm ] ; then
ln -s lvm $WDIR/usr/sbin/lvchange
ln -s lvm $WDIR/usr/sbin/lvrename
ln -s lvm $WDIR/usr/sbin/lvextend
ln -s lvm $WDIR/usr/sbin/lvcreate
ln -s lvm $WDIR/usr/sbin/lvdisplay
ln -s lvm $WDIR/usr/sbin/lvscan
ln -s lvm $WDIR/usr/sbin/pvchange
ln -s lvm $WDIR/usr/sbin/pvck
ln -s lvm $WDIR/usr/sbin/pvcreate
ln -s lvm $WDIR/usr/sbin/pvdisplay
ln -s lvm $WDIR/usr/sbin/pvscan
ln -s lvm $WDIR/usr/sbin/vgchange
ln -s lvm $WDIR/usr/sbin/vgcreate
ln -s lvm $WDIR/usr/sbin/vgscan
ln -s lvm $WDIR/usr/sbin/vgrename
ln -s lvm $WDIR/usr/sbin/vgck
# Conf file(s)
cp -a /etc/lvm $WDIR/etc
fi
# Install libraries
sort $unsorted | uniq | while read library ; do
# linux-vdso and linux-gate are pseudo libraries and do not correspond to a file
# libsystemd-shared is in /lib/systemd, so it is not found by copy, and
# it is copied below anyway
if [[ "$library" == linux-vdso.so.1 ]] ||
[[ "$library" == linux-gate.so.1 ]] ||
[[ "$library" == libsystemd-shared* ]]; then
continue
fi
copy $library lib
done
if [ -d /usr/lib/udev ]; then
cp -a /usr/lib/udev $WDIR/usr/lib
fi
if [ -d /usr/lib/systemd ]; then
cp -a /usr/lib/systemd $WDIR/usr/lib
fi
if [ -d /usr/lib/elogind ]; then
cp -a /usr/lib/elogind $WDIR/usr/lib
fi
# Install the kernel modules if requested
if [ -n "$KERNEL_VERSION" ]; then
find \
/usr/lib/modules/$KERNEL_VERSION/kernel/{crypto,fs,lib} \
/usr/lib/modules/$KERNEL_VERSION/kernel/drivers/{block,ata,nvme,md,firewire} \
/usr/lib/modules/$KERNEL_VERSION/kernel/drivers/{scsi,message,pcmcia,virtio} \
/usr/lib/modules/$KERNEL_VERSION/kernel/drivers/usb/{host,storage} \
-type f 2> /dev/null | cpio --make-directories -p --quiet $WDIR
cp /usr/lib/modules/$KERNEL_VERSION/modules.{builtin,order} \
$WDIR/usr/lib/modules/$KERNEL_VERSION
if [ -f /usr/lib/modules/$KERNEL_VERSION/modules.builtin.modinfo ]; then
cp /usr/lib/modules/$KERNEL_VERSION/modules.builtin.modinfo \
$WDIR/usr/lib/modules/$KERNEL_VERSION
fi
depmod -b $WDIR $KERNEL_VERSION
fi
( cd $WDIR ; find . | cpio -o -H newc --quiet | gzip -9 ) > $INITRAMFS_FILE
# Prepare early loading of microcode if available
if ls /usr/lib/firmware/intel-ucode/* >/dev/null 2>&1 ||
ls /usr/lib/firmware/amd-ucode/* >/dev/null 2>&1; then
# first empty WDIR to reuse it
rm -r $WDIR/*
DSTDIR=$WDIR/kernel/x86/microcode
mkdir -p $DSTDIR
if [ -d /usr/lib/firmware/amd-ucode ]; then
cat /usr/lib/firmware/amd-ucode/microcode_amd*.bin > $DSTDIR/AuthenticAMD.bin
fi
if [ -d /usr/lib/firmware/intel-ucode ]; then
cat /usr/lib/firmware/intel-ucode/* > $DSTDIR/GenuineIntel.bin
fi
( cd $WDIR; find . | cpio -o -H newc --quiet ) > microcode.img
cat microcode.img $INITRAMFS_FILE > tmpfile
mv tmpfile $INITRAMFS_FILE
rm microcode.img
fi
# Remove the temporary directories and files
rm -rf $WDIR $unsorted
printf "done.\n"
EOF
mkdir -p $ROOT/usr/share/mkinitramfs
cat > $ROOT/usr/share/mkinitramfs/init.in<< "EOF"
#!/bin/sh
PATH=/usr/bin:/usr/sbin
export PATH
problem()
{
printf "Encountered a problem!\n\nDropping you to a shell.\n\n"
sh
}
no_device()
{
printf "The device %s, which is supposed to contain the\n" $1
printf "root file system, does not exist.\n"
printf "Please fix this problem and exit this shell.\n\n"
}
no_mount()
{
printf "Could not mount device %s\n" $1
printf "Sleeping forever. Please reboot and fix the kernel command line.\n\n"
printf "Maybe the device is formatted with an unsupported file system?\n\n"
printf "Or maybe filesystem type autodetection went wrong, in which case\n"
printf "you should add the rootfstype=... parameter to the kernel command line.\n\n"
printf "Available partitions:\n"
}
do_mount_root()
{
# Make Temporary Directories
mkdir /.root
mkdir -p /mnt
mkdir -p /squash
# Make the loopback interface for the squashfs mount
mknod /dev/loop0 b 7 0
device="/dev/disk/by-label/lfs"
# Mount Rootfs
echo "Mounting USB Container Drive"
# Mount the actual drive to mount the squashfs
mount $device /mnt
echo "Mounting squashfs as overlay"
# Cow = Copy on Write, really just a name
mkdir -p /cow
# Create a ram tmpfs on the entire folder
mount -t tmpfs tmpfs /cow
# This is where the changes are stored
mkdir -p /cow/mod
# Overlayfs needs a buffer directory
mkdir -p /cow/buffer
mount /mnt/boot/lfs.squashfs /squash -t squashfs -o loop
mount -t overlay -o lowerdir=/squash,upperdir=/cow/mod,workdir=/cow/buffer overlay /.root
# Expose the changes directory and the carrier drive to the user
mkdir -p /.root/mnt/changes
mkdir -p /.root/mnt/container
mount --bind /cow/mod /.root/mnt/changes
mount --bind /mnt /.root/mnt/container
}
do_try_resume()
{
case "$resume" in
UUID=* ) eval $resume; resume="/dev/disk/by-uuid/$UUID" ;;
LABEL=*) eval $resume; resume="/dev/disk/by-label/$LABEL" ;;
esac
if $noresume || ! [ -b "$resume" ]; then return; fi
ls -lH "$resume" | ( read x x x x maj min x
echo -n ${maj%,}:$min > /sys/power/resume )
}
init=/sbin/init
root=
rootdelay=
rootfstype=auto
ro="ro"
rootflags=
device=
resume=
noresume=false
mount -n -t devtmpfs devtmpfs /dev
mount -n -t proc proc /proc
mount -n -t sysfs sysfs /sys
mount -n -t tmpfs tmpfs /run
read -r cmdline < /proc/cmdline
for param in $cmdline ; do
case $param in
init=* ) init=${param#init=} ;;
root=* ) root=${param#root=} ;;
rootdelay=* ) rootdelay=${param#rootdelay=} ;;
rootfstype=*) rootfstype=${param#rootfstype=} ;;
rootflags=* ) rootflags=${param#rootflags=} ;;
resume=* ) resume=${param#resume=} ;;
noresume ) noresume=true ;;
ro ) ro="ro" ;;
rw ) ro="rw" ;;
esac
done
# udevd location depends on version
if [ -x /sbin/udevd ]; then
UDEVD=/sbin/udevd
elif [ -x /lib/udev/udevd ]; then
UDEVD=/lib/udev/udevd
elif [ -x /lib/systemd/systemd-udevd ]; then
UDEVD=/lib/systemd/systemd-udevd
else
echo "Cannot find udevd nor systemd-udevd"
problem
fi
${UDEVD} --daemon --resolve-names=never
udevadm trigger
udevadm settle
if [ -f /etc/mdadm.conf ] ; then mdadm -As ; fi
if [ -x /sbin/vgchange ] ; then /sbin/vgchange -a y > /dev/null ; fi
if [ -n "$rootdelay" ] ; then sleep "$rootdelay" ; fi
do_try_resume # This function will not return if resuming from disk
do_mount_root
killall -w ${UDEVD##*/}
exec switch_root /.root "$init" "$@"
EOF
chmod 0755 $ROOT/usr/sbin/mkinitramfs
# Make the ISO directory structure
cd $TEMP_DIR
mkdir -p ISO
cd ISO
mkdir -p boot/grub buffer mod isolinux unmod
# Setup ISOLinux for Legacy BIOS booting
cd $TEMP_DIR
wget https://mirrors.edge.kernel.org/pub/linux/utils/boot/syslinux/Testing/6.04/syslinux-6.04-pre1.tar.xz
tar -xvf syslinux-6.04-pre1.tar.xz
cd syslinux-6.04-pre1
# Copy files from syslinux archive to the ISOLinux directory
# https://wiki.syslinux.org/wiki/index.php?title=ISOLINUX
cp bios/com32/elflink/ldlinux/ldlinux.c32 $TEMP_DIR/ISO/isolinux/
cp bios/core/isolinux.bin $TEMP_DIR/ISO/isolinux/
cp bios/com32/elflink/ldlinux/ldlinux.elf $TEMP_DIR/ISO/isolinux/
cp bios/mbr/isohdpfx.bin $TEMP_DIR/ISO/isolinux/
# Make a ISOLinux configuration file
cat > $TEMP_DIR/ISO/isolinux/isolinux.cfg << "EOF"
menu hshift 4
menu width 70
menu title Linux From Scratch GNU/Linux Live Boot (Legacy Boot)
DEFAULT Linux From Scratch GNU/Linux
LABEL Linux From Scratch GNU/Linux
SAY "Linux From Scratch"
linux /boot/vmlinuz-KERNEL_VER
APPEND initrd=/boot/initrd.img-KERNEL_VER
EOF
# Replace Kernel Ver
sed -i "s/KERNEL_VER/$KERNEL_VER/g" $TEMP_DIR/ISO/isolinux/isolinux.cfg
# EFI Boot Setup
cd $TEMP_DIR/ISO/boot/grub
# Copy the EFI modules
mkdir -p x86_64-efi
cp -rpv $ROOT/usr/lib/grub/x86_64-efi/*.mod x86_64-efi/
# Copy the EFI Font
mkdir -p fonts
cp -pv $ROOT/usr/share/grub/unicode.pf2 fonts/
# Make a configuration file
# The rootdelay kernel parameter is to give the kernel a chance to load the USB driver and recognnize the flash drive before the initrd attempts to mount the drive
cat > $TEMP_DIR/ISO/boot/grub/grub.cfg << "EOF"
set default=0
set timeout=5
insmod ext2
insmod iso9660
set loop=(cd0)
insmod efi_gop
insmod efi_uga
insmod font
search --no-floppy --label lfs --set root
set prefix=($root)/boot/grub
if loadfont ${prefix}/unicode.pf2
then
insmod gfxterm
set gfxmode=auto
set gfxpayload=keep
terminal_output gfxterm
fi
menuentry --hotkey=l 'Linux From Scratch' {
echo 'Loading /boot/vmlinuz-KERNEL_VER ...'
linux /boot/vmlinuz-KERNEL_VER console=tty0 rw rootdelay=5
echo 'Loading /boot/initrd.img-KERNEL_VER ...'
initrd /boot/initrd.img-KERNEL_VER
}
menuentry --hotkey=r 'Reboot' {
reboot
}
EOF
# Change Kernel Version
sed -i "s/KERNEL_VER/$KERNEL_VER/g" $TEMP_DIR/ISO/boot/grub/grub.cfg
# Setup the efi.img file
cd $TEMP_DIR/ISO
# Generate a .efi file with a preconfigured grub embeded into it
# (Once it finds the drive it will load the config off the drive itself)
grub-mkstandalone \
--format=x86_64-efi \
--output=$TEMP_DIR/bootx64.efi \
--locales="" \
--fonts="" \
--compress xz \
boot/grub/grub.cfg
# Wrap that into a clean fat formatted efi.img file (xorriso will take care of the rest if it knows where it is)
cd $TEMP_DIR
# Make a clean img file
dd if=/dev/zero of=efi.img bs=512 count=2880
# Make a filesystem
mkfs.msdos -F 12 -n 'LFS' efi.img
# Use mtools to inject the bootx64.efi into the img and mark the img as bootable/efi compliant
# Make directories
mmd -i efi.img ::EFI
mmd -i efi.img ::EFI/BOOT
mcopy -i efi.img $TEMP_DIR/bootx64.efi ::EFI/BOOT/bootx64.efi
# Copy the efi.img into the boot/grub directory
cp efi.img $TEMP_DIR/ISO/boot/grub
# Generate the initramfs
# Mount Temporary File Systems
if [[ ! $(mount | grep "$ROOT/proc") ]]; then
# Only mount the minimum amount of filesystems needed
mount --bind /dev $ROOT/dev
mount --bind /dev/pts $ROOT/dev/pts
mount --bind /proc $ROOT/proc
mount --bind /sys $ROOT/sys
fi
chroot "$ROOT" /usr/bin/env -i \
HOME=/root \
PATH=/usr/bin:/usr/sbin \
/bin/bash -c "cd / && mkinitramfs $KERNEL_VER"
# Copy the kernel and initrd from the chroot to the ISO
cp $ROOT/boot/vmlinuz-* $TEMP_DIR/ISO/boot/
cp $ROOT/initrd* $TEMP_DIR/ISO/boot
# Unmount temporary file systems and prepare for compression
umount $ROOT/dev/pts
umount $ROOT/dev
umount $ROOT/proc
umount $ROOT/sys
cd $TEMP_DIR
sudo mksquashfs $ROOT lfs.squashfs
# Copy the squashfs file to the boot directory
cp lfs.squashfs $TEMP_DIR/ISO/boot/
cd $TEMP_DIR/ISO
# Use Xorriso to generate the final iso
xorriso -as mkisofs \
-isohybrid-mbr $TEMP_DIR/ISO/isolinux/isohdpfx.bin \
-c isolinux/boot.cat \
-b isolinux/isolinux.bin \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
-eltorito-alt-boot \
-e boot/grub/efi.img \
-no-emul-boot \
-isohybrid-gpt-basdat \
-o lfs.iso -V lfs \
.
cp lfs.iso $TEMP_DIR
readlink -f $TEMP_DIR/lfs.iso