Skip to main content

Prepare Raspbian for headless install

Project description

pi-image-prep

PyPI version

pip install pi-image-prep

Overview

This is a script I created to help prepare Rasbian image before the first boot. My goal was to set up an image with some basic features, up to the point where I could pass off control to my configuration management system.

I was inspired to replicate the functionality of PiBakery, but I wanted something simple and lightweight that would run on Linux.

Features:

  • set system Locale
  • set system timezone
  • set user passwords for root and pi users
  • add authorized keys for the root and pi users
  • enable ssh
  • set hostname
  • enable wifi
  • install packages by name

Usage:

On most systems, this will need to be run as root, or with sudo. This is because the image partitions need to be mounted and unmounted, and most non-root users cannot do this by default.

The full usage is below. Nothing is enabled by default, unless specified in a cli flag.

The following options will enable a prompt in the script, but the values can be set via environment variables to prevent this.

  • --pi-pw: can be overridden by setting the environment variable PI_PW
  • --root-pw: can be overridden by setting the environment variable ROOT_PW
  • --wifi-ssid: This will cause a prompt for the wifi password, which can be overridden by specifing an environment variable WIFI_PASS.

Note: I chose not to make those options available via cli flags to discourage passing sensitive information to the command line.

$ pi-image-prep.py -h
usage: pi-image-prep.py [-h] [--bypass-space-checks] [--pause]
                        [--locale LOCALE] [--timezone TIMEZONE] [--root-pw]
                        [--root-keys [ROOT_KEYS [ROOT_KEYS ...]]] [--pi-pw]
                        [--pi-keys [PI_KEYS [PI_KEYS ...]]] [--enable-ssh]
                        [--hostname HOSTNAME] [--wifi-ssid WIFI_SSID]
                        [--wifi-country WIFI_COUNTRY]
                        [--install-packages [INSTALL_PACKAGES [INSTALL_PACKAGES ...]]]
                        image_file output_file

positional arguments:
  image_file
  output_file

optional arguments:
  -h, --help            show this help message and exit
  --bypass-space-checks
                        If you think you're smarter than the computer, bypass
                        the disk space checks.
  --pause               Pause for inspection before closing image
  --locale LOCALE       set the system locale, example: en_US.UTF-8
  --timezone TIMEZONE   Set the timezone name, example: America/Chicago

Authentication Options:
  --root-pw             Change root password
  --root-keys [ROOT_KEYS [ROOT_KEYS ...]]
                        Authorized key files for the root user
  --pi-pw               Change the pi user password
  --pi-keys [PI_KEYS [PI_KEYS ...]]
                        Authorized key files for the pi user

SSH Options:
  --enable-ssh          enable ssh on first boot

Network Options:
  --hostname HOSTNAME   raspberry pi hostname

WiFi Options:
  --wifi-ssid WIFI_SSID
                        wifi ssid
  --wifi-country WIFI_COUNTRY
                        2 letter ISO 3166-1 country code for wifi.

Configuration Options:
  --install-packages [INSTALL_PACKAGES [INSTALL_PACKAGES ...]]
                        packages to install on first boot

A Note on Sudo and Virtual Environments

If installed into a virtual environment, a sudo pi-image-prep.py call will mostly likely fail, since the root user would not have sourced the activate file. The easiest way to bypass this is to either install into the system python install or call the executable by full path.

Wishlist

These are features I think would be great additions to the script. I may or may not have time to add them, so contributions are always welcome:

  • SSH Host key management: either copy pre-existing keys or generate new then display an authorized_keys compabile entry to stdout.
  • Windows Compatibility: I won't use it, but I'd entertain cross compatbility features
  • Remove password for pi user
  • Use sudo with the mount/umount commands

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pi-image-prep-0.1.1.tar.gz (11.3 kB view details)

Uploaded Source

File details

Details for the file pi-image-prep-0.1.1.tar.gz.

File metadata

  • Download URL: pi-image-prep-0.1.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.6

File hashes

Hashes for pi-image-prep-0.1.1.tar.gz
Algorithm Hash digest
SHA256 83fabcda34bda4ffc5b29d838e37eeeab7572eaccc0a776deecb6d1d4a520bce
MD5 4a35d972583c4ba473813b8c77df8bd7
BLAKE2b-256 bde6f7254e79d6523aa0e842659ba2ebeffe38be97cb876568c6146e2350ea02

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page