177 lines
4.9 KiB
Bash
177 lines
4.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
require "wget"
|
|
|
|
get_image_path()
|
|
{
|
|
printf "%s" "$(realpath "./raw/${1}.img")"
|
|
}
|
|
|
|
setup_inet()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
[ -L "$rootdir/etc/resolv.conf" ] && {
|
|
mv "$rootdir/etc/resolv.conf" "$rootdir/etc/resolv.conf.1"
|
|
}
|
|
echo "nameserver 1.1.1.1" > "$rootdir/etc/resolv.conf"
|
|
echo "xiaomi-nabu" > "$rootdir/etc/hostname"
|
|
echo "127.0.0.1 localhost
|
|
127.0.1.1 xiaomi-nabu" > "$rootdir/etc/hosts"
|
|
}
|
|
|
|
create_image()
|
|
{
|
|
name="$(get_image_path "$1")"
|
|
if [ -z "$2" ]; then
|
|
size="$DEFAULT_IMAGE_SIZE"
|
|
else
|
|
size="${2}GB"
|
|
fi
|
|
|
|
if [ -f "$name" ]; then
|
|
rm "$name"
|
|
fi
|
|
|
|
truncate -s "$size" "$name" || {
|
|
log "Failed to cretae image [$name]" ierror
|
|
return 1
|
|
}
|
|
mkfs.ext4 "$name"
|
|
}
|
|
|
|
trim_image()
|
|
{
|
|
{
|
|
name="$1"
|
|
e2fsck -f "$name"
|
|
resize2fs -M "$name"
|
|
} || {
|
|
log "Failed to trim image" ierror
|
|
return 1
|
|
}
|
|
}
|
|
|
|
# shellcheck disable=SC2155
|
|
mount_image()
|
|
{
|
|
local mountdir="$(mktemp --tmpdir=./tmp/ -d)"
|
|
local mountdir="$(realpath "$mountdir")"
|
|
|
|
mount -o loop "$1" "$mountdir" || {
|
|
log "Failed to mount image" ierror
|
|
return 1
|
|
}
|
|
|
|
mkdir -p "$mountdir/boot/efi"
|
|
mkdir -p "$mountdir/boot/simpleinit"
|
|
|
|
mount -o size=512M,mode=0755 -t tmpfs nabu_esp "$mountdir/boot/efi"
|
|
mount -o size=512M,mode=0755 -t tmpfs nabu_simpleinit "$mountdir/boot/simpleinit"
|
|
|
|
sed "s|{cmdline}|$SIMPLEINIT_CMDLINE|g" < ./drop/simpleinit.uefi.cfg > "$mountdir/boot/simpleinit/simpleinit.uefi.cfg"
|
|
printf "%s" "$mountdir"
|
|
}
|
|
|
|
gen_fstab()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
|
|
if [ -d "$rootdir/etc/" ]; then
|
|
[ -f "$rootdir/etc/fstab" ] && rm "$rootdir/etc/fstab"
|
|
cp ./drop/fstab "$rootdir/etc/fstab"
|
|
fi
|
|
}
|
|
|
|
umount_image()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
|
|
rootdir="$(realpath "${1}")"
|
|
mkdir "$rootdir/opt/nabu/" -p
|
|
tar -cf "$rootdir/opt/nabu/efi.tar" -C "$rootdir/" boot/
|
|
cp ./drop/postinstall "$rootdir/opt/nabu/postinstall"
|
|
chmod +x "$rootdir/opt/nabu/postinstall"
|
|
|
|
umount "$rootdir/boot/efi" > /dev/null 2>&1
|
|
umount "$rootdir/boot/simpleinit" > /dev/null 2>&1
|
|
umount "$rootdir" > /dev/null 2>&1
|
|
rm -d "$rootdir" > /dev/null 2>&1
|
|
}
|
|
|
|
prepare_chroot()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
|
|
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:\$PATH
|
|
rootdir="$(realpath "$1")"
|
|
|
|
mount -t proc proc "$rootdir/proc" > /dev/null 2>&1
|
|
mount -t sysfs sysfs "$rootdir/sys" > /dev/null 2>&1
|
|
mount -t devtmpfs devtmpfs "$rootdir/dev" > /dev/null 2>&1
|
|
mount -t devpts devpts "$rootdir/dev/pts" > /dev/null 2>&1
|
|
mount -t tmpfs devshm "$rootdir/dev/shm" > /dev/null 2>&1
|
|
|
|
if uname -m | grep -q aarch64 || [ -f "/proc/sys/fs/binfmt_misc/qemu-aarch64" ]; then
|
|
log "Cancel qemu install for arm64" internal
|
|
else
|
|
wget -q --show-progress -N https://github.com/multiarch/qemu-user-static/releases/download/v7.2.0-1/qemu-aarch64-static -O ./cache/qemu-aarch64-static
|
|
install -m755 ./cache/qemu-aarch64-static "$rootdir/"
|
|
|
|
# shellcheck disable=SC2028
|
|
echo ':aarch64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/qemu-aarch64-static:PF' > /proc/sys/fs/binfmt_misc/register 2> /dev/null
|
|
|
|
# shellcheck disable=SC2028
|
|
echo ':aarch64ld:M::\x7fELF\x02\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/qemu-aarch64-static:PF' > /proc/sys/fs/binfmt_misc/register 2> /dev/null
|
|
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
detach_chroot()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
|
|
rootdir=$(realpath "$1")
|
|
umount "$rootdir/proc"
|
|
umount "$rootdir/sys"
|
|
umount "$rootdir/dev/pts"
|
|
umount "$rootdir/dev/shm"
|
|
umount "$rootdir/dev"
|
|
|
|
if uname -m | grep -q aarch64; then
|
|
log "Cancel qemu uninstall for arm64" internal
|
|
else
|
|
if [ -f "/proc/sys/fs/binfmt_misc/aarch64" ]; then
|
|
echo -1 > /proc/sys/fs/binfmt_misc/aarch64 2> /dev/null
|
|
fi
|
|
if [ -f "/proc/sys/fs/binfmt_misc/aarch64ld" ]; then
|
|
echo -1 > /proc/sys/fs/binfmt_misc/aarch64ld 2> /dev/null
|
|
fi
|
|
if [ -f "$rootdir/qemu-aarch64-static" ]; then
|
|
rm "$rootdir/qemu-aarch64-static"
|
|
fi
|
|
fi
|
|
return 0
|
|
}
|
|
|