OSDN Git Service

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