Skip to content

Instantly share code, notes, and snippets.

@camerahacks
Last active May 30, 2023 10:44
Show Gist options
  • Save camerahacks/bd1072d1c035380d09db7b6a3cbcd38b to your computer and use it in GitHub Desktop.
Save camerahacks/bd1072d1c035380d09db7b6a3cbcd38b to your computer and use it in GitHub Desktop.
CM4 Provision System Postinstall Script

CM4 Provision System Postinstall Script

{DPHacks} a website for everything camera hack related.

Postinstall script to be used with Raspberry Pi's CM Provisioning System (https://github.com/raspberrypi/cmprovision)

This script has been tested with Raspberry Pi OS Bullseye edition realeased April 8th 2022 and after.

It takes advantage of the user configuration file.

Instructions

Replace fields surrounded by <> with the values you want assigned during provisioning.

Copy and paste the script below as a postinstall script.

Notable settings

Script will overclock CM4 to 2GHz with an over_voltage value of 6.

Script will enable USB host mode.

Script will enable ssh.

Script will configure the America/Chicago timezone and set the keyboard layout to US. Change this as appropriate.

Update wpa_supplicant info

Replace <SSID> with your network's SSID.
Replace <SSID encrypted psk> with the encrypted passphrase. Type the command below in terminal in a different linux machine (like a Raspberry Pi)

wpa_passphrase <SSID> <SSID Passphrase>

Replace <country> with the two letter ISO country code of your choice.

Update user config

Replace <user> and <hashed password> with the desired values. Type the command below to get the hashed password value.

echo 'mypassword' | openssl passwd -6 -stdin

Hostname info

The script is configured to use the Compute Module's serial number as the hostname. Feel free to change this as needed.

Postinstall script

#!/bin/sh
set -e

#boot partition files
mkdir -p /mnt/boot
mount -t vfat $PART1 /mnt/boot
sed -i "/[CM4].*/a arm_freq=2000\nover_voltage=6" /mnt/boot/config.txt
sed -i "/[CM4].*/a dtoverlay=dwc2,dr_mode=host" /mnt/boot/config.txt

touch /mnt/boot/ssh

echo '<user>:<hashed password>' > /mnt/boot/userconf

cat >/mnt/boot/wpa_supplicant.conf <<'WPAEOF'
country=<country>
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
ap_scan=1

update_config=1
network={
	ssid="<SSID>"
	psk=<SSID encrypted psk>
}

WPAEOF

umount /mnt/boot

#root partition
mkdir -p /mnt/root
mount -t ext4 $PART2 /mnt/root

new_host=$SERIAL

echo $new_host >/mnt/root/etc/hostname
sed -i "s/127.0.1.1.*raspberrypi/127.0.1.1\t${new_host}/g" /mnt/root/etc/hosts
echo "America/Chicago" >/mnt/root/etc/timezone

cat >/mnt/root/etc/default/keyboard <<'KBEOF'
XKBMODEL="pc105"
XKBLAYOUT="us"
XKBVARIANT=""
XKBOPTIONS=""

KBEOF

umount /mnt/root
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment