3 2

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

Details
Message ID
<RPYRyW0DVyGvcu5a_OJwc7y8tlZvTJClG-2UdtYvuIBpMtCMJz2CUwykVXukm4n129WBgmnqsd7qNrnGKMpIP00JpX_PmDPyucXT7lZ4FNM=@emersion.fr>
DKIM signature
pass
Download raw message
Patch: +78 -64
From: emersion <contact@emersion.fr>

---

$snapshot_base hardcodes 13.0-CURRENT because the $release format is different
and these are snapshots any, so ports.txz is the same.

 images/freebsd/12.0/genimg |   2 +-
 images/freebsd/genimg      | 105 +++++++++++++++++++++++++++----------
 images/freebsd/install     |  35 -------------
 3 files changed, 78 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..3b6b5fe 100755
--- a/images/freebsd/genimg
+++ b/images/freebsd/genimg
@@ -3,42 +3,91 @@
 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"
+snapshot_base="https://download.freebsd.org/ftp/snapshots/$arch/13.0-CURRENT"
+dist_files="kernel.txz base.txz"
+dist_dir="/usr/freebsd-dist/$arch/$release"
+
+mkdir -p "$dist_dir"
+for f in $dist_files
+do
+	fetch -m -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 || 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 6G 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 5G md0
+newfs -U /dev/md0p2
+
+mount /dev/md0p2 /mnt
+
+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
+# Subversion is required for ports
+env ASSUME_ALWAYS_YES=YES pkg -c /mnt install git bash sudo curl subversion
+
+fetch -m -o "$dist_dir/ports.txz" "$snapshot_base/ports.txz"
+tar -C /mnt -xJf "$dist_dir/ports.txz"
+
+pw -R /mnt groupadd sudo
+pw -R /mnt useradd -n build -u 1000 -s /usr/local/bin/bash -m -w none -G sudo
+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
+++ /dev/null
@@ -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
Details
Message ID
<20190211165553.GA11779@homura.localdomain>
In-Reply-To
<RPYRyW0DVyGvcu5a_OJwc7y8tlZvTJClG-2UdtYvuIBpMtCMJz2CUwykVXukm4n129WBgmnqsd7qNrnGKMpIP00JpX_PmDPyucXT7lZ4FNM=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On 2019-02-11  4:38 PM, Simon Ser wrote:
> -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

Can we also resize the disk to make it a bit roomier? Say, 16G.
Details
Message ID
<4lzHjJly296lH-U3OOsVy0W2wzg3iI5DlYul7I_9AB7n0WyI60qf35NV1nDZfezLKQmEGfzqNyyaIZXF9LPfFyXWau3M5ghvkhkWCgNjGh4=@emersion.fr>
In-Reply-To
<20190211165553.GA11779@homura.localdomain> (view parent)
DKIM signature
pass
Download raw message
On Monday, February 11, 2019 5:55 PM, Drew DeVault <sir@cmpwn.com> wrote:
> On 2019-02-11 4:38 PM, Simon Ser wrote:
>
> > -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
>
> Can we also resize the disk to make it a bit roomier? Say, 16G.

This was already done previously, right after this line.

    qemu-img resize root.img.qcow2 16G
Details
Message ID
<20190211165832.GE1614@homura.localdomain>
In-Reply-To
<4lzHjJly296lH-U3OOsVy0W2wzg3iI5DlYul7I_9AB7n0WyI60qf35NV1nDZfezLKQmEGfzqNyyaIZXF9LPfFyXWau3M5ghvkhkWCgNjGh4=@emersion.fr> (view parent)
DKIM signature
pass
Download raw message
On 2019-02-11  4:57 PM, Simon Ser wrote:
> This was already done previously, right after this line.
> 
>     qemu-img resize root.img.qcow2 16G

Nice