OSDN Git Service

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