-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkdebiso
executable file
·341 lines (291 loc) · 8.66 KB
/
mkdebiso
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
#!/bin/bash
# DebISO - Debian/Ubuntu Remix Creator
# License: 3-clause BSD
# Copyright (c) 2023 Radio New Japan Broadcasting Club
set -e
set -o pipefail
set +u
export LC_ALL=C
DEBISO_VERSION="1.2"
_prepare(){
mkdir -p ${WORKDIR}/chroot
debootstrap --arch=${ARCH} --variant=minbase ${UPSTREAM_VERSION} ${WORKDIR}/chroot ${MIRROR_URL}
cp -pr ${PROFDIR}/dirootfs/* ${WORKDIR}/chroot/
cp -p ${PROFDIR}/packages.${ARCH} ${WORKDIR}/
sed "/^\#/d" -i ${WORKDIR}/packages.${ARCH}
cp -pr ${PROFDIR}/grub ${WORKDIR}/
cp -pr ${PROFDIR}/sources.list ${WORKDIR}/chroot/etc/apt/
if [ -d ${PROFDIR}/calamares_config.d ]; then
cp -pr ${PROFDIR}/calamares_config.d ${WORKDIR}/chroot/etc/calamares
fi
if [ -d ${PROFDIR}/sources.list.d ]; then
cp -pr ${PROFDIR}/sources.list.d/* ${WORKDIR}/chroot/etc/apt/sources.list.d/
fi
if [ -d ${PROFDIR}/trusted.gpg.d ]; then
cp -pr ${PROFDIR}/trusted.gpg.d/* ${WORKDIR}/chroot/etc/apt/trusted.gpg.d/
fi
if [ -f ${PROFDIR}/exclude_packages.${ARCH} ]; then
cp -p ${PROFDIR}/exclude_packages.${ARCH} ${WORKDIR}/
sed /^\#/d -i ${WORKDIR}/exclude_packages.${ARCH}
fi
if [ -f ${PROFDIR}/flatpak_packages.${ARCH} ]; then
cp -p ${PROFDIR}/flatpak_packages.${ARCH} ${WORKDIR}/
sed /^\#/d -i ${WORKDIR}/flatpak_packages.${ARCH}
fi
if [ -f ${PROFDIR}/ppa.${ARCH} ]; then
cp -p ${PROFDIR}/ppa.${ARCH} ${WORKDIR}/
sed "/^\#/d" -i ${WORKDIR}/ppa.${ARCH}
fi
if [ -d ${WORKDIR}/chroot/root/customise_dirootfs.d/ ]; then
chmod 755 ${WORKDIR}/chroot/root/customise_dirootfs.d/*.sh
fi
sed "s/%DISTRO_NAME%/${DISTRO_NAME}/g" -i ${WORKDIR}/grub/grub.cfg
sed "s/%DISTRO_UNAME%/${DISTRO_UNAME}/g" -i ${WORKDIR}/grub/grub.cfg
# chroot
cd ${WORKDIR}
mount --bind /dev chroot/dev
mount --bind /run chroot/run
chroot chroot mount none -t proc /proc
chroot chroot mount none -t sysfs /sys
chroot chroot mount none -t devpts /dev/pts
}
_setup(){
chmod 700 chroot/root
chown root:root chroot/root
if [ -f ppa.${ARCH} ]; then
chroot chroot apt-get install -y software-properties-common
PPALIST=($(cat ppa.${ARCH}))
for ((i=0; i < ${#PPALIST[*]}; i++)); do
chroot chroot add-apt-repository -y ppa:${PPALIST[i]}
done
fi
if [ -f chroot/root/customise_dirootfs.d/preinstall.sh ]; then
chroot chroot /root/customise_dirootfs.d/preinstall.sh
fi
chroot chroot apt-get update
chroot chroot apt-get upgrade -y
chroot chroot apt-get install -y $(cat packages.${ARCH})
if [ -f chroot/root/customise_dirootfs.d/postinstall.sh ]; then
chroot chroot /root/customise_dirootfs.d/postinstall.sh
rm -r chroot/root/customise_dirootfs.d
fi
if [ -f exclude_packages.${ARCH} ]; then
chroot chroot apt-get purge -y --autoremove $(cat exclude_packages.${ARCH})
fi
# flatpak's pkgs
if [ -f flatpak_packages.${ARCH} ]; then
chroot chroot apt-get install -y flatpak
chroot chroot flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
chroot chroot flatpak update
chroot chroot flatpak install -y flathub $(cat flatpak_packages.${ARCH})
fi
_PATH="/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/usr/sbin"
PATH=${_PATH} chroot chroot /usr/sbin/dpkg-reconfigure locales
PATH=${_PATH} chroot chroot /usr/sbin/dpkg-reconfigure resolvconf
cat <<EOF > chroot/etc/NetworkManager/NetworkManager.conf
[main]
rc-manager=resolvconf
plugins=ifupdown,keyfile
dns=dnsmasq
[ifupdown]
managed=false
EOF
PATH=${_PATH} chroot chroot /usr/sbin/dpkg-reconfigure network-manager
chroot chroot apt-get clean -y
}
_build_iso(){
umount -l chroot/proc
umount -l chroot/sys
umount -l chroot/dev/pts
umount -l chroot/dev
umount -l chroot/run
if [[ ${UPSTREAM} == "debian" ]]; then
LIVEDIR=live
elif [[ ${UPSTREAM} == "ubuntu" ]]; then
LIVEDIR=casper
fi
mkdir iso
mkdir -p iso/{${LIVEDIR},isolinux,install}
# copy memtest86
if [[ ${UPSTREAM} == "debian" ]]; then
cp chroot/boot/memtest86+x64.bin iso/install/memtest86+
elif [[ ${UPSTREAM} == "ubuntu" ]]; then
cp chroot/boot/memtest86+.bin iso/install/memtest86+
fi
wget https://www.memtest86.com/downloads/memtest86-usb.zip -O iso/install/memtest86-usb.zip
unzip -p iso/install/memtest86-usb.zip memtest86-usb.img > iso/install/memtest86
rm -f iso/install/memtest86-usb.zip
# copy kernel
if [[ ${UPSTREAM} == "debian" ]]; then
cp chroot/boot/vmlinuz-**-**-${ARCH} iso/live/vmlinuz
cp chroot/boot/initrd.img-**-**-${ARCH} iso/live/initrd
elif [[ ${UPSTREAM} == "ubuntu" ]]; then
cp chroot/boot/vmlinuz-**-**-generic iso/casper/vmlinuz
cp chroot/boot/initrd.img-**-**-generic iso/casper/initrd
fi
# grub
touch iso/${DISTRO_UNAME}
cp grub/grub.cfg iso/isolinux/grub.cfg
# package list
chroot chroot dpkg-query -W --showformat='${Package} ${Version}\n' > iso/${LIVEDIR}/filesystem.packages
# compress rootfs
mksquashfs chroot iso/${LIVEDIR}/filesystem.squashfs \
-noappend -no-duplicates -no-recovery \
-wildcards \
-e "var/cache/apt/archives/*" \
-e "root/*" \
-e "root/.*" \
-e "tmp/*" \
-e "tmp/.*" \
-e "swapfile"
if [[ ${UPSTREAM} == "ubuntu" ]]; then
# create diskdefines
cat <<EOF > iso/README.diskdefines
#define DISKNAME ${DISTRO_UNAME}
#define TYPE binary
#define TYPEbinary 1
#define ARCH amd64
#define ARCHamd64 1
#define DISKNUM 1
#define DISKNUM1 1
#define TOTALNUM 0
#define TOTALNUM0 1
EOF
# create disk info
mkdir iso/.disk
touch iso/.disk/base_installable
cat <<EOF > iso/.disk/cd_type
full_cd/single
${DISTRO_NAME} ${DISTRO_VERSION} - Release amd64 ($(date +%Y%m%d))"
EOF
fi
# generate grub image
pushd iso
grub-mkstandalone \
--format=x86_64-efi \
--output=isolinux/bootx64.efi \
--locales="" \
--fonts="" \
"boot/grub/grub.cfg=isolinux/grub.cfg"
(
cd isolinux && \
dd if=/dev/zero of=efiboot.img bs=1M count=20 && \
mkfs.vfat efiboot.img && \
LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
)
grub-mkstandalone \
--format=i386-pc \
--output=isolinux/core.img \
--install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
--modules="linux16 linux normal iso9660 biosdisk search" \
--locales="" \
--fonts="" \
--themes="" \
"boot/grub/grub.cfg=isolinux/grub.cfg"
cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
/bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v -e 'md5sum.txt' -e 'bios.img' -e 'efiboot.img' > md5sum.txt)"
# build iso
mkdir ../../${OUTDIR}
xorriso \
-as mkisofs \
-iso-level 3 \
-full-iso9660-filenames \
-volid "${DISTRO_UNAME}" \
-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 isolinux/efiboot.img \
-output "../../${OUTDIR}/${DISTRO_UNAME}-${DISTRO_VERSION}-${ARCH}.iso" \
-m "isolinux/efiboot.img" \
-m "isolinux/bios.img" \
-graft-points \
"/EFI/efiboot.img=isolinux/efiboot.img" \
"/boot/grub/bios.img=isolinux/bios.img" \
"."
popd
}
_usage(){
echo "usage: mkdebiso [options] -p <profile directory>"
echo
echo "Options:"
echo " -w <work_dir> Set the working directory"
echo " default: work"
echo " -o <out_dir> Set the output directory"
echo " default: out"
echo " -p <prof_dir> Set the profile directory"
echo " -i Enable 32-bit UEFI support"
echo
echo " -h Show this help"
echo " -v Show version info"
}
_version(){
echo "DebISO ${DEBISO_VERSION} - Debian/Ubuntu Remix Creator"
echo
echo "Copyright (c) 2023 Radio New Japan All Rights Reserved."
echo "This software is licensed under 3-clause BSD license."
}
_main(){
if [ ! -f ${PROFDIR}/profiledef.sh ]; then
echo "ERROR: No profile specified!"
_usage
exit 1
fi
if [[ $(whoami) != "root" ]]; then
echo "ERROR: Need to run as root!"
exit 1
fi
source "${PROFDIR}/profiledef.sh"
_prepare
_setup
_build_iso
export LC_ALL=$(printenv LANG)
exit 0
}
DISTRO_NAME=""
DISTRO_UNAME=""
DISTRO_VERSION=""
UPSTREAM=""
UPSTREAM_VERSION=""
MIRROR_URL=""
ARCH=""
WORKDIR="work"
OUTDIR="out"
PROFDIR=""
while getopts 'w:o:p:ihv' arg; do
case "${arg}" in
h)
_usage
exit 0
;;
o)
OUTDIR="${2}"
shift 2
;;
p)
PROFDIR="${2}"
shift 2
;;
v)
_version
exit 0
;;
w)
WORKDIR="${2}"
shift 2
;;
*)
echo "ERROR: Invalid argument '${1}'!"
_usage
exit 1
;;
esac
done
_main