git » mk-desktop-image.git » main » tree

[main] / make.sh

#!/bin/bash
set -e
BSN=desksys
BS=/$BSN
if [ -d $BS ]; then
	echo "Will delete $BS, enter to continue"
	read dummy
	# Shh, just in case
	umount $BS/dev 2>/dev/null || true
	umount $BS/proc 2>/dev/null || true
	umount $BS/sys 2>/dev/null || true
	umount $BS/var/cache/pacman/pkg 2>/dev/null || true
	umount $BS 2>/dev/null || true
	rm -r $BS
fi

if [ "$1" == "--untested" ]; then
	PKG_FS=/sources/pkg
	PKG_URL=https://a64box.urja.dev/armlfs/pkg
	DEBUGINFO_URL=https://a64box.urja.dev/armlfs-debuginfo
	DEVMODE=1
	IMGNAME=untested-armlfs
	COMPFMT=zstd
	COMPEXT=zst
else
	PKG_FS=/mnt/nfs/pkg
	PKG_URL=https://armlfs.urja.dev/pkg
	DEBUGINFO_URL=https://armlfs.urja.dev/debuginfo
	IMGNAME=armlfs-kde5
	COMPFMT=xz
	COMPEXT=xz
fi
echo "Making $IMGNAME (-base and -showcase) (tar and img) .$COMPEXT"

rm -f $BSN.img{,.zst,.xz}
truncate -s 15G $BSN.img

mkdir -p $BS

# losetup -P/partscan-support would need more DeviceAllows ...
LOOP=$(losetup -o 20971520 --sizelimit 16085138432 -P -f --show $BSN.img)
mkfs.ext4 -m 1 -L ARMLFS $LOOP
mount $LOOP -t ext4 $BS

# To allow pacman-in-chroot to believe that / is a mount point
#mount --bind $BS $BS

# Setup pacman (and keys)
mkdir -p $BS/etc/pacman.d/keys
cp /etc/pacman.d/keys/armlfs-release-key.pub $BS/etc/pacman.d/keys/
[ -n "$DEVMODE" ] && cp /etc/pacman.d/keys/armlfs-untested-key.pub $BS/etc/pacman.d/keys/ || true
cp pacman.conf $BS/etc/
CFG=$BS/etc/pacman.conf
sed -i $CFG -e "s|@URL[@]|$PKG_URL|g"

mkdir -p $BS/etc/pki/tls/certs
cp /etc/pki/tls/certs/ca-bundle.crt $BS/etc/pki/tls/certs/

mkdir -p $BS/var/lib/pacman
pacman --config $CFG -Sy -b $BS/var/lib/pacman -r $BS

cp packages.txt $BS/pkglist.txt
yes | pacman -Scc
pacman --config $CFG -S --noconfirm  --noscriptlet -b $BS/var/lib/pacman -r $BS minimal

# Greeter background image
mkdir -p $BS/usr/share/backgrounds/armlfs/
cp greeter_bg.jpg $BS/usr/share/backgrounds/armlfs/greeter.jpg

# Extlinux configuration
FS_UUID=$(uuidgen)
FS_UUID=${FS_UUID^^}

mkdir -p $BS/boot/extlinux
cp extlinux.conf $BS/boot/extlinux
sed -i "s/%PARTUUID%/${FS_UUID}/" $BS/boot/extlinux/extlinux.conf

# cd into target
cp initial.sh $BS
cd $BS

# Debuginfo
mkdir -p etc/debuginfod
echo "$DEBUGINFO_URL" > etc/debuginfod/armlfs.urls

# Timezone
ln -s ../usr/share/zoneinfo/Europe/Helsinki etc/localtime

# Keyboard
cat > etc/profile.d/c201-keyboard.sh << "EOF"
if grep -q "google,veyron" /proc/device-tree/compatible 2>/dev/null; then
	export XKB_DEFAULT_LAYOUT=c201
	export XKB_DEFAULT_VARIANT=usfibadesc
fi
EOF

# systemd-coredump
if [ -z "$DEVMODE" ]; then
	cat >> etc/systemd/coredump.conf << "EOF"
Storage=none
ProcessSizeMax=0
EOF
fi

run_in_chroot() {
mount -t proc proc proc
mount -t sysfs sys sys
mount --bind /dev dev
mkdir -p var/cache/pacman/pkg
mkdir -p /tmp/ds{cache,work}
cp /etc/resolv.conf etc/
mount -t overlay -o lowerdir=$PKG_FS/armv7h/kde5:$PKG_FS/armv7h/base:$PKG_FS/armv7h/core,upperdir=/tmp/dscache,workdir=/tmp/dswork overlay var/cache/pacman/pkg
chmod +x ./$1
chroot . ./$1
umount proc sys dev var/cache/pacman/pkg
rm ./$1
rm ./pkglist.txt
rm etc/resolv.conf
return 0
}
run_in_chroot initial.sh

# After-package-install (re)configuration

# greetd
sed -i 's/^command = .*/command = "cage -m last -s -- regreet 2>\&1 | logger"/' etc/greetd/config.toml

# regreet
cat >> etc/greetd/regreet.toml << "EOF"
[background]
path = "/usr/share/backgrounds/armlfs/greeter.jpg"
[GTK]
application_prefer_dark_theme = true
EOF

#sudo
sed -i 's/^# %wheel ALL=(ALL:ALL) NOPASSWD: ALL/%wheel ALL=(ALL:ALL) NOPASSWD: ALL/' etc/sudoers

cd -
umount $BS
e2fsck -f $LOOP
cp $BSN.img $BSN-stage2-bak.img
mount $LOOP -t ext4 $BS

tarball_and_image() {
rm -f $1.tar.$COMPEXT
echo "Tarballing up the system..."
time tar --xattrs --acls --$COMPFMT -C $BS -cf $1.tar.$COMPEXT .
echo "Creating the microSD disk image..."

# Adding the repart configuration
# We shall never fiddle with your partitions if our disk image was not used,
# thus we dont include these in the tarball.
mkdir -p $BS/usr/lib/repart.d
cat > $BS/usr/lib/repart.d/50-root.conf << "EOF"
[Partition]
Type=linux-generic
EOF

cat > $BS/usr/lib/repart.d/60-swap.conf << "EOF"
[Partition]
Type=swap
SizeMinBytes=128M
SizeMaxBytes=1G
Weight=33
Priority=1
EOF

ln -vsf /usr/lib/systemd/system/systemd-repart.service $BS/etc/systemd/system/multi-user.target.wants/systemd-repart.service
ln -vsf /usr/lib/systemd/system/systemd-growfs-root.service $BS/etc/systemd/system/multi-user.target.wants/systemd-growfs-root.service

dd if=$BS/boot/vmlinux.kpart of=$BSN.img bs=1M seek=4 conv=notrunc
umount $BS
e2fsck -f $LOOP
SZ=$(resize2fs -P $LOOP | cut -d ':' -f 2)
SZ_FS=$((SZ + 500000))
SZ_PART=$(( (SZ + 524288) * 8 ))
SZ_DISK=$(( (SZ_PART + 40960 + 128) * 512 ))
resize2fs $LOOP $SZ_FS
losetup -d $LOOP
truncate -s $SZ_DISK $BSN.img
echo "FS_UUID: ${FS_UUID}"
cgpt create $BSN.img 2>/dev/null
cgpt boot -p $BSN.img
cgpt add -i 1 -t kernel -b 8192 -s 32768 -l Kernel -S 1 -T 5 -P 10 $BSN.img
cgpt add -i 2 -t data -b 40960 -s $SZ_PART -l Root -u ${FS_UUID} $BSN.img
cgpt show $BSN.img
echo Image done - compressing
$COMPFMT -5 -v $BSN.img
mv $BSN.img.$COMPEXT $1.img.$COMPEXT
echo Done $1
return 0
}
tarball_and_image $IMGNAME-base

# Restore image and loopback mount for stage 2
mv $BSN-stage2-bak.img $BSN.img
LOOP=$(losetup -o 20971520 --sizelimit 16085138432 -P -f --show $BSN.img)
mount $LOOP -t ext4 $BS

# Pre-configure waydroid
#mkdir -p $BS/var/lib
#tar -C $BS/var/lib -xf var-lib-waydroid.tar.gz

# Add stage2.sh and package list and cd into target
cp showcase.txt $BS/pkglist.txt
cp stage2.sh $BS
cd $BS

run_in_chroot stage2.sh

# Return to our cd
cd -
# Make stage2 images
tarball_and_image $IMGNAME-showcase