makelni/common.d/build_functions.sh

149 lines
3.4 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
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"
return 0
}