diff --git a/README.md b/README.md new file mode 100644 index 0000000..b673075 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# PI Bakery + +## Firstboot +Adopted from https://github.com/nmcclain/raspberian-firstboot \ No newline at end of file diff --git a/ctl.sh b/ctl.sh index 72a94a7..2f4f23e 100755 --- a/ctl.sh +++ b/ctl.sh @@ -1,17 +1,45 @@ #!/usr/bin/env bash function info { - echo "PI-Lab Utils" + echo "Lab Utils" } function build { function image { + local image_config=${1:-$(ls packer | fzf)} + [[ -z "$image_config" ]] && die "Config not found" docker run --rm --privileged \ -v /dev:/dev \ -v ${PWD}:/build mkaczanowski/packer-builder-arm:latest \ - build packer/raspios.pkr.hcl + build packer/${image_config} } ${@:-info} } +function flash { + local input_file=${1:-raspios-arm64.img} + local output_file=${2:-$(lsblk -d -o NAME | fzf --height=10 --prompt="Select a block device: " | sed 's/^/\/dev\//')} + + [[ -z "$input_file" ]] && die "No device image selected." + [[ -z "$output_file" ]] && die "No block device selected." + + confirm "Flash ${input_file} to ${output_file}?" && { + echo "Proceeding..." + sudo dd if=${input_file} of=${output_file} bs=4M status=progress oflag=sync + } || { + echo "Aborting." + exit 1 + } +} + +function confirm { + read -p "$1 (Y/N): " -n 1 -r; echo + [[ $REPLY =~ ^[Yy]$ ]] +} + +function die { + echo "$1" >&2 + exit 1 +} + ${@:-info} diff --git a/packer/raspios.pkr.hcl b/packer/raspios.pkr.hcl index 8c92ced..c437ef4 100644 --- a/packer/raspios.pkr.hcl +++ b/packer/raspios.pkr.hcl @@ -11,7 +11,7 @@ source "arm" "raspios-arm64" { file_unarchive_cmd = ["xz", "--decompress", "$ARCHIVE_PATH"] image_build_method = "resize" image_path = "raspios-arm64.img" - image_size = "6G" + image_size = "4G" image_type = "dos" image_partitions { @@ -39,6 +39,17 @@ source "arm" "raspios-arm64" { build { sources = ["source.arm.raspios-arm64"] + + provisioner "file" { + source = "rootfs/lib/systemd/system/firstboot.service" + destination = "/lib/systemd/system/firstboot.service" + } + + provisioner "file" { + source = "rootfs/lib/firstboot.sh" + destination = "/lib/firstboot.sh" + } + provisioner "shell" { scripts = [ "scripts/pi.sh", diff --git a/rootfs/lib/firstboot.sh b/rootfs/lib/firstboot.sh new file mode 100755 index 0000000..ac10ba4 --- /dev/null +++ b/rootfs/lib/firstboot.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +# Generate a unique hostname +NEW_HOSTNAME="rpi-$(cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2 | tail -c 5)" +echo "$NEW_HOSTNAME" > /etc/hostname +sed -i "s/raspberrypi/$NEW_HOSTNAME/g" /etc/hosts +hostname "$NEW_HOSTNAME" \ No newline at end of file diff --git a/rootfs/lib/systemd/system/firstboot.service b/rootfs/lib/systemd/system/firstboot.service new file mode 100644 index 0000000..f4fad34 --- /dev/null +++ b/rootfs/lib/systemd/system/firstboot.service @@ -0,0 +1,14 @@ +[Unit] +Description=FirstBoot +After=network.target apt-daily.service apt-daily-upgrade.service +Before=rc-local.service +ConditionFileNotEmpty=/lib/firstboot.sh + +[Service] +ExecStart=/bin/bash /lib/firstboot.sh +ExecStartPost=/bin/mv /lib/firstboot.sh /lib/firstboot.sh.done +Type=oneshot +RemainAfterExit=no + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/scripts/pi.sh b/scripts/pi.sh index ff81905..1ebca14 100644 --- a/scripts/pi.sh +++ b/scripts/pi.sh @@ -1,4 +1,10 @@ #!/usr/bin/env bash +# enable ssh touch /boot/ssh.txt -echo 'pi:$6$c70VpvPsVNCG0YR5$l5vWWLsLko9Kj65gcQ8qvMkuOoRkEagI90qi3F/Y7rm8eNYZHW8CY6BOIKwMH7a3YYzZYL90zf304cAHLFaZE0' > /boot/userconf.txt \ No newline at end of file + +# configure user +echo 'pi:$6$c70VpvPsVNCG0YR5$l5vWWLsLko9Kj65gcQ8qvMkuOoRkEagI90qi3F/Y7rm8eNYZHW8CY6BOIKwMH7a3YYzZYL90zf304cAHLFaZE0' > /boot/userconf.txt + +# enable firstboot.service +cd /etc/systemd/system/multi-user.target.wants && ln -s /lib/systemd/system/firstboot.service . \ No newline at end of file