shimboot/build_rootfs.sh

88 lines
2.5 KiB
Bash
Raw Permalink Normal View History

2023-10-20 06:38:45 -04:00
#!/bin/bash
#build the debian rootfs
set -e
if [ "$DEBUG" ]; then
set -x
fi
. ./common.sh
2023-10-20 06:38:45 -04:00
print_help() {
echo "Usage: ./build_rootfs.sh rootfs_path release_name"
echo "Valid named arguments (specify with 'key=value'):"
echo " custom_packages - The packages that will be installed in place of task-xfce-desktop."
echo " hostname - The hostname for the new rootfs."
2024-05-03 07:59:07 -04:00
echo " enable_root - Enable the root user."
echo " root_passwd - The root password. This only has an effect if enable_root is set."
echo " username - The unprivileged user name for the new rootfs."
echo " user_passwd - The password for the unprivileged user."
2024-05-03 07:59:07 -04:00
echo " disable_base - Disable the base packages such as zram, cloud-utils, and command-not-found."
2024-06-12 01:27:19 -04:00
echo " arch - The CPU architecture to build the rootfs for."
echo "If you do not specify the hostname and credentials, you will be prompted for them later."
}
2023-10-20 06:38:45 -04:00
assert_root
assert_deps "realpath debootstrap findmnt"
assert_args "$2"
parse_args "$@"
2023-10-20 06:38:45 -04:00
2024-01-25 16:12:20 -05:00
rootfs_dir=$(realpath -m "${1}")
2023-10-20 06:38:45 -04:00
release_name="${2}"
packages="${args['custom_packages']-'task-xfce-desktop'}"
2024-06-13 21:02:10 -04:00
arch="${args['arch']-amd64}"
chroot_mounts="proc sys dev run"
2024-01-25 16:12:20 -05:00
mkdir -p $rootfs_dir
unmount_all() {
for mountpoint in $chroot_mounts; do
umount -l "$rootfs_dir/$mountpoint"
done
}
2023-10-20 06:38:45 -04:00
need_remount() {
local target="$1"
local mnt_options="$(findmnt -T "$target" | tail -n1 | rev | cut -f1 -d' '| rev)"
echo "$mnt_options" | grep -e "noexec" -e "nodev"
}
do_remount() {
local target="$1"
local mountpoint="$(findmnt -T "$target" | tail -n1 | cut -f1 -d' ')"
mount -o remount,dev,exec "$mountpoint"
}
if [ "$(need_remount "$rootfs_dir")" ]; then
do_remount "$rootfs_dir"
fi
2024-06-12 01:27:19 -04:00
debootstrap --arch $arch $release_name $rootfs_dir http://deb.debian.org/debian/
cp -ar rootfs/* $rootfs_dir
2023-12-25 22:33:28 -05:00
cp /etc/resolv.conf $rootfs_dir/etc/resolv.conf
2023-10-20 22:30:02 -04:00
trap unmount_all EXIT
2023-10-20 22:30:02 -04:00
for mountpoint in $chroot_mounts; do
mount --make-rslave --rbind "/${mountpoint}" "${rootfs_dir}/$mountpoint"
done
hostname="${args['hostname']}"
root_passwd="${args['root_passwd']}"
2024-05-03 07:59:07 -04:00
enable_root="${args['enable_root']}"
username="${args['username']}"
user_passwd="${args['user_passwd']}"
2024-05-03 07:59:07 -04:00
disable_base="${args['disable_base']}"
chroot_command="/opt/setup_rootfs.sh \
'$DEBUG' '$release_name' '$packages' \
'$hostname' '$root_passwd' '$username' \
2024-06-12 01:27:19 -04:00
'$user_passwd' '$enable_root' '$disable_base' \
'$arch'"
2024-05-03 07:59:07 -04:00
LC_ALL=C chroot $rootfs_dir /bin/bash -c "${chroot_command}"
2023-10-20 08:39:43 -04:00
trap - EXIT
unmount_all
2023-10-20 22:30:02 -04:00
2023-10-20 08:39:43 -04:00
echo "rootfs has been created"