Skip to content

Commit

Permalink
raspian -> Raspberry Pi OS && Added run-in-docker.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
meeDamian committed May 29, 2020
1 parent e8a2c66 commit d3af7a2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 50 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:

jobs:
check:
name: Check new Raspbian Release
name: Check new Raspberry Pi OS Release
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -37,11 +37,11 @@ jobs:
if: env.VERSION != ''
run: docker build -t builder .

- name: Modify Raspbian image
- name: Modify Raspberry Pi OS image
if: env.VERSION != ''
run: |
mkdir images/
docker run --rm --privileged -v="$(pwd)/images/:/raspbian/" builder
docker run --rm --privileged -v="$(pwd)/images/:/raspios/" builder
echo ::set-env name=FILE::"$(cd images && ls *-firstboot.zip)"
Expand All @@ -61,14 +61,14 @@ jobs:
images/${{ env.FILE }}.sha256
body: >
This release adds [`firstboot.sh`](/firstboot.service#L8) capabilities to
the original Raspbian Lite image: [${{ env.SRC_FILE }}](${{ env.SRC_URL }}).
the original Raspberry Pi OS Lite image: [${{ env.SRC_FILE }}](${{ env.SRC_URL }}).
- name: Comment
if: steps.release.outputs.release_id != ''
env:
URL: https://api.github.com/repos/${{ github.repository }}/issues
body: >
New Raspbian Lite release detected. It has been modified with `firstboot.service`, and pre-released as https://github.com/${{ github.repository }}/releases/tag/${{ env.VERSION }}.
New Raspberry Pi OS Lite release detected. It has been modified with `firstboot.service`, and pre-released as https://github.com/${{ github.repository }}/releases/tag/${{ env.VERSION }}.
Please verify, [everything checks out](https://github.com/${{ github.repository }}/runs/${{ github.run_id }}?check_suite_focus=true), and upgrade `Pre-release` to `Release`.
run: |
Expand Down
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ RUN apt-get update && \
ADD modify-image.sh /usr/local/bin/modify-image
RUN chmod +x /usr/local/bin/modify-image

RUN mkdir -p /mnt/raspbian/ /data/
RUN mkdir -p /mnt/raspios/ /data/
ADD firstboot.service firstboot-script.service /data/

VOLUME /raspbian/
WORKDIR /raspbian/
VOLUME /raspios/
WORKDIR /raspios/

ENTRYPOINT ["/usr/local/bin/modify-image"]
CMD ["create", "/raspbian/"]
CMD ["create", "/raspios/"]
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Raspbian
=========
Raspberry Pi OS
================

Literally just pure Raspbian Lite, but with added the ability to run a script on the first boot by putting it onto `/boot/` as either:
Literally just pure Raspberry Pi OS Lite, but with added the ability to run a script on the first boot by putting it onto `/boot/` as either:

* `/boot/firstboot.sh` - Run provided script directly
* `/boot/firstboot-script.sh` - Run via [`script(1)`][script] for complete session recording, that can be later played back using [`scriptreplay(1)`][replay]
Expand All @@ -18,24 +18,28 @@ There are 4 ways to get the image:

The easiest way is going to [Releases], and downloading the latest one.

Releases are created automatically upon each new Raspbian release, you can see their build log either directly in [Actions tab][actions], or by searching for [`release-pending-approval`][issues] issues.
Releases are created automatically upon each new Raspberry Pi OS release, you can see their build log either directly in [Actions tab][actions], or by searching for [`release-pending-approval`][issues] issues.

[Releases]: https://github.com/meeDamian/raspbian/releases
[actions]: https://github.com/meeDamian/raspbian/actions
[issues]: https://github.com/meeDamian/raspbian/issues?q=is%3Aissue+sort%3Aupdated-desc+label%3Arelease-pending-approval+
[Releases]: https://github.com/meeDamian/raspios/releases
[actions]: https://github.com/meeDamian/raspios/actions
[issues]: https://github.com/meeDamian/raspios/issues?q=is%3Aissue+sort%3Aupdated-desc+label%3Arelease-pending-approval+


### 2. Docker

Second easiest path is (after cloning this repo) running:

1. [`docker build -t builder .`][docker-build]
1. [`docker run --rm --privileged -v="$(pwd)/images/:/raspbian/" builder`][docker-run]
1. [`docker run --rm --privileged -v="$(pwd)/images/:/raspios/" builder`][docker-run]

[docker-build]: https://github.com/meeDamian/raspbian/blob/731a1681e0f9dd9ba8b02b810bb473c286b405e7/.github/workflows/release.yml#L34
[docker-run]: https://github.com/meeDamian/raspbian/blob/731a1681e0f9dd9ba8b02b810bb473c286b405e7/.github/workflows/release.yml#L40
[docker-build]: https://github.com/meeDamian/raspios/blob/731a1681e0f9dd9ba8b02b810bb473c286b405e7/.github/workflows/release.yml#L34
[docker-run]: https://github.com/meeDamian/raspios/blob/731a1681e0f9dd9ba8b02b810bb473c286b405e7/.github/workflows/release.yml#L40

> **NOTE:** `--privileged` flag is required because [`mount`]ing a filesystem requires root.
>
>**NOTE_2:** Alternatively [`./run-in-docker.sh`][run] can be run to achieve the same effect.
[run]: /run-in-docker.sh


### 3. Script
Expand All @@ -48,12 +52,12 @@ If you're on a Linux box, you can (after cloning this repo) run:

> **NOTE:** `sudo` might be required because [`mount`]ing a filesystem requires root.
[`mount`]: https://github.com/meeDamian/raspbian/blob/master/modify-image.sh#L166
[`mount`]: https://github.com/meeDamian/raspios/blob/master/modify-image.sh#L166


### 4. Manual

You can also completely ignore all contents of this repo, download Raspbian Lite, and (assuming you have the ability to mount `ext4` on your OS):
You can also completely ignore all contents of this repo, download Raspberry Pi OS Lite, and (assuming you have the ability to mount `ext4` on your OS):

> **NOTE: For `firstboot-script.service` see [here].**
Expand Down
64 changes: 35 additions & 29 deletions modify-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ show_help() {
cat << EOF >&2
modify-image.sh v1.0.1
Modify Raspbian Lite image to recognize, and run 'firstboot.sh' placed in '/boot/'.
Modify Raspberry Pi OS Lite image to recognize, and run 'firstboot.sh' placed in '/boot/'.
Usage: ./modify-image.sh COMMAND
./modify-image.sh create [DIR] [URL]
Expand All @@ -16,7 +16,7 @@ Where COMMAND is one of: help, url, version.
The 'create' COMMAND goal is:
0. Change to DIR (if specified)
1. Download most recent Raspbian Lite image
1. Download most recent Raspberry Pi OS Lite image
(unless direct URL to another release specified)
2. Modify that image with:
3. Install /etc/systemd/system/firstboot.service
Expand All @@ -30,14 +30,14 @@ For the exact explanation 'cat' this file and read top ⬇ bottom :).
Examples:
./modify-image.sh help # Shows the very thing you're reading
./modify-image.sh version # Fetches, and returns latest Raspbian version
./modify-image.sh create # Create firstboot flavor of Raspbian in current directory
./modify-image.sh create /tmp # Create firstboot flavor of Raspbian in /tmp
./modify-image.sh version # Fetches, and returns latest Raspberry Pi OS version
./modify-image.sh create # Create firstboot flavor of Raspberry Pi OS in current directory
./modify-image.sh create /tmp # Create firstboot flavor of Raspberry Pi OS in /tmp
# And to create release of ex. Raspbian Lite dated 2017-04-10 in /tmp, run:
# And to create release of ex. Raspberry Pi OS Lite dated 2017-04-10 in /tmp, run:
./modify-image.sh create /tmp https://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-04-10/2017-04-10-raspbian-jessie-lite.zip
github: github.com/meeDamian/raspbian/
github: github.com/meeDamian/raspios/
EOF
}
Expand Down Expand Up @@ -67,14 +67,14 @@ if [ -n "$missing" ]; then
fi

# This link always redirects to latest release
LATEST_RASPBIAN="https://downloads.raspberrypi.org/raspbian_lite_latest"
LATEST_RASPIOS="https://downloads.raspberrypi.org/raspios_lite_armhf_latest"

# Uncomment below, if you prefer Raspbian Desktop over Lite
#LATEST_RASPBIAN="https://downloads.raspberrypi.org/raspbian_latest"
# Uncomment below, if you prefer Raspberry Pi OS Desktop over Lite
#LATEST_RASPIOS="https://downloads.raspberrypi.org/raspios_armhf_latest"

# Return direct URL to the latest Raspbian image
# Return direct URL to the latest Raspberry Pi OS image
get_last_url() {
curl -ILs -o /dev/null -w "%{url_effective}" "$LATEST_RASPBIAN"
curl -ILs -o /dev/null -w "%{url_effective}" "$LATEST_RASPIOS"
}

# Exit error, if unable to get direct URL to latest release
Expand All @@ -83,7 +83,7 @@ if ! URL="$(get_last_url)"; then
exit 1
fi

# Exit after returning URL to Raspbian's latest release
# Exit after returning URL to Raspberry Pi OS's latest release
if [ "$1" = "url" ]; then
echo "$URL"
exit 0
Expand All @@ -97,7 +97,7 @@ extract_version() {
extract_filename "$1" | grep -Eo '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}'
}

# Exit after returning Raspbian's latest version
# Exit after returning Raspberry Pi OS's latest version
if [ "$1" = "version" ]; then
extract_version "$URL"
exit 0
Expand All @@ -121,21 +121,21 @@ fi
log_ok


# Start a subshell to prevent script from changing directory
# Start a subshell to avoid change-directory side-effect
(
# Change dir to $2, if it's passed and is not a URL
# Change dir to $2, if passed and not URL
if [ -n "$2" ] && [ "${2#http*://}" = "$2" ]; then
cd "$2"; shift
fi

# Use URL, if provided
# Use specific URL, if provided
if [ -n "$2" ] && [ "${2#http*://}" != "$2" ]; then
URL="$2"
fi

# Extract .zip filename from the URL
original_zip="$(extract_filename "$URL")"

# Extract filename.zip from the URL
original_zip="$(extract_filename "$URL")"

log "Downloading" "$original_zip to $(pwd)"
if ! wget -cq "$URL" "$URL.sig" "$URL.sha256"; then
Expand All @@ -152,8 +152,8 @@ log_ok
fi
log_ok "Checksum ok"

raspbian_key="54C3DD610D9D1B4AF82A37758738CD6B956F460C"
if ! gpg --keyserver keyserver.ubuntu.com --recv-keys "$raspbian_key"; then
raspios_key="54C3DD610D9D1B4AF82A37758738CD6B956F460C"
if ! gpg --keyserver keyserver.ubuntu.com --recv-keys "$raspios_key"; then
log_err "Unable to fetch GPG key"
exit 1
fi
Expand All @@ -166,31 +166,37 @@ log_ok
log_ok "Signature valid"


# Change extension from .zip to .img
# Create temporary DIR, and make sure it's removed upon EXIT
temp_dir="$(mktemp -d)"
# shellcheck disable=SC2064
trap "rm -rf $temp_dir" EXIT

# Get extracted name & full, temporary path to extraced image
original_img="${original_zip%.zip}.img"
temp_img="$temp_dir/$original_img"

log "Inflating" "$original_zip"
if ! out="$(unzip -n "$original_zip")"; then
if ! out="$(unzip -n "$original_zip" -d "$temp_dir/")"; then
log_err "$out"
exit 1
fi
log_ok "$original_img created"
log_ok "$original_zip extracted to $temp_dir"


log "Scanning image" "$original_img"
startsector="$(file "$original_img" | grep -Eo 'startsector [[:digit:]]+' | cut -d' ' -f2 | sort -nr | head -n1)"
startsector="$(file "$temp_img" | grep -Eo 'startsector [[:digit:]]+' | cut -d' ' -f2 | sort -nr | head -n1)"
if [ -z "$startsector" ]; then
echo "Can't find start sector of the last partition…"
exit 1
fi
log_ok "Start sector: $startsector"


mount_dir=/mnt/raspbian
mount_dir=/mnt/raspios
mkdir -p "$mount_dir"

log "Mounting" "$original_img at $mount_dir"
if ! out="$(mount -o "loop,offset=$((startsector * 512))" "$original_img" "$mount_dir")"; then
if ! out="$(mount -o "loop,offset=$((startsector * 512))" "$temp_img" "$mount_dir")"; then
echo "Unable to mount: $out"
return 1
fi
Expand Down Expand Up @@ -227,8 +233,8 @@ log_ok

firstboot_img="${original_img%.img}-firstboot.img"

log "Renaming" "$original_img"
mv "$original_img" "$firstboot_img"
log "Renaming & moving" "$original_img"
mv "$temp_img" "./$firstboot_img"
log_ok "Renamed to $firstboot_img"


Expand Down
11 changes: 11 additions & 0 deletions run-in-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh

set -e

export DOCKER_BUILDKIT=1

TAG=raspios-firstboot

docker build --progress=plain --tag "$TAG" .

docker run --privileged --rm --volume="$(pwd)/images:/raspios/" "$TAG"

0 comments on commit d3af7a2

Please sign in to comment.