Pidora-2014-Reformating-Boot-Script

From CDOT Wiki
Revision as of 12:07, 14 August 2014 by Agreene (talk | contribs) (Pidora 2014 Reformating Boot Partition Script)
Jump to: navigation, search

Pidora 2014 Reformating Boot Partition Script


#!/bin/bash
#
# Script to take an SD card image created by Livemedia-Creator (such as for a
# Raspberry Pi) with a ext4 boot partition and reformat it 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.0 2013-04-05
#
# Authors:
# Andrew Greene, Seneca College 2013-04-05
#



# partition 
loopdev=$(kpartx -av "$1" | awk  'NR==1 {print $3}') 
echo -e "$loopdev"

# You 
sleep 3

#create temp dirs
mkdir ./temp
mkdir -p ./tmp/boot


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

## copy boot data to temp dir
cp -r ./temp/* ./tmp/boot

#rename kernel.img work around for boot 
cp ./tmp/boot/kernel-3.* ./tmp/boot/kernel.img

#unmount temp boot dir
umount ./temp

#reformat boot parttions to vfat
mkfs -t vfat -n boot /dev/mapper/$loopdev
sleep 3

# update partition fs info
echo "t
1
c
d
3
w
" | fdisk /dev/${loopdev::5}

sleep 3
## re-mount boot partition
mount -o rw /dev/mapper/$loopdev ./temp

#cp orginal boot data to new vfat partition
cp -r ./tmp/boot/* ./temp
sleep 10
# umount tmp boot again
umount ./temp

sync

# remove partions and loopback devs
kpartx -dv "$1"

sleep 3

# cleanup
dmsetup remove $loopdev
sleep 2

rm -rf ./temp
rm -rf ./tmp