From 89a7ca3cc6289d6f9cfd1762cf909a018ba6cd88 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 22 Dec 2015 08:24:10 +0100 Subject: [PATCH] updated Doc and steps. Commit push before retest. --- build_arm_image/01_create_sdcard.sh | 42 +++++++++++---- build_arm_image/02_install_yunohost.sh | 18 +++---- build_arm_image/03_copy_sdcard_to_img.sh | 39 ++------------ build_arm_image/README.md | 69 ++++++++++++++++-------- 4 files changed, 90 insertions(+), 78 deletions(-) diff --git a/build_arm_image/01_create_sdcard.sh b/build_arm_image/01_create_sdcard.sh index 513f43e..f164833 100755 --- a/build_arm_image/01_create_sdcard.sh +++ b/build_arm_image/01_create_sdcard.sh @@ -4,6 +4,7 @@ # # Usage: # ./01_create_sdcard.sh downloaded_debian_image.zip /dev/sdcard_device +# ./01_create_sdcard.sh generated_yunohost.img /dev/sdcard_device # # # Status: functional @@ -50,6 +51,9 @@ skip_if() { sha_verify_zip() { local zip=$DEBIAN_IMG_ZIP + + [[ -z "$zip" ]] && { echo "no zip refuse to run"; return 3; } + local out=_build_arm_steps/sha_verify_zip skip_if $out && return 2 sha1sum $zip > $out @@ -57,19 +61,20 @@ sha_verify_zip() { local sha="$(sed -e 's/\(^[a-f0-9]\+\).*/\1/' $out)" if [[ "$sha" != "$SHA_DEBIAN_IMG_ZIP" ]] then - echo "NOK: '$sha' != '$SHA_DEBIAN_IMG_ZIP'" - exit 1 + die "NOK: '$sha' != '$SHA_DEBIAN_IMG_ZIP'" fi } # unzip raspbian image in the cache folder unzip_img() { - if ! skip_if _build_arm_steps/*.img + local img_filename=$(unzip -l $DEBIAN_IMG_ZIP | awk '/\.img$/ { print $4 }') + if ! skip_if _build_arm_steps/$img_filename then unzip -o $DEBIAN_IMG_ZIP -d _build_arm_steps fi + # get extrated image filename from zip file - DEBIAN_IMG=$(ls _build_arm_steps/ | grep \\.img$) + DEBIAN_IMG="_build_arm_steps/$img_filename" } # helper, try to guess top device name @@ -111,7 +116,8 @@ umount_sdcard_partition() { dd_to_sdcard() { [[ -z "$SDCARD" ]] && { echo '$SDCARD is empty refusing to run'; return; } # ensure that sdcard partitions are unmounted with umount_sdcard_partition - sudo dd bs=16M if=_build_arm_steps/$DEBIAN_IMG of=$SDCARD + echo "starting dd it will take some times…" + sudo dd bs=16M if="$DEBIAN_IMG" of=$SDCARD sudo sync } @@ -126,10 +132,10 @@ test_all_tools() { mount_loopback_img() { [[ -z "$DEBIAN_IMG" ]] && { echo '$DEBIAN_IMG is empty refusing to run'; return; } mkdir -p _build_arm_steps/mnt - local dev_loop0=$(sudo losetup -f --show _build_arm_steps/$DEBIAN_IMG) + local dev_loop0=$(sudo losetup -f --show "$DEBIAN_IMG") local part_offset=$(sudo fdisk -l $dev_loop0 | awk '/Linux/ { print $2 }') # mount the ext4 partition at computed offset - local dev_loop1=$(sudo losetup -f --show -o $((512 * $part_offset)) _build_arm_steps/$DEBIAN_IMG) + local dev_loop1=$(sudo losetup -f --show -o $((512 * $part_offset)) "$DEBIAN_IMG") sudo mount $dev_loop1 _build_arm_steps/mnt/ } @@ -158,14 +164,17 @@ add_ssh_key_to_img() { chmod -R go= .ssh/ # give to pi user id sudo chown -R --reference . .ssh + # return to working folder cd - > /dev/null umount_loopback_img + echo "ssh-key added" } # functions call in that order, edit remove a long running step if already done or if # you want to skip it, step states are saved in folder _build_arm_steps and skipped automatically. +# STEPS is modifiy in main() if argument1 is an .img and not a .zip STEPS=" -sha_verify_zip +sha_verify_zip unzip_img add_ssh_key_to_img umount_sdcard_partition @@ -177,14 +186,27 @@ main() { # init if [[ -z "$1" ]] then - echo "argument 1 error: expecting raspbian image file.zip" + echo "argument 1 error: expecting raspbian image file.zip or an img" echo "can be downloaded here: $DOWNLOAD_URL" exit 1 fi + local regexp='\.img$' + # reading script argument DEBIAN_IMG_ZIP=$1 - [[ -f "$DEBIAN_IMG_ZIP" ]] || die "error raspbian image not found: '$DEBIAN_IMG_ZIP'" + if [[ "$1" =~ $regexp ]] + then + DEBIAN_IMG="$1" + DEBIAN_IMG_ZIP="" + # write image verbatim without modification + STEPS=" + umount_sdcard_partition + dd_to_sdcard" + else + [[ -f "$DEBIAN_IMG_ZIP" ]] || die "error raspbian image not found: '$DEBIAN_IMG_ZIP'" + fi + SDCARD=$(get_top_device "$2") [[ -z "$SDCARD" ]] && die "argument 2 error: expecting sdcard_device" diff --git a/build_arm_image/02_install_yunohost.sh b/build_arm_image/02_install_yunohost.sh index ef587c9..a441215 100755 --- a/build_arm_image/02_install_yunohost.sh +++ b/build_arm_image/02_install_yunohost.sh @@ -2,9 +2,6 @@ # # Usage: ./02_install_yunohost.sh DHCP_IP_ADDRESS_OF_RASPBERRYPI # -# This script apply this doc:  -# https://yunohost.org/#/build_arm_image -# # Doc: See README.md # # STEPS: @@ -12,7 +9,7 @@ # 2. run install_yunohostv2 on the raspi # 3. install an yunohost-firstboot script and shutdown # -# Status: draft +# Status: functional # Licence: GPLv3 # Author: sylvain303@github @@ -22,7 +19,7 @@ sshpi() { ssh -i _build_arm_steps/nopasskey pi@$PI_IP "$@" } -# harcoded test IP +# harcoded test IP, you can change for yours for debuging. See main() PI_IP=192.168.1.2 # the installer script is used localy, no git clone else where. YUNOHOST_INSTALL=../install_yunohostv2 @@ -31,12 +28,7 @@ YUNOHOST_REMOTE_DIR=/tmp/install_yunohost # dummy password PASSROOT='Free_money?yunomakeit' -# wrapper to init with positional parameters -init_param() { - PI_IP=$1 - ask_root_pass -} - +# used by main() See at the end. ask_root_pass() { echo "HINT: very good program to generate strong memorisable passwords: pwqgen" echo "HINT: sudo apt-get install passwdqc" @@ -225,6 +217,10 @@ do_step() { # main script code, wrapped inside a function, so the whole script can also be # sourced as a lib, for debug or unittesting purpose. main() { + # you can comment the 2 lines, for debuging. Fix PI_IP with your dhcp IP. + PI_IP=$1 + ask_root_pass + do_step init_sdcard_and_reboot wait_raspberrypi || return 1 diff --git a/build_arm_image/03_copy_sdcard_to_img.sh b/build_arm_image/03_copy_sdcard_to_img.sh index 5988e8a..760fc61 100755 --- a/build_arm_image/03_copy_sdcard_to_img.sh +++ b/build_arm_image/03_copy_sdcard_to_img.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash # # Create the image file from the sdcard # @@ -28,18 +28,6 @@ test_tool() { fi } -# skip if the file exists. -# usage: skip_if _build_arm_steps/somefile && return 2 -skip_if() { - if [[ -e "$1" ]] - then - echo "cached" - return 0 - fi - # echo -n "continue for $1" - return 1 -} - # helper, try to guess top device name # /dev/sdp2 => /dev/sdp # /dev/mmcblk0p1 => /dev/mmcblk0 @@ -95,34 +83,15 @@ test_all_tools() { done } -# mount the .img so we can write on it before copying on the sdcard -mount_loopback_img() { - [[ -z "$OUTPUT_IMG" ]] && { echo '$OUTPUT_IMG is empty refusing to run'; return; } - [[ ! -d _build_arm_steps/mnt ]] && mkdir _build_arm_steps/mnt - local dev_loop0=$(sudo losetup -f --show _build_arm_steps/$OUTPUT_IMG) - local part_offset=$(sudo fdisk -l $dev_loop0 | awk '/Linux/ { print $2 }') - # mount the ext4 partition at computed offset - local dev_loop1=$(sudo losetup -f --show -o $((512 * $part_offset)) _build_arm_steps/$OUTPUT_IMG) - sudo mount $dev_loop1 _build_arm_steps/mnt/ -} - -umount_loopback_img() { - # find mounted loopback - local dev_loop1=$(mount | awk '/_build_arm_steps/ { print $1 }') - # compute loop n-1 - local n=${dev_loop1#/dev/loop} - local dev_loop0="/dev/loop$(($n - 1))" - sudo umount _build_arm_steps/mnt - sudo losetup -d $dev_loop1 - sudo losetup -d $dev_loop0 -} - +# debug, not used mount_sdcard_data_partition() { local part_data=2 [[ ! -d _build_arm_steps/sdcard ]] && mkdir _build_arm_steps/sdcard sudo mount ${SDCARD}p2 _build_arm_steps/sdcard } +# prototype, not used. Wanna test if I can d.d only used part of the partion, and fix it back on the PC +# not working yet, may be not achieved anyway… get_used_partition_size() { local start_offset=$(sudo fdisk -l /dev/mmcblk0 | awk '/Linux/ { print $2 * 512 }') local used=$(df -B1 _build_arm_steps/sdcard | awk '/dev.mmcblk0p2/ { print $3 }') diff --git a/build_arm_image/README.md b/build_arm_image/README.md index 3872c88..d3019b4 100644 --- a/build_arm_image/README.md +++ b/build_arm_image/README.md @@ -6,42 +6,41 @@ The files here are for builder. They are not needed to install your yunohost. The folder etc/ structure maps debian OS folders, shell script here are helpers. Files in etc/ subfolders will be copied on the sdcard when installation is finished. +Those scripts are following the steps provided here: https://yunohost.org/#/build_arm_image + ## License All the content here is distributed under [GPLv3](http://www.gnu.org/licenses/gpl-3.0.txt). ## See also -* https://yunohost.org/#/build_arm_image for building steps explanations * https://forum.yunohost.org/t/building-a-new-image-for-raspberry-debian-jessie-fr-en/1101/13 - discussion about building a new image for Raspberry Pi. ## Files Description of the files available here. -All step stripts are using your PC to store some files in the folder _build_arm_steps/. This folder will be created by 01_create_sdcard.sh. You can wipe it, or remove selected script data form it, to redo a step. +All step stripts are using your PC to store some files in the folder `_build_arm_steps/`. This folder will be created by `01_create_sdcard.sh`. You can wipe it, or remove selected script data form it, to redo a step. -All step script are designed to run in this folder: +All step scripts are designed to run in this folder: -~~~ +~~~bash git clone this_repos cd path_to/build_arm_image/ +./01_create_sdcard.sh raspbian-jessie.zip /dev/mmcblk0 +# plug and boot raspberrypi +./02_install_yunohost.sh 192.168.1.200 +# unplug raspberrypi and connect the sdcard back to the PC +./03_copy_sdcard_to_img.sh /dev/mmcblk0 ~~~ -then run steps scripts in order. +### Steps -### Run on PC - -Scripts are prefixed by a number which is an hint of the order they should be ran. - -#### autosizer.sh - -Used on your PC to compact the dd image you will copy back after the rapsbian has been built. -This script requier root privilege to run and modify the local sdcard image. +Scripts are prefixed by a number which is the order they must be ran. #### 01_create_sdcard.sh Create a bootable image for raspbian. You have to download the .zip of the raspbian image. -It embeds sudo call for using dd to copy the raw image to the sdcard. -It will add an ssh key to pi default rasbian user in oder to connect later to continue automated installation. The pi user will be removed at the end. The ssh key-pair is generated only for you. +This script embeds sudo call for using dd to copy the raw image to the sdcard. +It will add an ssh key to pi default rasbian user in oder to connect later to continue automated installation. The pi user will be removed at the end (See #4). The ssh key-pair is generated only for you. Usage: @@ -51,7 +50,9 @@ Usage: It takes some minutes to perform all the steps (~ 2m3.867s at last run). Be patient. -Use `df` or `lsblk` to find the name of your sdcard device. The script is taking care of umonting the partion if any. It also guess the disk's name if you give an partition's name instead of entire disk device's name. +Use `df` or `lsblk` to find the name of your sdcard device. The script is taking care of umonting the partition if any. It also guesses the disk's name if you give an partition's name instead of entire disk device's name. + +Be carefull not to wipe the wrong device! #### 02_install_yunohost.sh @@ -75,16 +76,40 @@ Or what ever your DHCP is giving… Run: -~~~ +~~~bash ./02_install_yunohost.sh 192.168.1.123 ~~~ -### Run on Raspberry +#### 03_copy_sdcard_to_img.sh -Will be handled by [02_install_yunohost.sh](https://github.com/Sylvain303/install_script/blob/master/build_arm_image/02_install_yunohost.sh) - -run the following script before shuting down your raspbian. +When the build is finished the Raspberry will shutdown. Unplug and insert the sdcard back in the computer. +~~~bash +./03_copy_sdcard_to_img.sh /dev/sdcard_device ~~~ -./intsall_arm.sh + +This will ask your sudo password and dd the resulting img from the sdcard back to your PC. Stored in `_build_arm_steps/` folder. It takes arround 6min on my PC to copy an sdcard of 8G. + +The image will be automaticly shrinked to its minimal size using autosizer.sh. + + +#### test the image back + +You can test the resutling image back with `01_create_sdcard.sh`. + +~~~bash +./01_create_sdcard.sh _build_arm_steps/newly_created_image.img /dev/sdcard_device ~~~ + +#### share + +You can zip, bzip2, or…, and share your freshly created yunohost image on the [forum](https://forum.yunohost.org). + +Enjoy! + +### Helper + +#### autosizer.sh + +Used on your PC to compact the dd image you will copy back after the rapsbian has been built. +This script requier root privilege to run and modify the local sdcard image.