From 2647a7b46bc7a838aa1061c60748e53b05894cdc Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Wed, 24 May 2017 18:58:46 +0200 Subject: [PATCH] Initial version Signed-off-by: Knut Ahlers --- README.md | 23 ++++++++++++ flash.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++ interfaces.txt | 12 +++++++ pubkey.txt | 1 + 4 files changed, 131 insertions(+) create mode 100644 README.md create mode 100755 flash.sh create mode 100644 interfaces.txt create mode 100644 pubkey.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..11c68eb --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Luzifer / flashpi + +This repository contains a small helper script I built to aid myself with preparing SDCards for a Raspberry PI. There is no big magic in it and you can do everything done in here by hand but this way it's more convenient... + +## What is done? + +- Download latest raspbian-lite image (optional, existing image is detected) +- Flash image to SDCard +- Resize partition to fill the whole card +- Write `/etc/network/interfaces` file to enable WiFi with preconfigured password (see `interfaces.txt`) +- Write SSH key (see `pubkey.txt`) +- Enable SSHd on the PI + +## Usage + +The script will only work on Linux machines using the `root` user! + +- Insert SDCard and find its mount point using `fdisk -l` +- Edit `interfaces.txt` and `pubkey.txt` +- Start flash process: + ```bash + ./flash.sh + ``` diff --git a/flash.sh b/flash.sh new file mode 100755 index 0000000..342622c --- /dev/null +++ b/flash.sh @@ -0,0 +1,95 @@ +#!/bin/bash + +set -xe + +SDCARD_DEVICE=$2 +SOURCE_IMAGE=${SOURCE_IMAGE:-none} +STORAGE_DIR=${STORAGE_DIR:-/tmp} +SYSTEM_NAME=$1 + +if ( test "$(uname)" != "Linux" ); then + echo "This script will only work on Linux. OSX is not capable of writing ext4 filesystems" + exit 1 +fi + +if ( test "$(whoami)" != "root" ); then + echo "Please execute me as user root or using sudo for flashing / mounting SDCARD" + exit 1 +fi + +if [ -z "$SYSTEM_NAME" ]; then + echo "You need to provide a system name!" + exit 1 +fi + +if [ -z "$SDCARD_DEVICE" ]; then + echo "You need to provide a device to modify!" + exit 1 +fi + +if ! [ -f "${SOURCE_IMAGE}" ]; then + echo "Did not find SOURCE_IMAGE, downloading / detecting once" + if ( test $(ls -1tr ${STORAGE_DIR}/*raspbian-jessie-lite.img | wc -l) -lt 1 ); then + curl -L https://downloads.raspberrypi.org/raspbian_lite_latest -o /tmp/raspbian.zip + unzip /tmp/raspbian.zip -d ${STORAGE_DIR} + rm /tmp/raspbian.zip + fi + SOURCE_IMAGE=$(ls -1tr ${STORAGE_DIR}/*raspbian-jessie-lite.img | head -n1) +fi + +echo "Flashing Raspian image" +dd if=${SOURCE_IMAGE} of=${SDCARD_DEVICE} bs=1048576 + +echo "Adjusting root filesystem" +PART_START=$(parted ${SDCARD_DEVICE} -ms unit s p | grep "^2" | cut -f 2 -d: | sed "s/s$//") +[ "$PART_START" ] || return 1 + +fdisk ${SDCARD_DEVICE} < etc/hostname + +echo "Configuring SSH-key" +mkdir -p home/pi/.ssh +cp "${HERE}/pubkey.txt" home/pi/.ssh/authorized_keys + +echo "Fixing permissions" +chown -R 1000:1000 home/pi/.ssh/ +chmod 0700 home/pi/.ssh +chmod 0600 home/pi/.ssh/authorized_keys + +echo "Enabling SSH" +ln -s /lib/systemd/system/ssh.socket etc/systemd/system/sockets.target.wants/ssh.socket + +echo "Unmounting" +cd ~ +umount /tmp/pi +rmdir /tmp/pi + +echo "Done." diff --git a/interfaces.txt b/interfaces.txt new file mode 100644 index 0000000..895d4a6 --- /dev/null +++ b/interfaces.txt @@ -0,0 +1,12 @@ +auto lo +iface lo inet loopback +iface eth0 inet dhcp + +allow-hotplug wlan0 +auto wlan0 +iface wlan0 inet dhcp + wpa-ssid "my-wifi" + wpa-psk "topsecretpassword" + + +iface default inet dhcp diff --git a/pubkey.txt b/pubkey.txt new file mode 100644 index 0000000..5272a8c --- /dev/null +++ b/pubkey.txt @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHzwqMqPrIgiOfgkTw35cVVwihnWhjFLVoXY7PZMGSdO luzifer@knut-ws01