~sircmpwn/sr.ht-dev

builds.sr.ht: images/freebsd: add a custom install script v1 PROPOSED

Simon Ser: 1
 images/freebsd: add a custom install script

 3 files changed, 81 insertions(+), 64 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/sr.ht-dev/patches/3300/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH builds.sr.ht] images/freebsd: add a custom install script Export this patch

From: emersion <contact@emersion.fr>
---

References:

- https://lists.freebsd.org/pipermail/freebsd-hackers/2019-February/054030.html
- https://hackmd.io/s/SJRD7QRNE#

 images/freebsd/12.0/genimg |   2 +-
 images/freebsd/genimg      | 108 +++++++++++++++++++++++++++----------
 images/freebsd/install     |  35 ------------
 3 files changed, 81 insertions(+), 64 deletions(-)
 delete mode 100644 images/freebsd/install

diff --git a/images/freebsd/12.0/genimg b/images/freebsd/12.0/genimg
index b85d949..13f136c 100755
--- a/images/freebsd/12.0/genimg
+++ b/images/freebsd/12.0/genimg
@@ -1,3 +1,3 @@
#!/bin/sh
export release=12.0
export release=12.0-RELEASE
exec ../genimg "$@"
diff --git a/images/freebsd/genimg b/images/freebsd/genimg
index 930b09f..3365cce 100755
--- a/images/freebsd/genimg
+++ b/images/freebsd/genimg
@@ -3,42 +3,94 @@
echo "$release" >/dev/null # fail on -u if release unset
arch="${1:-amd64}"

export DISTRIBUTIONS="kernel.txz base.txz ports.txz"
export BSDINSTALL_DISTSITE="https://download.freebsd.org/ftp/releases/$arch/$release/"
export BSDINSTALL_DISTDIR="/usr/freebsd-dist/$arch/$release"
export BSDINSTALL_LOG="/tmp/bsdinstall.log"
dist_base="https://download.freebsd.org/ftp/releases/$arch/$release"
dist_files="kernel.txz base.txz ports.txz"
dist_dir="/usr/freebsd-dist/$arch/$release"

mkdir -p "$dist_dir"
for f in $dist_files
do
	curl -C - -o "$dist_dir/$f" "$dist_base/$f"
done

cleanup() {
	cat "$BSDINSTALL_LOG" || true
	# The order here is important if you don't want to hose your mounts
	umount -f /mnt/dev/pts 2>/dev/null || true
	umount -f /mnt/dev/shm 2>/dev/null || true
	umount -f /mnt/dev 2>/dev/null || true
	umount -f /mnt/proc 2>/dev/null || true
	umount -f /mnt/run 2>/dev/null || true
	umount -f /mnt/sys 2>/dev/null || true
	umount -f /mnt/boot 2>/dev/null || true
	umount -f /mnt 2>/dev/null || true 
	mdconfig -d -u 0
	sync || true
	umount /mnt/dev || true
	umount /mnt || true
	mdconfig -du md0 || true
}

# qemu-nbd is not supported, so we just create a raw image and convert it
rm -f "$BSDINSTALL_LOG"
qemu-img create -f raw root.img.raw 6G
mdconfig -a -t vnode -f root.img.raw -u 0
trap cleanup EXIT

export nonInteractive=YES
mkdir -p "$BSDINSTALL_DISTDIR"
bsdinstall distfetch
bsdinstall checksum
bsdinstall script ./install
rm -f disk.img
truncate -s 4G disk.img
mdconfig -a -t vnode -f disk.img -u md0
gpart create -s gpt /dev/md0
gpart add -t freebsd-boot -l bootfs -b 40 -s 512K md0
gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 md0
gpart add -t freebsd-ufs -l rootfs -b 1M -s 3G md0
newfs -U /dev/md0p2

mount /dev/md0p2 /mnt
mkdir -p /mnt/dev
mount -t devfs devfs /mnt/dev

run_root() {
	chroot /mnt /bin/sh -c "$*"
}

for f in $dist_files
do
	tar -C /mnt -xJf "$dist_dir/$f"
done

echo "/dev/ada0p2 / ufs rw,noatime 1 1" >/mnt/etc/fstab
touch /mnt/firstboot
echo 'autoboot_delay="-1"' >>/mnt/boot/loader.conf

cat >>/mnt/etc/rc.conf <<EOF
ntpd_enable=YES
sshd_enable=YES
growfs_enable=YES
ifconfig_em0="inet 10.0.2.15 netmask 255.255.255.0"
defaultrouter="10.0.2.2"
EOF
echo "nameserver 1.1.1.1" >/mnt/etc/resolv.conf
tzsetup -s -C /mnt UTC

cat >>/mnt/etc/ssh/sshd_config <<EOF
PermitRootLogin yes
PasswordAuthentication yes
PermitEmptyPasswords yes
EOF

mkdir -p /mnt/usr/local/etc/pkg/repos/
cat >/mnt/usr/local/etc/pkg/repos/FreeBSD.conf <<EOF
FreeBSD: {
	url: pkg+http://pkg.FreeBSD.org/\$\{ABI\}/latest
	enabled: yes
}
EOF

/usr/sbin/freebsd-update -b /mnt \
	--currently-running 12.0-RELEASE \
	--not-running-from-cron \
	fetch install \
	>/dev/null

env ASSUME_ALWAYS_YES=YES pkg -c /mnt bootstrap -f
# TODO: remove bash
env ASSUME_ALWAYS_YES=YES pkg -c /mnt install git bash sudo curl

echo "build:1000:::::::/usr/local/bin/bash:" | run_root adduser -w none -f -
run_root pw groupadd sudo
run_root pw groupmod sudo -M build
echo "%sudo ALL=(ALL) NOPASSWD: ALL" >>/mnt/usr/local/etc/sudoers

trap - EXIT
cleanup
trap : EXIT

qemu-img convert -f raw -O qcow2 root.img.raw root.img.qcow2
rm root.img.raw
qemu-img convert -f raw -O qcow2 disk.img root.img.qcow2
rm disk.img

# Filesystem will be enlarged by growfs(7) on next startup
qemu-img resize root.img.qcow2 16G
diff --git a/images/freebsd/install b/images/freebsd/install
deleted file mode 100644
index f9565df..0000000
--- a/images/freebsd/install
@@ -1,35 +0,0 @@
PARTITIONS="md0 GPT { 512K freebsd-boot, 2G freebsd-swap, auto freebsd-ufs / }"
DISTRIBUTIONS="base.txz kernel.txz ports.txz"

#!/bin/sh -eux

sysrc ntpd_enable=YES
sysrc sshd_enable=YES
sysrc growfs_enable=YES
sysrc 'ifconfig_em0="inet 10.0.2.15 netmask 255.255.255.0"'
sysrc 'defaultrouter="10.0.2.2"'
echo "nameserver 1.1.1.1" >/etc/resolv.conf
echo 'autoboot_delay="-1"' >>/boot/loader.conf

# Device names at runtime are different from the ones at install time
sed -e s:/dev/md0:/dev/ada0: -i .orig /etc/fstab

# Run firstboot scripts on next startup
touch /firstboot

cat <<EOF >>/etc/ssh/sshd_config
PermitRootLogin yes
PasswordAuthentication yes
PermitEmptyPasswords yes
EOF

ln -sf /usr/share/zoneinfo/UTC /etc/localtime

# TODO: remove bash
pkg install -y git bash sudo curl

echo "build:1000:::::::/usr/local/bin/bash:" | adduser -w none -f -
pw groupadd sudo
pw groupmod sudo -M build

echo "%sudo ALL=(ALL) NOPASSWD: ALL" >>/usr/local/etc/sudoers
-- 
2.20.1