OSDN Git Service

[merge] : merge start message and end message
[alterlinux/LUBS.git] / lubs
1 #!/usr/bin/env bash
2
3 set -e -u
4
5 export LANG=C
6
7
8 arch=amd64
9 script_path=$(readlink -f ${0%/*})
10 work_dir="${script_path}work"
11 cache_dir="${work_dir}/cache"
12 out_dir="${work_dir}/out"
13 channels_dir="${script_path}/channels"
14 isolinux_dir="${script_path}/isolinux"
15 codename="focal"
16 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
17
18 os_name="Ubuntu"
19 iso_name="ubuntu"
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
27 # Show an INFO message
28 # _msg_info <message>
29 _msg_info () {
30     local _msg="${@}"
31     "${script_path}/echo_color"  -t 36 "[LUBS Core]" /! -t 32 "INFO:" "/#${_msg}"
32 }
33
34 # Show an ERROR message then exit with status
35 # _msg_error <message> <exit code>
36 _msg_error() {
37     local _msg="${1}"
38     local _error=${2}
39     "${script_path}/echo_color" -t 36 "[LUBS Core]" /! -t 31 "ERROR:" "/#${_msg}" >&2
40     if [[ ! ${_error} = 0 ]]; then
41         exit ${_error}
42     fi
43 }
44
45 # Helper function to run make_*() only one time.
46 run_once() {
47     local name
48
49     if [[ "run_out" == "$1" ]]; then
50         name="$2"
51     else
52         name="$1"
53     fi
54
55     if [[ ! -e "${work_dir}/build.${name}" ]]; then
56         _msg_info "$(echo $name | sed "s@_@ @g") is starting."
57
58         if [[ "run_out" == "$1" ]]; then
59             "$1" "$2"
60         else
61             "$1"
62         fi
63
64         _msg_info "$(echo $name | sed "s@_@ @g") was done!"
65         touch "${work_dir}/build.${name}"
66     fi
67 }
68
69 run_cmd () {
70     "${script_path}/lubs-chroot" "${work_dir}/airootfs" ${@}
71 }
72
73 run_out () {
74     cd "${out_dir}"
75     "$1"
76     cd - > /dev/null
77 }
78
79 _apt_install () {
80     run_cmd apt-get --yes install ${@}
81 }
82
83
84
85
86 prepare_build () {
87     if [[ ${EUID} -ne 0 ]]; then
88         _msg_error "This script must be run as root." 1
89     fi
90     
91     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
92
93     local mount
94     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
95         _msg_info "Unmounting ${mount}"
96         umount "${mount}"
97     done
98
99 }
100
101
102 make_basefs () {
103     if [[ ! -d "${cache_dir}/${codename}" ]]; then
104         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
105         mkdir -p "${cache_dir}/${codename}"
106         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
107         _msg_info "${codename} installed successfully!"
108     else
109         _msg_info "${codename} cache is found."
110     fi
111     
112     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
113     _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
114     rsync  -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
115     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
116     run_cmd apt-get update
117     # run_cmd apt-get upgrade
118 }
119
120 make_sourcelist () {
121     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
122 }
123
124 make_systemd() {
125     _apt_install systemd-sysv
126     run_cmd dbus-uuidgen > /etc/machine-id
127     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
128 }
129
130 make_packages () {
131     run_cmd apt-get update
132     installpkglist=($(grep -h -v ^'#' ${script_path}/packages.x86_64))
133     run_cmd sudo -i apt-get --yes install ${installpkglist[@]}
134 }
135
136 make_config() {
137     # Locales
138     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'
139
140     # resolvconf
141     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
142
143     # NetworkManager
144     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
145
146     # Timezone
147     #run_cmd echo -ne "UTC" > '/etc/timezone'
148     #run_cmd dpkg-reconfigure -f noninteractive tzdata
149
150     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
151     run_cmd truncate -s 0 /etc/machine-id
152 }
153
154 make_customize_airootfs() {
155     # Overwrite airootfs with customize_airootfs.
156     cp -af "${channels_dir}/share/airootfs" "${work_dir}"
157
158     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
159         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
160         run_cmd "/root/customize_airootfs.sh"
161     fi
162 }
163
164 make_clean() {
165     run_cmd apt-get clean
166     run_cmd apt-get --yes autoremove
167     run_cmd rm -rf "/tmp/* ~/.bash_history"
168 }
169
170 make_bootfiles() {
171     run_cmd update-initramfs -c -k all
172     _apt_install memtest86+
173     mkdir -p ${out_dir}/{casper,isolinux,install}
174     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${out_dir}/casper/vmlinuz
175     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${out_dir}/casper/initrd
176     cp ${work_dir}/airootfs/boot/memtest86+.bin ${out_dir}/install/memtest86+
177
178     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
179         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
180     fi
181
182     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${out_dir}/install/memtest86)
183 }
184
185 make_grubcfg() {
186     touch "${out_dir}/ubuntu"
187     cp ${isolinux_dir}/grub.cfg ${out_dir}/isolinux/grub.cfg
188 }
189
190 make_manifest() {
191     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${out_dir}/casper/filesystem.manifest
192     cp -v ${out_dir}/casper/filesystem.manifest "${out_dir}/casper/filesystem.manifest-desktop"
193     sed -i '/ubiquity/d' "${out_dir}/casper/filesystem.manifest-desktop"
194     sed -i '/casper/d' "${out_dir}/casper/filesystem.manifest-desktop"
195     sed -i '/discover/d' "${out_dir}/casper/filesystem.manifest-desktop"
196     sed -i '/laptop-detect/d' "${out_dir}/casper/filesystem.manifest-desktop"
197     sed -i '/os-prober/d' "${out_dir}/casper/filesystem.manifest-desktop"
198 }
199
200 make_squashfs() {
201     mksquashfs "${work_dir}/airootfs" "${out_dir}/casper/filesystem.squashfs"
202     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${out_dir}/casper/filesystem.size
203 }
204
205 make_deifnes() {
206     cp ${isolinux_dir}/README.diskdefines ${out_dir}/README.diskdefines
207 }
208
209 make_isolinux() {
210     grub-mkstandalone \
211         --format=x86_64-efi \
212         --output=isolinux/bootx64.efi \
213         --locales="" \
214         --fonts="" \
215         "boot/grub/grub.cfg=isolinux/grub.cfg"
216     (
217         cd isolinux && \
218         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
219         sudo mkfs.vfat efiboot.img && \
220         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
221         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
222     )
223     grub-mkstandalone \
224         --format=i386-pc \
225         --output=isolinux/core.img \
226         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
227         --modules="linux16 linux normal iso9660 biosdisk search" \
228         --locales="" \
229         --fonts="" \
230         "boot/grub/grub.cfg=isolinux/grub.cfg"
231     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
232
233     _msg_end "${job_name}"
234 }
235
236 make_md5sum() {
237     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
238 }
239
240 make_iso() {
241     xorriso \
242         -as mkisofs \
243         -iso-level 3 \
244         -full-iso9660-filenames \
245         -volid "${iso_label}" \
246         -appid "${iso_application}" \
247         -publisher "${iso_publisher}" \
248         -preparer "prepared by LUBS" \
249         -eltorito-boot boot/grub/bios.img \
250         -no-emul-boot \
251         -boot-load-size 4 \
252         -boot-info-table \
253         --eltorito-catalog boot/grub/boot.cat \
254         --grub2-boot-info \
255         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
256         -eltorito-alt-boot \
257         -e EFI/efiboot.img \
258         -no-emul-boot \
259         -append_partition 2 0xef isolinux/efiboot.img \
260         -output "../${iso_filename}" \
261         -graft-points \
262             "." \
263             /boot/grub/bios.img=isolinux/bios.img \
264             /EFI/efiboot.img=isolinux/efiboot.img
265 }
266
267 prepare_build
268 run_once make_basefs
269 run_once make_sourcelist
270 run_once make_systemd
271 run_once make_packages
272 run_once make_config
273 run_once make_customize_airootfs
274 run_once make_clean
275 run_once make_bootfiles
276 run_once make_grubcfg
277 run_once make_manifest
278 run_once make_squashfs
279 run_once make_deifnes
280 run_once run_out make_isolinux
281 run_once run_out make_md5sum
282 run_once run_out make_iso