#!/bin/bash # # Copy a img.gz image file to a Solid State Drive / Flash Card # # Copyright (C) 2016 Lonnie Abelbeck # This is free software, licensed under the GNU General Public License # version 3 as published by the Free Software Foundation; you can # redistribute it and/or modify it under the terms of the GNU # General Public License; and comes with ABSOLUTELY NO WARRANTY. # # Input Arg1: sda|sdb|sdc|sdd (usually 'sda') # Input Arg2: img.gz or .img image file # exit_no_action() { echo "Exiting, no action performed." exit 1 } exit_with_usage() { echo "Usage: chromeos-ssd-write sda|sdb|sdc|sdd image_file.img[.gz]" exit 1 } if [ "$(id -u)" -ne 0 ]; then echo "Permission denied, run with 'sudo'" echo "Usage: sudo chromeos-ssd-write sda|sdb|sdc|sdd image_file.img[.gz]" echo "" exit_no_action fi if [ -z "$1" ]; then exit_with_usage fi DISK="${1#/dev/}" if [ "${DISK%[a-d]}" != "sd" ]; then exit_with_usage fi if [ -z "$2" ]; then exit_with_usage fi IMAGEFILE="$2" if [ ! -f "$IMAGEFILE" ]; then echo "File: $IMAGEFILE, can not be found." exit_no_action fi fdisk -l /dev/$DISK printf "Is this the disk you want to OVERWRITE ? [y/N] " read x case $x in y|Y|yes) ;; *) exit_no_action ;; esac if [ ! -e "/dev/$DISK" ]; then echo "Device not found: /dev/$DISK" exit_no_action fi # # Inputs look good, off to work... # printf "\nOpen the 'Files' App and eject all partitions related to the '$DISK' disk.\n" printf "Hit [enter] when complete or [q] to quit... " read x case $x in q|Q|quit) exit_no_action ;; esac sleep 1 for i in 1 2 3 4 5 6 7 8; do if [ -e "/dev/$DISK$i" ]; then umount "/dev/$DISK$i" 2>/dev/null fi done if [ ! -e "/dev/$DISK" ]; then echo "Device not found: /dev/$DISK" exit_no_action fi echo "" echo "Erasing first 1 GB of /dev/$DISK..." echo "This may take a long time..." dd if=/dev/zero of=/dev/$DISK iflag=nocache oflag=direct bs=64k count=16000 echo "" echo "Writing \"$IMAGEFILE\" to /dev/$DISK" echo "This may take a long time..." if [ "${IMAGEFILE}" != "${IMAGEFILE%.gz}" ]; then zcat "$IMAGEFILE" | dd of=/dev/$DISK iflag=fullblock oflag=direct bs=64k else dd if="$IMAGEFILE" of=/dev/$DISK iflag=fullblock oflag=direct bs=64k fi sync echo "" echo "Ejecting /dev/$DISK" eject /dev/$DISK echo "" echo "You can safely remove your '$DISK' disk."