EricGentry045
Posts: 1
Joined: Sun Jul 30, 2017 6:09 am

run a custom script at first boot after etcher burn....

Sun Jul 30, 2017 6:18 am

Hi all,

I can't seem to find anything on this topic. I keep finding stuff on "running script at boot", which is *definitely not* what I'm looking for.

I have just burned the latest raspbian jessie lite image to the sd card and I'm looking at the boot image. I've copied the empty ssh file there to turn on ssh. But, I have a rather large script that relies on being run from the /home/pi folder that does a very custom setup for a very specific project.

I am willing to do whatever it takes to have this script run as a part of the default setup, but it needs to be copied to the /home/pi folder and then run from there (presumably after that folder has been created).

Any help would be appreciated.

Eric

User avatar
rpdom
Posts: 18156
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: run a custom script at first boot after etcher burn....

Sun Jul 30, 2017 9:03 am

The "folder" (directory) is there after you have written the image. It just can't be accessed by certain limited operating systems without extra software.

I create my SD cards using a Linux PC using a script I wrote. It installs the base image, expands the filesystem (and disables the expand on first boot thing to save time), then customises a whole load of stuff for me.

RonR
Posts: 2004
Joined: Tue Apr 12, 2016 10:29 pm
Location: US

Re: run a custom script at first boot after etcher burn....

Sun Jul 30, 2017 8:43 pm

Eric,

If your objective is to distribute an SD card (or image of an SD card) that on being run the first time (and only the first time) executes your setup script, an approach I've used is:

1. Mount a virgin Raspbian-lite image's BOOT partition (using a loop device - see below)
2. Create an empty ssh file in the image's /boot directory (touch ssh)
3. Mount a virgin Raspbian-lite image's ROOT partition (using a loop device - see below)
4. Copy your setup script to the image's /home/pi directory
5. Modify the image's /etc/rc.local by adding code just prior to the last line (exit 0) that:
A. Calls your setup script in /home/pi
B. Deletes your setup script in /home/pi (if applicable)
C. Restores /etc/rc.local to its original state

Code: Select all

#!/bin/bash

# Usage: image-mount <image-filename> <W95|Linux>
# W95 = Boot Partition / Linux = Root Partition

IMG="$1"
PART="$2"

INFO="$(fdisk -lu "${IMG}")"
START="$(grep "${PART}" <<< "${INFO}" | awk '{print $2}')"
SIZE="$(grep "${PART}" <<< "${INFO}" | awk '{print $4}')"
LOOP="$(losetup -f --show -o $((${START} * 512)) --sizelimit $((${SIZE} * 512)) "${IMG}")"
mount "${LOOP}" /mnt/
echo ""
echo "${IMG} mounted on /mnt/"
echo ""
echo "When done, run:"
echo "umount /mnt/"
echo "losetup -d ${LOOP}"
echo ""

User avatar
B.Goode
Posts: 11215
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: run a custom script at first boot after etcher burn....

Sun Jul 30, 2017 9:21 pm

Maybe PiBakery would let you achieve your aim?

http://www.pibakery.org

Failing that, all you need to modify the microSD card created by Etcher is a Linux system capable of modifying an ext4 filesystem. You could do this with a conventional PC booted from the Raspberry Pi Foundation "Raspberry Pi Desktop for x86" os, or from your own RPi equipped with a card reader/writer dongle booted from a spare microSD card with a standard version of Raspbian Jessie.

mikerr
Posts: 2826
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: run a custom script at first boot after etcher burn....

Sun Jul 30, 2017 9:57 pm

Create a file called "setup.txt" on the sd card in windows ( boot partition)

Put a line like this in /etc/rc.local:

Code: Select all

if [ -e /boot/setup.txt]
then
    sh /home/pi/yourscript.sh
    rm /boot/setup.txt
fi
The above runs your script on boot only if it sees the file "setup", then deletes that file once its done.
So will only run once.

Doing it this way also means the end user can just create that (empty) setup file to reset if necessary.
Android app - Raspi Card Imager - download and image SD cards - No PC required !

Return to “Advanced users”