Browse Source

Proof of concept for utility scripts

A full workflow would look like this:

```sh
./prepare_image.sh && ./mount.sh && sudo ./copy.sh $HOME/.ssh/id_rsa.pub && cp ./setup.sh root/tmp/ && ./chroot.sh && ./umount.sh
```

- prepare_image.sh: loads a fresh raspbian-lite, extracts it and expands
the root partition to ~3.7GB
- mount.sh: mounts the image in rootfs/
- copy.sh: copies some files into the root filesystem, e.g
wpa_supplicant.conf, authorizes SSH key
- setup.sh: updates and installs packages (must be called manually in
chroot)
- chroot.sh: opens a qemu-arm based chroot
- umount.sh: as the name says
master
Andreas Linz 2 years ago
commit
98facaaade
Signed by: alinz GPG Key ID: 9BF39809C9DA580C
7 changed files with 100 additions and 0 deletions
  1. +3
    -0
      .gitignore
  2. +8
    -0
      chroot.sh
  3. +17
    -0
      copy.sh
  4. +29
    -0
      mount.sh
  5. +24
    -0
      prepare_image.sh
  6. +13
    -0
      setup.sh
  7. +6
    -0
      umount.sh

+ 3
- 0
.gitignore View File

@ -0,0 +1,3 @@
/wpa_supplicant.conf
/*raspbian*

+ 8
- 0
chroot.sh View File

@ -0,0 +1,8 @@
#!/bin/bash
set -euo pipefail
cat <<'HEREDOC'
You are now transferred in to a qemu shell (run "exit" to quit)
HEREDOC
sudo chroot $PWD/root qemu-arm-static /bin/bash --login

+ 17
- 0
copy.sh View File

@ -0,0 +1,17 @@
#!/bin/bash
SSH_PUBLIC_KEY=${1:-~/.ssh/id_rsa.pub}
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo 'This script must be run as root!'
exit 1
fi
touch root/boot/ssh
# wpa_supplicant <ssid> <password> > wpa_supplicant.conf
cp wpa_supplicant.conf root/etc/wpa_supplicant/
mkdir -p root/home/pi/.ssh
cat "$SSH_PUBLIC_KEY" > root/home/pi/.ssh/authorized_keys
echo 'PasswordAuthentication no' >> root/etc/ssh/sshd_config

+ 29
- 0
mount.sh View File

@ -0,0 +1,29 @@
#!/bin/bash
set -euo pipefail
mkdir --parents root
IMAGE_DATE=2018-06-29
# Weird but the image is from a different date.
FILE_DATE=2018-06-27
FILENAME="${FILE_DATE}-raspbian-stretch-lite"
BOOT_START_SECTOR=$(sfdisk --json ${FILENAME}.img | jq '.partitiontable.partitions[] | select(.type == "c") | .start')
BOOT_SECTOR_SIZE=$(sfdisk --json ${FILENAME}.img | jq '.partitiontable.partitions[] | select(.type == "c") | .size')
ROOT_START_SECTOR=$(sfdisk --json ${FILENAME}.img | jq '.partitiontable.partitions[] | select(.type == "83") | .start')
[[ -z $(mount | grep "${PWD}/root") ]]\
&& sudo mount --options loop,offset=$((512*ROOT_START_SECTOR)) "${FILENAME}.img" root
[[ -z $(mount | grep "${PWD}/boot") ]]\
&& sudo mount --options loop,offset=$((512*BOOT_START_SECTOR)),sizelimit=$((512*BOOT_SECTOR_SIZE)) "${FILENAME}.img" root/boot
# https://gist.github.com/htruong/7df502fb60268eeee5bca21ef3e436eb
sudo mount --bind /dev root/dev/
sudo mount --bind /sys root/sys/
sudo mount --bind /proc root/proc/
sudo mount --bind /dev/pts root/dev/pts
sudo mount --bind /etc/resolv.conf root/etc/resolv.conf
sudo sed -i 's/^/#CHROOT /g' root/etc/ld.so.preload
sudo cp /usr/bin/qemu-arm-static root/usr/bin/

+ 24
- 0
prepare_image.sh View File

@ -0,0 +1,24 @@
#!/bin/bash
set -euo pipefail
IMAGE_DATE=2018-06-29
# Weird but the image is from a different date.
FILE_DATE=2018-06-27
FILENAME="${FILE_DATE}-raspbian-stretch-lite"
for ext in info zip zip.sig; do
[[ ! -f "${FILENAME}.${ext}" ]] && curl --remote-name --location --fail "https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-${IMAGE_DATE}/${FILENAME}.${ext}"
done
# gpg --recv-keys 8738CD6B956F460C
[[ ! -f "${FILENAME}.img" ]]\
&& gpg --quiet --verify "${FILENAME}.zip.sig" "${FILENAME}.zip"\
&& unzip "${FILENAME}.zip"
BOOT_START_SECTOR=$(sfdisk --json ${FILENAME}.img | jq '.partitiontable.partitions[] | select(.type == "c") | .start')
ROOT_START_SECTOR=$(sfdisk --json ${FILENAME}.img | jq '.partitiontable.partitions[] | select(.type == "83") | .start')
sfdisk --delete "${FILENAME}.img" 2
head --bytes=2G /dev/zero | pv --size=2G >> "${FILENAME}.img"
echo "start=$ROOT_START_SECTOR type=83" | sfdisk "${FILENAME}.img" 2

+ 13
- 0
setup.sh View File

@ -0,0 +1,13 @@
#!/bin/bash
set -euo pipefail
locale-gen
timedatectl set-timezone Europe/Berlin
apt update
apt upgrade -y
apt dist-upgrade -y
apt install -y docker vim git

+ 6
- 0
umount.sh View File

@ -0,0 +1,6 @@
#!/bin/bash
set -euo pipefail
sudo sed -i 's/^#CHROOT //g' root/etc/ld.so.preload
sudo umount root/{dev/pts,dev,etc/resolv.conf,sys,proc,boot,}

Loading…
Cancel
Save