Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ading2210
GitHub Repository: ading2210/shimboot
Path: blob/main/build_rootfs.sh
402 views
1
#!/bin/bash
2
3
#build the debian rootfs
4
5
. ./common.sh
6
7
print_help() {
8
echo "Usage: ./build_rootfs.sh rootfs_path release_name"
9
echo "Valid named arguments (specify with 'key=value'):"
10
echo " custom_packages - The packages that will be installed in place of task-xfce-desktop."
11
echo " hostname - The hostname for the new rootfs."
12
echo " enable_root - Enable the root user."
13
echo " root_passwd - The root password. This only has an effect if enable_root is set."
14
echo " username - The unprivileged user name for the new rootfs."
15
echo " user_passwd - The password for the unprivileged user."
16
echo " disable_base - Disable the base packages such as zram, cloud-utils, and command-not-found."
17
echo " arch - The CPU architecture to build the rootfs for."
18
echo " distro - The Linux distro to use. This should be either 'debian' or 'alpine'."
19
echo "If you do not specify the hostname and credentials, you will be prompted for them later."
20
}
21
22
assert_root
23
assert_deps "realpath debootstrap findmnt wget pcregrep tar"
24
assert_args "$2"
25
parse_args "$@"
26
27
rootfs_dir=$(realpath -m "${1}")
28
release_name="${2}"
29
packages="${args['custom_packages']-task-xfce-desktop}"
30
arch="${args['arch']-amd64}"
31
distro="${args['distro']-debian}"
32
chroot_mounts="proc sys dev run"
33
34
mkdir -p $rootfs_dir
35
36
unmount_all() {
37
for mountpoint in $chroot_mounts; do
38
umount -l "$rootfs_dir/$mountpoint"
39
done
40
}
41
42
need_remount() {
43
local target="$1"
44
local mnt_options="$(findmnt -T "$target" | tail -n1 | rev | cut -f1 -d' '| rev)"
45
echo "$mnt_options" | grep -e "noexec" -e "nodev"
46
}
47
48
do_remount() {
49
local target="$1"
50
local mountpoint="$(findmnt -T "$target" | tail -n1 | cut -f1 -d' ')"
51
mount -o remount,dev,exec "$mountpoint"
52
}
53
54
if [ "$(need_remount "$rootfs_dir")" ]; then
55
do_remount "$rootfs_dir"
56
fi
57
58
if [ "$distro" = "debian" ]; then
59
print_info "bootstraping debian chroot"
60
debootstrap --arch $arch --components=main,contrib,non-free,non-free-firmware "$release_name" "$rootfs_dir" http://deb.debian.org/debian/
61
chroot_script="/opt/setup_rootfs.sh"
62
63
elif [ "$distro" = "ubuntu" ]; then
64
print_info "bootstraping ubuntu chroot"
65
repo_url="http://archive.ubuntu.com/ubuntu"
66
if [ "$arch" = "amd64" ]; then
67
repo_url="http://archive.ubuntu.com/ubuntu"
68
else
69
repo_url="http://ports.ubuntu.com"
70
fi
71
debootstrap --arch $arch "$release_name" "$rootfs_dir" "$repo_url"
72
chroot_script="/opt/setup_rootfs.sh"
73
74
elif [ "$distro" = "alpine" ]; then
75
print_info "downloading alpine package list"
76
pkg_list_url="https://dl-cdn.alpinelinux.org/alpine/latest-stable/main/x86_64/"
77
pkg_data="$(wget -qO- --show-progress "$pkg_list_url" | grep "apk-tools-static")"
78
pkg_url="$pkg_list_url$(echo "$pkg_data" | pcregrep -o1 '"(.+?.apk)"')"
79
80
print_info "downloading and extracting apk-tools-static"
81
pkg_extract_dir="/tmp/apk-tools-static"
82
pkg_dl_path="$pkg_extract_dir/pkg.apk"
83
apk_static="$pkg_extract_dir/sbin/apk.static"
84
mkdir -p "$pkg_extract_dir"
85
wget -q --show-progress "$pkg_url" -O "$pkg_dl_path"
86
tar --warning=no-unknown-keyword -xzf "$pkg_dl_path" -C "$pkg_extract_dir"
87
88
print_info "bootstraping alpine chroot"
89
real_arch="x86_64"
90
if [ "$arch" = "arm64" ]; then
91
real_arch="aarch64"
92
fi
93
$apk_static \
94
--arch $real_arch \
95
-X http://dl-cdn.alpinelinux.org/alpine/$release_name/main/ \
96
-U --allow-untrusted \
97
--root "$rootfs_dir" \
98
--initdb add alpine-base
99
chroot_script="/opt/setup_rootfs_alpine.sh"
100
101
else
102
print_error "'$distro' is an invalid distro choice."
103
exit 1
104
fi
105
106
print_info "copying rootfs setup scripts"
107
cp -arv rootfs/* "$rootfs_dir"
108
cp /etc/resolv.conf "$rootfs_dir/etc/resolv.conf"
109
110
print_info "creating bind mounts for chroot"
111
trap unmount_all EXIT
112
for mountpoint in $chroot_mounts; do
113
mount --make-rslave --rbind "/${mountpoint}" "${rootfs_dir}/$mountpoint"
114
done
115
116
hostname="${args['hostname']}"
117
root_passwd="${args['root_passwd']}"
118
enable_root="${args['enable_root']}"
119
username="${args['username']}"
120
user_passwd="${args['user_passwd']}"
121
disable_base="${args['disable_base']}"
122
123
chroot_command="$chroot_script \
124
'$DEBUG' '$release_name' '$packages' \
125
'$hostname' '$root_passwd' '$username' \
126
'$user_passwd' '$enable_root' '$disable_base' \
127
'$arch'"
128
129
LC_ALL=C chroot $rootfs_dir /bin/sh -c "${chroot_command}"
130
131
trap - EXIT
132
unmount_all
133
134
print_info "rootfs has been created"
135