OSDN Git Service

[change] : Changed function name.
[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 cache_dir="cache"
10 work_dir="work"
11 script_path=$(readlink -f ${0%/*})
12 codename="focal"
13 #codename="bionic"
14 mirror="http://ftp.jaist.ac.jp/pub/Linux/ubuntu/"
15
16
17 # Show an INFO message
18 # _msg_info <message>
19 _msg_info () {
20     local _msg="${@}"
21     echo -e "\033[0;36m[LUBS Core] \033[0;32mINFO:\033[0;39m ${_msg}"
22 }
23
24 # Show an ERROR message then exit with status
25 # _msg_error <message> <exit code>
26 _msg_error() {
27     local _msg="${1}"
28     local _error=${2}
29     echo -e "\033[0;36m[LUBS Core] \033[0;31mERROR:\033[0;39m ${_msg}" >&2
30     if [[ ! ${_error} = 0 ]]; then
31         exit ${_error}
32     fi
33 }
34
35
36 # Helper function to run make_*() only one time.
37 run_once() {
38     if [[ ! -e "${work_dir}/build.${1}" ]]; then
39         "$1"
40         touch "${work_dir}/build.${1}"
41     fi
42 }
43
44
45 run_cmd () {
46     "${script_path}/lubs-chroot" "${work_dir}/airootfs" ${@}
47 }
48
49 _apt_install () {
50     run_cmd apt-get --yes install ${@}
51 }
52
53
54
55
56 prepare_build () {
57     if [[ ${EUID} -ne 0 ]]; then
58         _msg_error "This script must be run as root." 1
59     fi
60     
61     [[ ! -d "${work_dir}" ]] && mkdir -p "${work_dir}"
62
63     local mount
64     for mount in $(mount | awk '{print $3}' | grep $(realpath ${work_dir}) | sort -r); do
65         _msg_info "Unmounting ${mount}"
66         umount "${mount}"
67     done
68
69 }
70
71
72 make_basefs () {
73     if [[ ! -d "${cache_dir}/${codename}" ]]; then
74         _msg_info "Installing Ubuntu to '${cache_dir}/${codename}'..."
75         mkdir -p "${cache_dir}/${codename}"
76         debootstrap --arch=${arch} --include=linux-image-generic  --verbose --merged-usr "${codename}" "${cache_dir}/${codename}" ${mirror}
77         _msg_info "${codename} installed successfully!"
78     fi
79     
80     rm -rf "${work_dir}/airootfs" && mkdir -p "${work_dir}/airootfs"
81     _msg_info "copy base files from '${cache_dir}/${codename}' to '${work_dir}/airootfs'..."
82     rsync  -au "${cache_dir}/${codename}/" "${work_dir}/airootfs"
83     echo 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PATH}' >> "${work_dir}/airootfs/etc/bash.bashrc"
84     run_cmd apt-get update
85     # run_cmd apt-get upgrade
86     _msg_info "make basefs done!"
87 }
88
89 make_sourcelist () {
90     cp ${script_path}/source.list.d/${codename}/* ${work_dir}/airootfs/etc/apt
91 }
92
93 make_systemd() {
94     run_cmd apt-get install --yes systemd-sysv
95     run_cmd dbus-uuidgen > /etc/machine-id
96     run_cmd ln -fs /etc/machine-id /var/lib/dbus/machine-id
97 }
98
99 make_packages () {
100     run_cmd apt-get update
101     installpkglist=($(grep -h -v ^'#' ${script_path}/packages.x86_64))
102     run_cmd sudo -i apt-get --yes install ${installpkglist[@]}
103 }
104
105 make_config() {
106     run_cmd dpkg-reconfigure locales
107     run_cmd dpkg-reconfigure resolvconf
108     run_cmd echo -e "[main]\nrc-manager=resolvconf\nplugins=ifupdown,keyfile\n\ndns=dnsmasq\n[ifupdown]\nmanaged=false" > /etc/NetworkManager/NetworkManager.conf
109     run_cmd dpkg-reconfigure network-manager
110     run_cmd truncate -s 0 /etc/machine-id
111 }
112
113 make_customize_airootfs() {
114     # Overwrite airootfs with customize_airootfs.
115     cp -af "${script_path}/airootfs" "${work_dir}"
116
117     if [[ -f "${work_dir}/airootfs/root/customize_airootfs.sh" ]]; then
118         chmod 755 "${work_dir}/airootfs/root/customize_airootfs.sh"
119         run_cmd "/root/customize_airootfs.sh"
120     fi
121 }
122
123 make_clean() {
124     run_cmd apt-get clean
125     run_cmd rm -rf "/tmp/* ~/.bash_history"
126 }
127
128 make_bootfiles() {
129     run_cmd update-initramfs -c -k all
130     run_cmd apt-get install --yes memtest86+
131     mkdir -p out/{casper,isolinux,install}
132     cp ${work_dir}/airootfs/boot/vmlinuz-*-*-generic out/casper/vmlinuz
133     cp ${work_dir}/airootfs/boot/initrd.img-*-*-generic out/casper/initrd
134     cp ${work_dir}/airootfs/boot/memtest86+.bin out/install/memtest86+
135     wget -O out/install/memtest86-usb.zip https://www.memtest86.com/downloads/memtest86-usb.zip
136     zip -r out/install/memtest86-usb.zip memtest86-usb.img out/install/memtest86
137     rm -f out/install/memtest86-usb.zip
138 }
139
140 make_grubcfg() {
141 touch "out/ubuntu"
142 cat <<EOF > out/isolinux/grub.cfg
143
144 search --set=root --file /ubuntu
145
146 insmod all_video
147
148 set default="0"
149 set timeout=30
150
151 menuentry "Try Ubuntu FS without installing" {
152    linux /casper/vmlinuz boot=casper quiet splash ---
153    initrd /casper/initrd
154 }
155
156 menuentry "Install Ubuntu FS" {
157    linux /casper/vmlinuz boot=casper only-ubiquity quiet splash ---
158    initrd /casper/initrd
159 }
160
161 menuentry "Check disc for defects" {
162    linux /casper/vmlinuz boot=casper integrity-check quiet splash ---
163    initrd /casper/initrd
164 }
165
166 menuentry "Test memory Memtest86+ (BIOS)" {
167    linux16 /install/memtest86+
168 }
169
170 menuentry "Test memory Memtest86 (UEFI, long load time)" {
171    insmod part_gpt
172    insmod search_fs_uuid
173    insmod chain
174    loopback loop /install/memtest86
175    chainloader (loop,gpt1)/efi/boot/BOOTX64.efi
176 }
177 EOF
178 }
179
180 make_manifest() {
181     run_cmd dpkg-query -W --showformat='${Package} ${Version}\n' | tee out/casper/filesystem.manifest
182     cp -v out/casper/filesystem.manifest "out/casper/filesystem.manifest-desktop"
183     sed -i '/ubiquity/d' "out/casper/filesystem.manifest-desktop"
184     sed -i '/casper/d' "out/casper/filesystem.manifest-desktop"
185     sed -i '/discover/d' "out/casper/filesystem.manifest-desktop"
186     sed -i '/laptop-detect/d' "out/casper/filesystem.manifest-desktop"
187     sed -i '/os-prober/d' "out/casper/filesystem.manifest-desktop"
188 }
189
190 make_squashfs() {
191     mksquashfs "${work_dir}/airootfs" "out/casper/filesystem.squashfs"
192     printf $(du -sx --block-size=1 "${work_dir}/airootfs" | cut -f1) > out/casper/filesystem.size
193 }
194
195 make_deifnes() {
196 cat <<EOF > out/README.diskdefines
197 #define DISKNAME  Ubuntu from scratch
198 #define TYPE  binary
199 #define TYPEbinary  1
200 #define ARCH  amd64
201 #define ARCHamd64  1
202 #define DISKNUM  1
203 #define DISKNUM1  1
204 #define TOTALNUM  0
205 #define TOTALNUM0  1
206 EOF
207 }
208
209 make_isolinux() {
210     cd out
211     grub-mkstandalone \
212         --format=x86_64-efi \
213         --output=isolinux/bootx64.efi \
214         --locales="" \
215         --fonts="" \
216         "boot/grub/grub.cfg=isolinux/grub.cfg"
217     (
218         cd isolinux && \
219         dd if=/dev/zero of=efiboot.img bs=1M count=10 && \
220         sudo mkfs.vfat efiboot.img && \
221         LC_CTYPE=C mmd -i efiboot.img efi efi/boot && \
222         LC_CTYPE=C mcopy -i efiboot.img ./bootx64.efi ::efi/boot/
223     )
224     grub-mkstandalone \
225         --format=i386-pc \
226         --output=isolinux/core.img \
227         --install-modules="linux16 linux normal iso9660 biosdisk memdisk search tar ls" \
228         --modules="linux16 linux normal iso9660 biosdisk search" \
229         --locales="" \
230         --fonts="" \
231         "boot/grub/grub.cfg=isolinux/grub.cfg"
232     cat /usr/lib/grub/i386-pc/cdboot.img isolinux/core.img > isolinux/bios.img
233     cd ..
234 }
235
236 make_md5sum() {
237     cd out
238     /bin/bash -c "(find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt)"
239     cd ..
240 }
241
242 make_iso() {
243     cd out
244     xorriso \
245         -as mkisofs \
246         -iso-level 3 \
247         -full-iso9660-filenames \
248         -volid "Ubuntu from scratch" \
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 "../ubuntu-from-scratch.iso" \
261         -graft-points \
262             "." \
263             /boot/grub/bios.img=isolinux/bios.img \
264             /EFI/efiboot.img=isolinux/efiboot.img
265     cd ..
266 }
267
268 prepare_build
269 run_once make_basefs
270 run_once make_sourcelist
271 run_once make_systemd
272 run_once make_packages
273 run_once make_config
274 run_once make_customize_airootfs
275 run_once make_clean
276 run_once make_bootfiles
277 run_once make_grubcfg
278 run_once make_manifest
279 run_once make_squashfs
280 run_once make_deifnes
281 run_once make_isolinux
282 run_once make_md5sum
283 run_once make_iso