OSDN Git Service

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