mirror of
https://github.com/0x1d/terraform-docker-packer.git
synced 2025-12-14 14:01:29 +01:00
Add firstboot provisioning
This commit is contained in:
4
README.md
Normal file
4
README.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# PI Bakery
|
||||
|
||||
## Firstboot
|
||||
Adopted from https://github.com/nmcclain/raspberian-firstboot
|
||||
32
ctl.sh
32
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}
|
||||
|
||||
@@ -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",
|
||||
|
||||
7
rootfs/lib/firstboot.sh
Executable file
7
rootfs/lib/firstboot.sh
Executable file
@@ -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"
|
||||
14
rootfs/lib/systemd/system/firstboot.service
Normal file
14
rootfs/lib/systemd/system/firstboot.service
Normal file
@@ -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
|
||||
@@ -1,4 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# enable ssh
|
||||
touch /boot/ssh.txt
|
||||
echo 'pi:$6$c70VpvPsVNCG0YR5$l5vWWLsLko9Kj65gcQ8qvMkuOoRkEagI90qi3F/Y7rm8eNYZHW8CY6BOIKwMH7a3YYzZYL90zf304cAHLFaZE0' > /boot/userconf.txt
|
||||
|
||||
# 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 .
|
||||
Reference in New Issue
Block a user