Pidora-2014-Reformating-Boot-Script

From CDOT Wiki
Jump to: navigation, search
#!/usr/bin/env bash
#
# Script to take an SD card image created by Livemedia-Creator (such as for a
# Raspberry Pi) with an ext4 boot partition and reformat it to vfat. 
#
# To use this script, provide the image name as
# the first argument.
# Example "./revfat image-name.img"
#
# Requirements (Fedora 17 package):
# bash (bash)
# fdisk (util-linux)
# kpartx (kpartx)
#
# Version 1.1 2014-08-27
#
# Authors:
# Andrew Greene,        Seneca College 2013-04-05
# Christopher Markieta, Seneca College 2014-08-27
#

set -e

MY_DIR=$(dirname $0)

if [ $(id -u) -ne 0 ]; then
    echo "Root privileges are required for running $0."
    exit 1
elif [ -z $1 ]; then
    echo "Usage: $0 [image]"
    exit 1
fi

loopdev=$(kpartx -avs "$1" | awk 'NR==1 {print $3}')
echo "$loopdev"

mkdir    "$MY_DIR"/temp/
mkdir -p "$MY_DIR"/tmp/boot/

# mount boot partition
mount -o rw /dev/mapper/$loopdev "$MY_DIR"/temp/

cp -r "$MY_DIR"/temp/* "$MY_DIR"/tmp/boot/

# Rename kernel.img workaround for boot
cp "$MY_DIR"/tmp/boot/kernel-3.* "$MY_DIR"/tmp/boot/kernel.img

umount "$MY_DIR"/temp/

# format boot parttion to vfat
mkfs -t vfat -n BOOT /dev/mapper/$loopdev

# update partition fs info
echo "t
1
c
d
3
w
" | fdisk "$1"

# re-mount boot partition
mount -o rw /dev/mapper/$loopdev "$MY_DIR"/temp/

# copy original boot data to new vfat partition
cp -r "$MY_DIR"/tmp/boot/* "$MY_DIR"/temp/

umount "$MY_DIR"/temp/
kpartx -dv "$1"

rmdir  "$MY_DIR"/temp/
rm -rf "$MY_DIR"/tmp/

Source: https://github.com/Markieta/raspberrypi-reformat