Skip to content

Instantly share code, notes, and snippets.

@kafeg
Created January 12, 2022 22:18
Show Gist options
  • Save kafeg/93d8057cf32e249d91da332449016eb4 to your computer and use it in GitHub Desktop.
Save kafeg/93d8057cf32e249d91da332449016eb4 to your computer and use it in GitHub Desktop.
Shell script for modify dockerpi image and install there required packages on first boot.
#/bin/bash
#set -x
#ZIP_URL="http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2015-11-24/2015-11-21-raspbian-jessie-lite.zip"
#ZIP_NAME="2015-11-21-raspbian-jessie-lite.zip"
#IMG_NAME="2015-11-21-raspbian-jessie-lite.img"
ZIP_URL="http://downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2019-09-30/2019-09-26-raspbian-buster-lite.zip"
ZIP_NAME="2019-09-26-raspbian-buster-lite.zip"
IMG_NAME="2019-09-26-raspbian-buster-lite.img"
MOUNT_PATH=/opt/pi-mount
LOOP_NAME="/dev/loop0" # get empty loop device
PART_NAME="/dev/loop0p2" # get /dev/loop0p2 by fdisk -l $LOOP_NAME
IMG_NAME_MOD="filesystem.img"
DMPWD=`pwd`
if [ "$EUID" -ne 0 ]
then echo "Please run with sudo"
exit
fi
if [ ! -f "$ZIP_NAME" ]
then
echo "Downloading filesystem image"
wget $ZIP_URL
fi
if [ ! -f "$IMG_NAME" ]
then
echo "Unzipping filesystem image"
unzip $ZIP_NAME
fi
chmod 777 $IMG_NAME $ZIP_NAME
FPARAM=$1
if [[ "$FPARAM" != "--force" ]]
then
if [ -f "$IMG_NAME_MOD" ]
then
echo "$IMG_NAME_MOD file already exists. You can remove it to modify again or call with --force"
exit 1
fi
fi
echo "Modifying filesystem image $IMG_NAME"
cp $IMG_NAME $IMG_NAME_MOD
if [ -d "$MOUNT_PATH/bin" ]; then umount $MOUNT_PATH; fi
if [ -f "$LOOP_NAME" ]; then losetup -d $LOOP_NAME; fi
mkdir -p $MOUNT_PATH
chmod 777 $MOUNT_PATH
losetup $LOOP_NAME $IMG_NAME_MOD
partprobe $LOOP_NAME
mount -t ext4 -o rw,sync,nosuid,nodev,relatime,uhelper=udisks2 $PART_NAME $MOUNT_PATH
if [ ! -d "$MOUNT_PATH/bin" ]
then
echo "Mount failed, exit..."
if [ -d "$MOUNT_PATH/bin" ]; then umount $MOUNT_PATH; fi
if [ -f "$LOOP_NAME" ]; then losetup -d $LOOP_NAME; fi
exit 1
fi
ls -alh $MOUNT_PATH/
if [ -f "$MOUNT_PATH/modified.flag" ]
then
echo "Already modified, exit..."
echo "Next step: docker run -it -v \`pwd\`:/sdcard/ lukechilds/dockerpi:vm"
umount $MOUNT_PATH
losetup -d $LOOP_NAME
exit 1
fi
# add service
cat <<EOT >> $MOUNT_PATH/etc/systemd/system/firstboot.service
[Unit]
Description=FirstBoot
After=network.target
Before=rc-local.service
ConditionFileNotEmpty=/firstboot.sh
[Service]
Type=oneshot
#Type=default
ExecStart=/firstboot.sh
#ExecStartPost=/bin/mv /firstboot.sh /firstboot.sh.done
RemainAfterExit=no
StandardOutput=tty
StandardError=tty
SyslogIdentifier=firstboot
[Install]
WantedBy=multi-user.target
EOT
# add script
cat <<EOT >> $MOUNT_PATH/firstboot.sh
#!/bin/bash
#set -x
sleep 15
sudo apt-get update --allow-releaseinfo-change
sudo apt-get install -y build-essential
sudo apt-get autoremove -y
sleep 30
sudo halt
EOT
chmod a+x $MOUNT_PATH/firstboot.sh
cd $MOUNT_PATH/etc/systemd/system/multi-user.target.wants/
ln -s /etc/systemd/system/firstboot.service .
cd $DMPWD
touch $MOUNT_PATH/modified.flag
umount $MOUNT_PATH
losetup -d $LOOP_NAME
sleep 1
rmdir $MOUNT_PATH
echo "Finishing..."
echo "Next step: docker run -it -v \`pwd\`:/sdcard/ lukechilds/dockerpi:vm"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment