#!/bin/bash # adapted from ezpi4me - Raspberry Pi ME image creation script # Written by Huan Truong , 2018 # This script is licensed under GNU Public License v3 setglobal IMAGE_FILE = 'raspbian-stretch-lite.zip' setglobal TODAY_EXT = $[date +"%Y-%m-%d-%H-%M] setglobal BUILD_ID = $[hexdump -n 4 -e '4/4 "%X" 1 "\n"' /dev/random] setglobal IMAGE_FILE_CUSTOMIZED = $(IMAGE:-"crankshaft-${TODAY_EXT}-${BUILD_ID}.img") setglobal IMAGE_URL = 'https://downloads.raspberrypi.org/raspbian_lite_latest' setglobal TEMP_CHROOT_DIR = '/mnt/raspbian-temp' setglobal DROP_IN = $(DROP_IN:-0) setglobal CUSTOM_SCRIPT = $(CUSTOM_SCRIPT:-"") clear echo "###########################################################################" echo "" echo " Welcome to the Crankshaft build script!" echo "" echo "###########################################################################" ######################################################### # Support functions ######################################################### proc bail_and_cleanup { kpartx -d $1 # rm $2 } proc check_command_ok { if ! test -x $[command -v $1] { echo "###########################################################################" echo "" echo "Error: $1 is not installed. Please install it." >&2 echo "" echo "###########################################################################" exit 1 } } proc check_root { # make sure we're root if test $EUID -ne 0 { echo "###########################################################################" echo "" echo "Please run this script as using sudo/as root, otherwise it can't continue. " echo "" echo "###########################################################################" exit } } proc check_dependencies { check_command_ok kpartx # check_command_ok parted check_command_ok qemu-arm-static check_command_ok chroot check_command_ok pv check_command_ok zipinfo check_command_ok zerofree } proc get_unzip_image { #get raspberry image if test -f $(IMAGE_FILE) { #check remote filze size setglobal remotesize = $[wget https://downloads.raspberrypi.org/raspbian_lite_latest --spider --server-response -O - 2>&1 | sed -ne '/Content-Length/{s/.*: //;p}] setglobal localsize = $[wc -c $(IMAGE_FILE) | awk '{print $1}] # Failsafe - if string length of remote size is to short try again (sometimes happens caused by delayed response) if test $(#remotesize) != $(#localsize) { setglobal remotesize = $[wget https://downloads.raspberrypi.org/raspbian_lite_latest --spider --server-response -O - 2>&1 | sed -ne '/Content-Length/{s/.*: //;p}] } if test $remotesize = $localsize { echo "---------------------------------------------------------------------------" echo "Image file $(IMAGE_FILE) is already the same, skipping download." echo "It will be re-downloaded if remote file has changed." echo "---------------------------------------------------------------------------" } else { #re-download cause filesize has changed echo "---------------------------------------------------------------------------" echo "Downloading new version of raspbian image from server..." wget -q --show-progress -O$(IMAGE_FILE) $(IMAGE_URL) echo "---------------------------------------------------------------------------" } } else { echo "---------------------------------------------------------------------------" echo "Downloading raspbian image from server..." wget -q --show-progress -O$(IMAGE_FILE) $(IMAGE_URL) echo "---------------------------------------------------------------------------" } setglobal IMAGE_FILE_UNZIPPED = $[zipinfo -1 $(IMAGE_FILE)] setglobal IMAGE_FILE_UNZIPPED_SIZE = $[zipinfo -l $(IMAGE_FILE) | tail -1 | xargs | cut -d' ' -f3] if ! test -f $(IMAGE_FILE_UNZIPPED) { echo "---------------------------------------------------------------------------" echo "Unpacking raspbian image..." echo "---------------------------------------------------------------------------" unzip -o -p $(IMAGE_FILE) | pv -p -s $(IMAGE_FILE_UNZIPPED_SIZE) -w 80 > ${IMAGE_FILE_UNZIPPED} } if ! test -f $(IMAGE_FILE_CUSTOMIZED) { echo "---------------------------------------------------------------------------" echo "Copying a big file..." echo "---------------------------------------------------------------------------" cp $(IMAGE_FILE_UNZIPPED) $(IMAGE_FILE_CUSTOMIZED) } else { echo "---------------------------------------------------------------------------" echo "Skipping creation of $(IMAGE_FILE_CUSTOMIZED), it's already there. To re-create, delete it." echo "---------------------------------------------------------------------------" } } proc resize_raw_image { setglobal IMAGE_SIZE_RAW = $[wc -c] setglobal IMAGE_SIZE_ACTUAL = $[wc -c] setglobal IMAGE_ROOTPART_START = $[parted $(IMAGE_FILE_UNZIPPED) unit s print -sm | tail -1 | cut -d: -f2 | sed 's/s//] if test $(IMAGE_SIZE_ACTUAL) -gt $(IMAGE_SIZE_RAW) { echo "---------------------------------------------------------------------------" echo "Image seems already resized, or something is wrong." echo "If the image doesn't work, try removing the .img and try again." echo "---------------------------------------------------------------------------" return } echo "---------------------------------------------------------------------------" echo "Resizing image..." echo "---------------------------------------------------------------------------" #resize image dd if=/dev/zero bs=1M count=512 >> ${IMAGE_FILE_CUSTOMIZED} setglobal PART_NUM = '2' fdisk $(IMAGE_FILE_CUSTOMIZED) <