OSDN Git Service

[update] : Limited PPA files.
[alterlinux/LUBS.git] / lubs
1 #!/usr/bin/env bash
2
3 set -e
4 # set -u
5
6 export LANG=C
7
8 script_path=$(readlink -f ${0%/*})
9
10 cache_dir="${script_path}/cache"
11 channels_dir="${script_path}/channels"
12 isolinux_dir="${script_path}/isolinux"
13
14 codename="focal"
15 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
16 os_name="Ubuntu"
17 iso_name="ubuntu"
18
19 arch=amd64
20 work_dir="${script_path}/work"
21 out_dir="${script_path}/out"
22 iso_label="${os_name}_${codename}_${arch}"
23 iso_publisher='Fascode Network <https://fascode.net>'
24 iso_application="${os_name} Live/Rescue CD"
25 iso_version="${codename}-$(date +%Y.%m.%d)"
26 iso_filename="${iso_name}-${iso_version}-${arch}.iso"
27
28 channel_name="serene"
29
30 debug=false
31
32
33 # Show an INFO message
34 # _msg_info <message>
35 _msg_info() {
36     local _msg="${@}"
37     "${script_path}/echo_color"  -t 36 "[LUBS Core]" /! -t 32 " INFO:" "/#" -t 0 "${_msg}"
38 }
39
40 # Show an debug message
41 # _msg_debug <message>
42 _msg_debug() {
43     if [[ "${debug}" = true ]]; then
44         local _msg="${@}"
45         "${script_path}/echo_color"  -t 36 "[LUBS Core]" /! -t 35 "Debug:" "/#" -t 0 "${_msg}"
46     fi
47 }
48
49 # Show an ERROR message then exit with status
50 # _msg_error <message> <exit code>
51 _msg_error() {
52     local _msg="${1}"
53     local _error=${2}
54     "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 31 "ERROR:" "/#" -t 0 "${_msg}" >&2
55     if [[ ! ${_error} = 0 ]]; then
56         exit ${_error}
57     fi
58 }
59
60 # Helper function to run make_*() only one time.
61 run_once() {
62     local name
63
64     if [[ "run_bootfiles" == "$1" ]]; then
65         name="$2"
66     else
67         name="$1"
68     fi
69
70     if [[ ! -e "${work_dir}/build.${name}" ]]; then
71         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
72
73         if [[ "run_bootfiles" == "$1" ]]; then
74             "$1" "$2"
75         else
76             "$1"
77         fi
78
79         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
80         touch "${work_dir}/build.${name}"
81     fi
82 }
83
84 run_cmd() {
85     "${script_path}/lubs-chroot" "${work_dir}/airootfs" "${@}"
86 }
87
88 run_bootfiles() {
89     cd "${bootfiles_dir}"
90     "$1"
91     cd - > /dev/null
92 }
93
94 _apt_install() {
95     run_cmd apt-get --no-install-recommends --yes install ${@}
96 }
97
98 # rm helper
99 # Delete the file if it exists.
100 # For directories, rm -rf is used.
101 # If the file does not exist, skip it.
102 # remove <file> <file> ...
103 remove() {
104     local _list
105     local _file
106     _list=($(echo "$@"))
107     for _file in "${_list[@]}"; do
108         _msg_debug "Removeing ${_file}"
109         if [[ -f ${_file} ]]; then
110             rm -f "${_file}"
111         elif [[ -d ${_file} ]]; then
112             rm -rf "${_file}"
113         fi
114     done
115 }
116
117 # Show help
118 _usage () {
119     echo "usage ${0} [options] [channel]"
120     echo
121     echo " General options:"
122     echo
123     echo "    -a | --arch <str>      Set architecture"
124     echo "                           Default: ${arch}"
125     echo "    -c | --codename <str>  Set ubuntu codename"
126     echo "                           Default: ${codename}"
127     echo "    -m | --mirror <url>    Set apt mirror server."
128     echo "                           Default: ${mirror}"
129     echo "    -o | --out <out_dir>   Set the output directory"
130     echo "                           Default: ${out_dir}"
131     echo "    -w | --work <work_dir> Set the working directory"
132     echo "                           Default: ${work_dir}"
133     echo
134     echo "    -d | --debug           "
135     echo "    -h | --help            This help message and exit"
136     echo
137 }
138
139
140 prepare_build() {
141     if [[ ${EUID} -ne 0 ]]; then
142         _msg_error "This script must be run as root." 1
143     fi
144     
145     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
146     [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
147
148     local mount
149     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
150         _msg_info "Unmounting ${mount}"
151         umount "${mount}"
152     done
153
154 }
155
156
157 make_basefs() {
158     if [[ ! -d "${cache_dir}/${codename}" ]]; then
159         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
160         mkdir -p "${cache_dir}/${codename}"
161         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
162         _msg_info "${codename} installed successfully!"
163     else
164         _msg_info "${codename} cache is found."
165     fi
166     
167     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
168     _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
169     rsync  -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
170     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
171     run_cmd apt-get update
172     # run_cmd apt-get upgrade
173 }
174
175 make_sourcelist() {
176     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
177     run_cmd apt-get update
178
179     if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
180         _apt_install gnupg
181
182         for repo in $(find ${channels_dir}/*/repo -name *.list); do
183             key="$(dirname $repo)/$(basename $repo | sed "s/list/key/")"
184             
185             if [[ -f "$key" ]]; then
186                 if $(file $key | grep -q "PGP/GPG key"); then
187                     cp $key ${work_dir}/airootfs/$(basename $key)
188                 else
189                     wget -q -O ${work_dir}/airootfs/$(basename $key) $(cat $key)
190                 fi
191
192                 run_cmd apt-key add $(basename $key)
193                 rm ${work_dir}/airootfs/$(basename $key)
194                 cp $repo ${work_dir}/airootfs/etc/apt/sources.list.d
195             fi
196         done
197
198         run_cmd apt-get update
199     fi
200     
201     # PPA
202     local PPA_FILELIST
203     local _PPA_FILE
204     local _ppa
205
206     PPA_FILELIST=("${channels_dir}/${channel_name}/ppa_list.${arch}"
207     _apt_install software-properties-common
208     for _PPA_FILE in ${PPA_FILELIST[@]}; do
209         for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
210             run_cmd add-apt-repository --yes "${_ppa}"
211         done
212     done
213 }
214
215 make_systemd() {
216     _apt_install systemd-sysv
217     run_cmd dbus-uuidgen > /etc/machine-id
218     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
219 }
220
221 make_packages() {
222     remove "${work_dir}/airootfs/installpkglist"
223
224     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
225         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
226     fi
227     if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
228         grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
229     fi
230     if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
231         run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
232     fi
233 }
234
235 make_config() {
236     # Locales
237     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive LANG=en_US.UTF-8 LC_ALL=C LANGUAGE=en_US.UTF-8 dpkg-reconfigure locales'
238
239     # resolvconf
240     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
241
242     # NetworkManager
243     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
244
245     # Timezone
246     #run_cmd echo -ne "UTC" > '/etc/timezone'
247     #run_cmd dpkg-reconfigure -f noninteractive tzdata
248
249     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
250     run_cmd truncate -s 0 /etc/machine-id
251 }
252
253 make_customize_airootfs() {
254     # Overwrite airootfs with customize_airootfs.
255     local copy_airootfs
256
257     copy_airootfs() {
258         local i 
259         for i in "${@}"; do
260             local _dir="${1%/}"
261             if [[ -d "${_dir}" ]]; then
262                 cp -af "${_dir}"/* "${work_dir}/airootfs"
263             fi
264         done
265     }
266
267     copy_airootfs "${channels_dir}/share/airootfs"
268     copy_airootfs "${channels_dir}/${channel_name}/airootfs"
269
270     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
271         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
272         run_cmd "/root/customize_airootfs.sh"
273     fi
274 }
275
276 make_clean() {
277     run_cmd apt-get clean
278     run_cmd apt-get --yes autoremove
279     run_cmd rm -rf "/tmp/* ~/.bash_history"
280 }
281
282 make_bootfiles() {
283     run_cmd update-initramfs -c -k all
284     _apt_install memtest86+
285     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
286     mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
287     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
288     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
289     cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
290
291     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
292         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
293     fi
294
295     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
296 }
297
298 make_grubcfg() {
299     touch "${bootfiles_dir}/ubuntu"
300     cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
301 }
302
303 make_manifest() {
304     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
305     cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
306     sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
307     sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
308     sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
309     sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
310     sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
311 }
312
313 make_squashfs() {
314     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
315     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
316 }
317
318 make_deifnes() {
319     cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
320 }
321
322 make_isolinux() {
323     if [[ "${arch}" = "amd64" ]]; then
324         local _arch="x86_64"
325     else
326         local _arch="${arch}"
327     fi
328     grub-mkstandalone \
329         --format=${_arch}-efi \
330         --output=isolinux/bootx64.efi \
331         --locales="" \
332         --fonts="" \
333         "boot/grub/grub.cfg=isolinux/grub.cfg"
334     (
335         cd isolinux && \
336         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
337         sudo mkfs.vfat efiboot.img && \
338         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
339         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
340     )
341     grub-mkstandalone \
342         --format=i386-pc \
343         --output=isolinux/core.img \
344         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
345         --modules="linux16 linux normal iso9660 biosdisk search" \
346         --locales="" \
347         --fonts="" \
348         "boot/grub/grub.cfg=isolinux/grub.cfg"
349     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
350 }
351
352 make_md5sum() {
353     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
354 }
355
356 make_iso() {
357     xorriso \
358         -as mkisofs \
359         -iso-level 3 \
360         -full-iso9660-filenames \
361         -volid "${iso_label}" \
362         -appid "${iso_application}" \
363         -publisher "${iso_publisher}" \
364         -preparer "prepared by LUBS" \
365         -eltorito-boot boot/grub/bios.img \
366         -no-emul-boot \
367         -boot-load-size 4 \
368         -boot-info-table \
369         --eltorito-catalog boot/grub/boot.cat \
370         --grub2-boot-info \
371         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
372         -eltorito-alt-boot \
373         -e EFI/efiboot.img \
374         -no-emul-boot \
375         -append_partition 2 0xef isolinux/efiboot.img \
376         -output "${out_dir}/${iso_filename}" \
377         -graft-points \
378             "." \
379             /boot/grub/bios.img=isolinux/bios.img \
380             /EFI/efiboot.img=isolinux/efiboot.img
381 }
382
383 make_checksum() {
384     cd ${out_dir}
385     _msg_info "Creating md5 checksum ..."
386     md5sum "${iso_filename}" > "${iso_filename}.md5"
387
388     _msg_info "Creating sha256 checksum ..."
389     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
390     cd - > /dev/null 2>&1
391 }
392
393
394 # 引数解析()
395 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
396
397 _opt_short="w:o:ha:-:m:c:d"
398 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
399 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
400 if [[ ${?} != 0 ]]; then
401     exit 1
402 fi
403 eval set -- "${OPT}"
404 while :; do
405     case ${1} in
406         -a | --arch)
407             if [[ -z ${2} ]]; then
408                 _msg_error "Please specify the architecture."
409                 exit 1
410             else
411                 arch="${2}"
412             fi
413             shift 2
414             ;;
415         -c | --codename)
416             if [[ -z ${2} ]]; then
417                 _msg_error "Please specify the codename."
418                 exit 1
419             else
420                 codename="${2}"
421             fi
422             shift 2
423             ;;
424         -d | --debug)
425             debug=true
426             shift 1
427             ;;
428         -h | --help)
429             _usage
430             exit 0
431             ;;
432         -m | --mirror)
433             if [[ -z ${2} ]]; then
434                 _msg_error "Please specify the mirror server."
435                 exit 1
436             else
437                 mirror="${2}"
438             fi
439             shift 2
440             ;;
441         -o | --out)
442             if [[ -z ${2} ]]; then
443                 _msg_error "Please specify the out dir."
444                 exit 1
445             else
446                 out_dir="${2}"
447             fi
448             shift 2
449             ;;
450         -w | --work)
451             if [[ -z ${2} ]]; then
452                 _msg_error "Please specify the out dir."
453                 exit 1
454             else
455                 work_dir="${2}"
456             fi
457             shift 2
458             ;;
459         --)
460             shift
461             break
462             ;;
463         *)
464             _msg_error "Invalid argument '${1}'"
465             _usage 1
466             ;;
467     esac
468 done
469
470 bootfiles_dir="${work_dir}/bootfiles"
471
472
473 prepare_build
474 run_once make_basefs
475 run_once make_sourcelist
476 run_once make_systemd
477 run_once make_packages
478 run_once make_config
479 run_once make_customize_airootfs
480 run_once make_clean
481 run_once make_bootfiles
482 run_once make_grubcfg
483 run_once make_manifest
484 run_once make_squashfs
485 run_once make_deifnes
486 run_once run_bootfiles make_isolinux
487 run_once run_bootfiles make_md5sum
488 run_once run_bootfiles make_iso
489 run_once make_checksum