160 lines
3.7 KiB
Bash
160 lines
3.7 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"
|
|
return 0
|
|
}
|
|
|
|
create_image()
|
|
{
|
|
local 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
|
|
}
|
|
log "Creating ext4 fs on $name" internal
|
|
mkfs.ext4 "$name" || {
|
|
log "Failed to create new fs" ierror
|
|
}
|
|
}
|
|
|
|
trim_image()
|
|
{
|
|
{
|
|
local name="$(get_image_path "$1")"
|
|
e2fsck -f "$name"
|
|
resize2fs -M "$name"
|
|
} || {
|
|
log "Failed to trim image" ierror
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
# shellcheck disable=SC2155
|
|
mount_image()
|
|
{
|
|
local mountdir="$(mktemp --tmpdir=./tmp/ -d)"
|
|
local mountdir="$(realpath "$mountdir")"
|
|
|
|
local name="$(get_image_path "$1")"
|
|
|
|
mount -o loop "$name" "$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"
|
|
return 0
|
|
}
|
|
|
|
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
|
|
return 0
|
|
}
|
|
|
|
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_force "$rootdir/boot/efi"
|
|
umount_force "$rootdir/boot/simpleinit"
|
|
umount_force "$rootdir"
|
|
rm -d "$rootdir"
|
|
return 0
|
|
}
|
|
|
|
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"
|
|
mount -t sysfs sysfs "$rootdir/sys"
|
|
mount -t devtmpfs devtmpfs "$rootdir/dev"
|
|
mount -t devpts devpts "$rootdir/dev/pts"
|
|
mount -t tmpfs devshm "$rootdir/dev/shm"
|
|
return 0
|
|
}
|
|
|
|
detach_chroot()
|
|
{
|
|
local rootdir="$1"
|
|
[ ! -d "$rootdir" ] && {
|
|
log "Rootdir [$rootdir] does not exists" ierror
|
|
return 2
|
|
}
|
|
|
|
rootdir=$(realpath "$1")
|
|
killall gpg-agent > /dev/null 2>&1
|
|
umount_force "$rootdir/proc" > /dev/null 2>&1
|
|
umount_force "$rootdir/sys" > /dev/null 2>&1
|
|
umount_force "$rootdir/dev/pts" > /dev/null 2>&1
|
|
umount_force "$rootdir/dev/shm" > /dev/null 2>&1
|
|
umount_force "$rootdir/dev" > /dev/null 2>&1
|
|
return 0
|
|
}
|
|
|