OSDN Git Service

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