0%

使用 bash 脚本创建自定义的 Debian Live ISO

项目地址

操作环境

  • Debian 10 Buster

自定义

运行方式

  • nano build.sh #把下列脚本的内容复制到该脚本
  • chmod u+x build.sh
  • ./build.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
#!/bin/bash
#
# Debian Live Image Custom
#
# Author: Mathias Fredriksson

set -e

IN_CHROOT=0
if [[ $1 == chroot ]]; then
IN_CHROOT=1
shift
fi

SCRIPT="$0"
MIRROR="http://mirrors.ustc.edu.cn/debian/"
#WORK=$(mktemp -d -t live-image.XXXX)
WORK=$HOME/build
CHROOT="$WORK/chroot"
mkdir -p "$WORK"

prepare() {
apt-get -y install \
debootstrap \
squashfs-tools \
xorriso \
grub-pc-bin \
grub-efi-amd64-bin \
mtools

debootstrap \
--arch=amd64 \
--variant=minbase \
stretch \
"$CHROOT" \
$MIRROR
}

cont_in_chroot() {
local name

name="$(basename "$SCRIPT")"
cp -a "$SCRIPT" "$CHROOT"

mount --rbind /dev "$CHROOT"/dev
mount --make-rslave "$CHROOT"/dev
mount --rbind /proc "$CHROOT"/proc
mount --make-rslave "$CHROOT"/proc
mount --rbind /sys "$CHROOT"/sys
mount --make-rslave "$CHROOT"/sys

chroot "$CHROOT" /bin/bash ./"$name" chroot "$@"

# Try to unmount a few times since this doesn't alway work on the first try...
grep "$CHROOT" /proc/mounts | cut -f2 -d" " | sort -r | xargs umount -n || \
grep "$CHROOT" /proc/mounts | cut -f2 -d" " | sort -r | xargs umount -n || \
grep "$CHROOT" /proc/mounts | cut -f2 -d" " | sort -r | xargs umount -n

rm "$CHROOT/$name"
}

chroot_prepare() {
echo live > /etc/hostname

# Set root password to 'live'.
echo $'toor\ntoor' | passwd root

apt-get update
apt-get -y install --no-install-recommends \
linux-image-amd64 \
live-boot \
systemd-sysv
}

chroot_install_tools() {
cat <<-EOS > /etc/apt/sources.list.d/contrib-non-free.list
deb http://mirrors.ustc.edu.cn/debian/ stretch main contrib non-free
EOS

apt update
apt-get -y install --no-install-recommends \
grub-pc-bin \
grub-efi-amd64-bin \
sudo \
curl \
hdparm \
sdparm \
pciutils \
usbutils \
dnsutils \
moreutils \
net-tools \
dosfstools \
openssh-server \
openssh-client \
nano \
vim \
zsh \
lsof \
lshw \
less \
rsync \
smartmontools \
intel-microcode \
cryptsetup \
lvm2 \
mdadm

chsh -s /bin/zsh
}

chroot_install_zfs() {
cat <<-EOS > /etc/apt/sources.list.d/zfs.list
deb http://mirrors.163.com/debian/ stretch-backports main contrib non-free
EOS

apt-get update
apt-get -y install dpkg-dev linux-headers-amd64
apt-get -y install -t stretch-backports zfs-dkms
}

chroot_finalize() {
apt-get clean
}

finalize() {
mkdir -p "$WORK"/{scratch,image/live}

mksquashfs \
"$CHROOT" \
"$WORK"/image/live/filesystem.squashfs \
-e boot

cp "$CHROOT"/boot/vmlinuz-* "$CHROOT"/boot/initrd.img-* \
"$WORK"/image/live

cat <<EOF > "$WORK"/scratch/grub.cfg
insmod all_video
insmod play
play 960 440 1 0 4 440 1
if [ \${iso_path} ] ; then
set loopback="findiso=\${iso_path}"
fi
search --set=root --file /DEBIAN_LIVE
set default="0"
set timeout=5
EOF

(cd "$CHROOT"/boot || exit 1
for kernel in vmlinuz-*; do
version=${kernel#vmlinuz-}
cat <<EOF >> "$WORK"/scratch/grub.cfg
menuentry "Debian GNU/Linux Live (kernel $version)" {
linux /live/vmlinuz-$version boot=live components "\${loopback}"
initrd /live/initrd.img-$version
}
EOF
done
)

# Create file used by grub to search for boot device.
touch "$WORK"/image/DEBIAN_LIVE
}

create_iso() {
grub-mkstandalone \
--format=x86_64-efi \
--output="$WORK"/scratch/bootx64.efi \
--locales="" \
--fonts="" \
"boot/grub/grub.cfg=$WORK/scratch/grub.cfg"

(cd "$WORK"/scratch
dd if=/dev/zero of=efiboot.img bs=1M count=10
mkfs.vfat efiboot.img
mmd -i efiboot.img efi efi/boot
mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
)

grub-mkstandalone \
--format=i386-pc \
--output="$WORK"/scratch/core.img \
--install-modules="linux normal iso9660 biosdisk memdisk search tar ls" \
--modules="linux normal iso9660 biosdisk search" \
--locales="" \
--fonts="" \
"boot/grub/grub.cfg=$WORK/scratch/grub.cfg"

cat /usr/lib/grub/i386-pc/cdboot.img "$WORK"/scratch/core.img \
> "$WORK"/scratch/bios.img

xorriso \
-as mkisofs \
-iso-level 3 \
-full-iso9660-filenames \
-volid "DEBIAN_LIVE" \
-eltorito-boot boot/grub/bios.img \
-no-emul-boot \
-boot-load-size 4 \
-boot-info-table \
--eltorito-catalog boot/grub/boot.cat \
--grub2-boot-info \
--grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
-eltorito-alt-boot \
-e EFI/efiboot.img \
-no-emul-boot \
-append_partition 2 0xef "$WORK"/scratch/efiboot.img \
-output "$WORK/ling218_linux.iso" \
-graft-points \
"$WORK/image" \
/boot/grub/bios.img="$WORK"/scratch/bios.img \
/EFI/efiboot.img="$WORK"/scratch/efiboot.img
}

if (( IN_CHROOT )); then
chroot_prepare
chroot_install_tools
chroot_install_zfs
chroot_finalize
exit 0
fi

prepare
cont_in_chroot "$@"
finalize
create_iso
  • 本文标题:使用 bash 脚本创建自定义的 Debian Live ISO
  • 本文作者: LinG218
  • 本文链接: https://www.ling218.cn/archives/e7b274ff.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!