OSDN Git Service

[update] : Enhanced cache.
[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     arch-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     # Check codename
155     if [[ -z $(grep -h -v ^'#' ${channels_dir}/${channel_name}/codename.${arch} | grep -x ${codename}) ]]; then
156         _msg_error "This codename (${channel_name}) is not supported on this channel (${codename})."
157     fi
158
159 }
160
161
162 make_basefs() {
163     local debootstrap_status
164     statusfile="${cache_dir}/${codename}/status"
165
166     debootstrap_status() {
167         if [[ ! -d "$(dirname ${statusfile})" ]]; then
168             mkdir -p "$(dirname ${statusfile})"
169         fi
170         echo "${1}" > "${statusfile}"
171     }
172
173     if [[ -f "${statusfile}" ]] && [[ $(cat "${statusfile}" 2> /dev/null) = "Done" ]]; then
174         _msg_info "${codename} cache is found."
175     else
176         remove "${cache_dir}/${codename}"
177         debootstrap_status "Running"
178         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}/airootfs'..."
179         mkdir -p "${cache_dir}/${codename}/airootfs"
180         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}/airootfs" ${mirror}
181         _msg_info "${codename} installed successfully!"
182         debootstrap_status "Done"
183     fi
184     
185     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
186     _msg_info "copy base files from '${cache_dir}/${codename}/airootfs' to '${work_dir}/airootfs'..."
187     rsync  -au "${cache_dir}/${codename}/airootfs/" "${work_dir}/airootfs"
188     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
189     run_cmd apt-get update
190     # run_cmd apt-get upgrade
191 }
192
193 make_sourcelist() {
194     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
195     run_cmd apt-get update
196
197     if [[ -n $(find ${channels_dir}/*/repo -type f) ]]; then
198         _apt_install gnupg
199
200         for repo in $(find ${channels_dir}/*/repo -name *.list); do
201             key="$(dirname $repo)/$(basename $repo | sed "s/list/key/")"
202             
203             if [[ -f "$key" ]]; then
204                 if $(file $key | grep -q "PGP/GPG key"); then
205                     cp $key ${work_dir}/airootfs/$(basename $key)
206                 else
207                     wget -q -O ${work_dir}/airootfs/$(basename $key) $(cat $key)
208                 fi
209
210                 run_cmd apt-key add $(basename $key)
211                 rm ${work_dir}/airootfs/$(basename $key)
212                 cp $repo ${work_dir}/airootfs/etc/apt/sources.list.d
213             fi
214         done
215
216         run_cmd apt-get update
217     fi
218     
219     # PPA
220     local PPA_FILELIST
221     local _PPA_FILE
222     local _ppa
223
224     PPA_FILELIST=("${channels_dir}/share/ppa_list.${arch}" "${channels_dir}/${channel_name}/ppa_list.${arch}")
225     _apt_install software-properties-common
226     for _PPA_FILE in ${PPA_FILELIST[@]}; do
227         if [[ -f "${_PPA_FILE}" ]]; then
228             for _ppa in $(grep -h -v ^'#' ${_PPA_FILE}); do
229                 run_cmd add-apt-repository --yes "${_ppa}"
230             done
231         fi
232     done
233 }
234
235 make_systemd() {
236     _apt_install systemd-sysv
237     run_cmd dbus-uuidgen > /etc/machine-id
238     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
239 }
240
241 make_packages() {
242     remove "${work_dir}/airootfs/installpkglist"
243
244     if [[ -f "${channels_dir}/share/packages.${arch}" ]]; then
245         grep -h -v ^'#' "${channels_dir}/share/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
246     fi
247     if [[ -f "${channels_dir}/${channel_name}/packages.${arch}" ]]; then
248         grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.${arch}" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
249     fi
250     if [[ -n "$(echo $(<"${work_dir}/airootfs/installpkglist"))" ]]; then
251         run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends --yes install $(echo $(<installpkglist))'
252     fi
253 }
254
255 make_config() {
256     # Locales
257     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'
258
259     # resolvconf
260     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
261
262     # NetworkManager
263     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
264
265     # Timezone
266     #run_cmd echo -ne "UTC" > '/etc/timezone'
267     #run_cmd dpkg-reconfigure -f noninteractive tzdata
268
269     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
270     run_cmd truncate -s 0 /etc/machine-id
271 }
272
273 make_customize_airootfs() {
274     # Overwrite airootfs with customize_airootfs.
275     local copy_airootfs
276
277     copy_airootfs() {
278         local i 
279         for i in "${@}"; do
280             local _dir="${1%/}"
281             if [[ -d "${_dir}" ]]; then
282                 cp -af "${_dir}"/* "${work_dir}/airootfs"
283             fi
284         done
285     }
286
287     copy_airootfs "${channels_dir}/share/airootfs"
288     copy_airootfs "${channels_dir}/${channel_name}/airootfs"
289
290     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
291         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
292         run_cmd "/root/customize_airootfs.sh"
293     fi
294 }
295
296 make_clean() {
297     run_cmd apt-get clean
298     run_cmd apt-get --yes autoremove
299     run_cmd rm -rf "/tmp/* ~/.bash_history"
300 }
301
302 make_bootfiles() {
303     run_cmd update-initramfs -c -k all
304     _apt_install memtest86+
305     [[ -d "${bootfiles_dir}" ]] && rm -r "${bootfiles_dir}"
306     mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
307     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
308     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
309     cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
310
311     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
312         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
313     fi
314
315     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
316 }
317
318 make_grubcfg() {
319     touch "${bootfiles_dir}/ubuntu"
320     cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
321 }
322
323 make_manifest() {
324     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
325     cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
326     sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
327     sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
328     sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
329     sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
330     sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
331 }
332
333 make_squashfs() {
334     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
335     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
336 }
337
338 make_deifnes() {
339     cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
340 }
341
342 make_isolinux() {
343     if [[ "${arch}" = "amd64" ]]; then
344         local _arch="x86_64"
345     else
346         local _arch="${arch}"
347     fi
348     grub-mkstandalone \
349         --format=${_arch}-efi \
350         --output=isolinux/bootx64.efi \
351         --locales="" \
352         --fonts="" \
353         "boot/grub/grub.cfg=isolinux/grub.cfg"
354     (
355         cd isolinux && \
356         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
357         sudo mkfs.vfat efiboot.img && \
358         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
359         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
360     )
361     grub-mkstandalone \
362         --format=i386-pc \
363         --output=isolinux/core.img \
364         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
365         --modules="linux16 linux normal iso9660 biosdisk search" \
366         --locales="" \
367         --fonts="" \
368         "boot/grub/grub.cfg=isolinux/grub.cfg"
369     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
370 }
371
372 make_md5sum() {
373     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
374 }
375
376 make_iso() {
377     xorriso \
378         -as mkisofs \
379         -iso-level 3 \
380         -full-iso9660-filenames \
381         -volid "${iso_label}" \
382         -appid "${iso_application}" \
383         -publisher "${iso_publisher}" \
384         -preparer "prepared by LUBS" \
385         -eltorito-boot boot/grub/bios.img \
386         -no-emul-boot \
387         -boot-load-size 4 \
388         -boot-info-table \
389         --eltorito-catalog boot/grub/boot.cat \
390         --grub2-boot-info \
391         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
392         -eltorito-alt-boot \
393         -e EFI/efiboot.img \
394         -no-emul-boot \
395         -append_partition 2 0xef isolinux/efiboot.img \
396         -output "${out_dir}/${iso_filename}" \
397         -graft-points \
398             "." \
399             /boot/grub/bios.img=isolinux/bios.img \
400             /EFI/efiboot.img=isolinux/efiboot.img
401 }
402
403 make_checksum() {
404     cd ${out_dir}
405     _msg_info "Creating md5 checksum ..."
406     md5sum "${iso_filename}" > "${iso_filename}.md5"
407
408     _msg_info "Creating sha256 checksum ..."
409     sha256sum "${iso_filename}" > "${iso_filename}.sha256"
410     cd - > /dev/null 2>&1
411 }
412
413
414 # 引数解析()
415 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
416
417 _opt_short="w:o:ha:-:m:c:d"
418 _opt_long="help,arch:,codename:,debug,help,mirror:,out:,work"
419 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
420 if [[ ${?} != 0 ]]; then
421     exit 1
422 fi
423 eval set -- "${OPT}"
424 while :; do
425     case ${1} in
426         -a | --arch)
427             if [[ -z ${2} ]]; then
428                 _msg_error "Please specify the architecture."
429                 exit 1
430             else
431                 arch="${2}"
432             fi
433             shift 2
434             ;;
435         -c | --codename)
436             if [[ -z ${2} ]]; then
437                 _msg_error "Please specify the codename."
438                 exit 1
439             else
440                 codename="${2}"
441             fi
442             shift 2
443             ;;
444         -d | --debug)
445             debug=true
446             shift 1
447             ;;
448         -h | --help)
449             _usage
450             exit 0
451             ;;
452         -m | --mirror)
453             if [[ -z ${2} ]]; then
454                 _msg_error "Please specify the mirror server."
455                 exit 1
456             else
457                 mirror="${2}"
458             fi
459             shift 2
460             ;;
461         -o | --out)
462             if [[ -z ${2} ]]; then
463                 _msg_error "Please specify the out dir."
464                 exit 1
465             else
466                 out_dir="${2}"
467             fi
468             shift 2
469             ;;
470         -w | --work)
471             if [[ -z ${2} ]]; then
472                 _msg_error "Please specify the out dir."
473                 exit 1
474             else
475                 work_dir="${2}"
476             fi
477             shift 2
478             ;;
479         --)
480             shift
481             break
482             ;;
483         *)
484             _msg_error "Invalid argument '${1}'"
485             _usage 1
486             ;;
487     esac
488 done
489
490 bootfiles_dir="${work_dir}/bootfiles"
491
492
493 prepare_build
494 run_once make_basefs
495 run_once make_sourcelist
496 run_once make_systemd
497 run_once make_packages
498 run_once make_config
499 run_once make_customize_airootfs
500 run_once make_clean
501 run_once make_bootfiles
502 run_once make_grubcfg
503 run_once make_manifest
504 run_once make_squashfs
505 run_once make_deifnes
506 run_once run_bootfiles make_isolinux
507 run_once run_bootfiles make_md5sum
508 run_once run_bootfiles make_iso
509 run_once make_checksum