#!/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 IMAGE_FILE=raspbian-stretch-lite.zip TODAY_EXT=$(date +"%Y-%m-%d-%H-%M") BUILD_ID=$(hexdump -n 4 -e '4/4 "%X" 1 "\n"' /dev/random) IMAGE_FILE_CUSTOMIZED=${IMAGE:-"crankshaft-${TODAY_EXT}-${BUILD_ID}.img"} IMAGE_URL=https://downloads.raspberrypi.org/raspbian_lite_latest TEMP_CHROOT_DIR=/mnt/raspbian-temp DROP_IN=${DROP_IN:-0} CUSTOM_SCRIPT=${CUSTOM_SCRIPT:-""} clear echo "###########################################################################" echo "" echo " Welcome to the Crankshaft build script!" echo "" echo "###########################################################################" ######################################################### # Support functions ######################################################### bail_and_cleanup() { kpartx -d $1 # rm $2 } check_command_ok() { if ! [ -x "$(command -v $1)" ]; then echo "###########################################################################" echo "" echo "Error: $1 is not installed. Please install it." >&2 echo "" echo "###########################################################################" exit 1 fi } check_root() { # make sure we're root if [ "$EUID" -ne 0 ]; then echo "###########################################################################" echo "" echo "Please run this script as using sudo/as root, otherwise it can't continue. " echo "" echo "###########################################################################" exit fi } 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 } get_unzip_image() { #get raspberry image if [ -f ${IMAGE_FILE} ]; then #check remote filze size remotesize=`wget https://downloads.raspberrypi.org/raspbian_lite_latest --spider --server-response -O - 2>&1 | sed -ne '/Content-Length/{s/.*: //;p}'` 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 [ ${#remotesize} != ${#localsize} ]; then remotesize=`wget https://downloads.raspberrypi.org/raspbian_lite_latest --spider --server-response -O - 2>&1 | sed -ne '/Content-Length/{s/.*: //;p}'` fi if [ "$remotesize" = "$localsize" ]; then 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 "---------------------------------------------------------------------------" fi else echo "---------------------------------------------------------------------------" echo "Downloading raspbian image from server..." wget -q --show-progress -O${IMAGE_FILE} ${IMAGE_URL} echo "---------------------------------------------------------------------------" fi IMAGE_FILE_UNZIPPED=`zipinfo -1 ${IMAGE_FILE}` IMAGE_FILE_UNZIPPED_SIZE=`zipinfo -l ${IMAGE_FILE} | tail -1 | xargs | cut -d' ' -f3` if ! [ -f ${IMAGE_FILE_UNZIPPED} ]; then echo "---------------------------------------------------------------------------" echo "Unpacking raspbian image..." echo "---------------------------------------------------------------------------" unzip -o -p ${IMAGE_FILE} | pv -p -s ${IMAGE_FILE_UNZIPPED_SIZE} -w 80 > ${IMAGE_FILE_UNZIPPED} fi if ! [ -f ${IMAGE_FILE_CUSTOMIZED} ]; then 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 "---------------------------------------------------------------------------" fi } resize_raw_image() { IMAGE_SIZE_RAW=$(wc -c < "${IMAGE_FILE_UNZIPPED}") IMAGE_SIZE_ACTUAL=$(wc -c < "${IMAGE_FILE_CUSTOMIZED}") IMAGE_ROOTPART_START=$(parted ${IMAGE_FILE_UNZIPPED} unit s print -sm | tail -1 | cut -d: -f2 | sed 's/s//') if [ ${IMAGE_SIZE_ACTUAL} -gt ${IMAGE_SIZE_RAW} ]; then 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 fi echo "---------------------------------------------------------------------------" echo "Resizing image..." echo "---------------------------------------------------------------------------" #resize image dd if=/dev/zero bs=1M count=512 >> ${IMAGE_FILE_CUSTOMIZED} PART_NUM=2 fdisk ${IMAGE_FILE_CUSTOMIZED} <