OSDN Git Service

[fix] : Fixed something
[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_bootfiles" == "$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_bootfiles" == "$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     [[ ! -d "${out_dir}" ]] && mkdir -p "${out_dir}"
96
97     local mount
98     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
99         _msg_info "Unmounting ${mount}"
100         umount "${mount}"
101     done
102
103 }
104
105
106 make_basefs() {
107     if [[ ! -d "${cache_dir}/${codename}" ]]; then
108         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
109         mkdir -p "${cache_dir}/${codename}"
110         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
111         _msg_info "${codename} installed successfully!"
112     else
113         _msg_info "${codename} cache is found."
114     fi
115     
116     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
117     _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
118     rsync  -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
119     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
120     run_cmd apt-get update
121     # run_cmd apt-get upgrade
122 }
123
124 make_sourcelist() {
125     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
126 }
127
128 make_systemd() {
129     _apt_install systemd-sysv
130     run_cmd dbus-uuidgen > /etc/machine-id
131     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
132 }
133
134 make_packages() {
135     run_cmd apt-get update
136     grep -h -v ^'#' "${channels_dir}/share/packages.x86_64" | grep -v "^$" > "${work_dir}/airootfs/installpkglist"
137     grep -h -v ^'#' "${channels_dir}/${channel_name}/packages.x86_64" | grep -v "^$" >> "${work_dir}/airootfs/installpkglist"
138     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive apt-get --yes install $(echo $(<installpkglist))'
139     rm ${work_dir}/airootfs/installpkglist
140 }
141
142 make_config() {
143     # Locales
144     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'
145
146     # resolvconf
147     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure resolvconf'
148
149     # NetworkManager
150     cp ${channels_dir}/share/airootfs/etc/NetworkManager/NetworkManager.conf ${work_dir}/airootfs/etc/NetworkManager/NetworkManager.conf
151
152     # Timezone
153     #run_cmd echo -ne "UTC" > '/etc/timezone'
154     #run_cmd dpkg-reconfigure -f noninteractive tzdata
155
156     run_cmd env -i bash -c 'DEBIAN_FRONTEND=noninteractive dpkg-reconfigure network-manager'
157     run_cmd truncate -s 0 /etc/machine-id
158 }
159
160 make_customize_airootfs() {
161     # Overwrite airootfs with customize_airootfs.
162     local copy_airootfs
163
164     copy_airootfs() {
165         local i 
166         for i in "${@}"; do
167             local _dir="${1%/}"
168             if [[ -d "${_dir}" ]]; then
169                 cp -af "${_dir}"/* "${work_dir}/airootfs"
170             fi
171         done
172     }
173
174     copy_airootfs "${channels_dir}/share/airootfs"
175     copy_airootfs "${channels_dir}/${channel_name}/airootfs"
176
177     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
178         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
179         run_cmd "/root/customize_airootfs.sh"
180     fi
181 }
182
183 make_clean() {
184     run_cmd apt-get clean
185     run_cmd apt-get --yes autoremove
186     run_cmd rm -rf "/tmp/* ~/.bash_history"
187 }
188
189 make_bootfiles() {
190     run_cmd update-initramfs -c -k all
191     _apt_install memtest86+
192     mkdir -p ${bootfiles_dir}/{casper,isolinux,install}
193     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic ${bootfiles_dir}/casper/vmlinuz
194     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic ${bootfiles_dir}/casper/initrd
195     cp ${work_dir}/airootfs/boot/memtest86+.bin ${bootfiles_dir}/install/memtest86+
196
197     if [[ ! -f "${cache_dir}/memtest86-usb.zip" ]]; then
198         wget -O ${cache_dir}/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
199     fi
200
201     (unzip -p ${cache_dir}/memtest86-usb.zip memtest86-usb.img > ${bootfiles_dir}/install/memtest86)
202 }
203
204 make_grubcfg() {
205     touch "${bootfiles_dir}/ubuntu"
206     cp ${isolinux_dir}/grub.cfg ${bootfiles_dir}/isolinux/grub.cfg
207 }
208
209 make_manifest() {
210     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee ${bootfiles_dir}/casper/filesystem.manifest
211     cp -v ${bootfiles_dir}/casper/filesystem.manifest "${bootfiles_dir}/casper/filesystem.manifest-desktop"
212     sed -i '/ubiquity/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
213     sed -i '/casper/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
214     sed -i '/discover/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
215     sed -i '/laptop-detect/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
216     sed -i '/os-prober/d' "${bootfiles_dir}/casper/filesystem.manifest-desktop"
217 }
218
219 make_squashfs() {
220     mksquashfs "${work_dir}/airootfs" "${bootfiles_dir}/casper/filesystem.squashfs"
221     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > ${bootfiles_dir}/casper/filesystem.size
222 }
223
224 make_deifnes() {
225     cp ${isolinux_dir}/README.diskdefines ${bootfiles_dir}/README.diskdefines
226 }
227
228 make_isolinux() {
229     grub-mkstandalone \
230         --format=x86_64-efi \
231         --output=isolinux/bootx64.efi \
232         --locales="" \
233         --fonts="" \
234         "boot/grub/grub.cfg=isolinux/grub.cfg"
235     (
236         cd isolinux && \
237         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
238         sudo mkfs.vfat efiboot.img && \
239         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
240         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
241     )
242     grub-mkstandalone \
243         --format=i386-pc \
244         --output=isolinux/core.img \
245         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
246         --modules="linux16 linux normal iso9660 biosdisk search" \
247         --locales="" \
248         --fonts="" \
249         "boot/grub/grub.cfg=isolinux/grub.cfg"
250     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
251 }
252
253 make_md5sum() {
254     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
255 }
256
257 make_iso() {
258     xorriso \
259         -as mkisofs \
260         -iso-level 3 \
261         -full-iso9660-filenames \
262         -volid "${iso_label}" \
263         -appid "${iso_application}" \
264         -publisher "${iso_publisher}" \
265         -preparer "prepared by LUBS" \
266         -eltorito-boot boot/grub/bios.img \
267         -no-emul-boot \
268         -boot-load-size 4 \
269         -boot-info-table \
270         --eltorito-catalog boot/grub/boot.cat \
271         --grub2-boot-info \
272         --grub2-mbr /usr/lib/grub/i386-pc/boot_hybrid.img \
273         -eltorito-alt-boot \
274         -e EFI/efiboot.img \
275         -no-emul-boot \
276         -append_partition 2 0xef isolinux/efiboot.img \
277         -output "${out_dir}/${iso_filename}" \
278         -graft-points \
279             "." \
280             /boot/grub/bios.img=isolinux/bios.img \
281             /EFI/efiboot.img=isolinux/efiboot.img
282 }
283
284 make_chacksum() {
285     cd ${out_dir}
286     _msg_info "Creating md5 checksum ..."
287     md5sum "${out_dir}/${iso_filename}" > "${out_dir}/${iso_filename}.md5"
288
289     _msg_info "Creating sha256 checksum ..."
290     sha256sum "${out_dir}/${iso_filename}" > "${out_dir}/${iso_filename}.sha256"
291     cd - > /dev/null 2>&1
292 }
293
294 prepare_build
295 run_once make_basefs
296 run_once make_sourcelist
297 run_once make_systemd
298 run_once make_packages
299 run_once make_config
300 run_once make_customize_airootfs
301 run_once make_clean
302 run_once make_bootfiles
303 run_once make_grubcfg
304 run_once make_manifest
305 run_once make_squashfs
306 run_once make_deifnes
307 run_once run_bootfiles make_isolinux
308 run_once run_bootfiles make_md5sum
309 run_once run_bootfiles make_iso
310 run_once make_chacksum