Easy way to format a micro SD card

Started by lubod, April 01, 2014, 01:58:53 PM

Previous topic - Next topic

lubod

So I was looking at the instructions on how to set up a micro SD card:

http://olimex.wordpress.com/2014/01/15/building-debian-linux-bootable-sd-card-with-hardware-accelerated-video-decoding-and-kernel-3-4-for-a10-olinuxino-lime/

A lot of it is things you have to do yourself, unless you go with a ready made image, like the one from Olimex:

https://www.olimex.com/wiki/A10-OLinuXino-LIME#Linux

or this one, which I used:

https://romanrm.net/a10/kernel

But if you are setting up the micro SD card yourself, you can shorten the process a little. I wrote a shell script (for bash, but it can be adapted for other shells) which does all of step 4 (Format and setup the SD-card) for you, automatically.

---
format_sd_card_lime.txt (copy/cut from #! down) (added code to display available partitions at the beginning, reworked/cleaned up to check for root/sudo)
---
#!/bin/bash
# written by Lubo Diakov
# replaces/automates step 4 in A10 Lime setup from:
# http://olimex.wordpress.com/2014/01/15/building-debian-linux-bootable-sd-card-with-hardware-accelerated-video-decoding-and-kernel-3-4-for-a10-olinuxino-lime/
# set to do 2 partitions, but can be edited to do whole disk FAT (MSDOS) partition

# Define some useful shell variables
#
export Output_Device=sdb
export Last_Chance=n

# Check if root/sudo, usage hints
myname=`whoami`
if [ $myname = root ]; then
        echo ""
        echo "Running as root/sudo, proceeding to format script."
        echo "Don't forget to verify the device/partition"
        echo "name is correct, and it is unmounted!"
else
        echo "You must be root to run this script"
        echo "e.g. ./format_sd_card_lime.txt when logged in as root, "
        echo "or sudo bash format_sd_card_lime.txt in the directory"
        echo "where the script is saved."
        echo "Also, don't forget to verify the device/partition"
        echo "name is correct, and it is unmounted!"
        exit 1
fi

# List recognized devices/partitions
echo ""
echo "Currently recognized devices/partitions"
echo ""
fdisk -l|grep /dev
echo ""

# Getting/verifying device/partition name
read -p "Device/partition to format? (e.g. sda, sdb): " Output_Device
echo ""
echo "The device/partition you wish to format is:"
echo "/dev/"$Output_Device
read -p "Continue with formatting? (enter y to continue): " Last_Chance

# Warnings 15, 10 and 5 seconds beforehand, then formatting begins
if [[ $Last_Chance = [Yy] ]]; then
   echo "About to format device" "/dev/"$Output_Device"."
   echo "If this is the wrong device, press control-C immediately!"
   echo "Script will resume in 15 seconds"
   echo "This script WILL erase all partitions!"
   echo "You have been warned!"
   sleep 5
   echo "Script will resume in 10 seconds"
   sleep 5
   echo "Script will resume in 5 seconds"
   sleep 5
   
# Unmount before formatting, umount only checks mtab, sudo eject seems better
# umount /dev/"$Output_Device"
   eject /dev/"$Output_Device"
   echo "OK, formatting /dev/"$Output_Device"."
   
# Fdisk erases partition table (and all existing partitions),
# and creates a new partition table, with 1 FAT partition on entire disk:
   #(echo o; echo n; echo p; echo 1; echo ""; echo ""; echo a; echo 1;echo w; echo q) | fdisk /dev/$(echo $Output_Device)
# Prepare both partitions (FAT for boot, ext3 for root)
   (echo o; echo n; echo p; echo 1; echo ""; echo +64M; echo n; echo p; echo 2; echo ""; echo ""; echo w; echo q) | fdisk /dev/$(echo $Output_Device)
# Prepare only FAT partition (FAT on entire disk, -n, name should be uppercase)   
   echo "Formatting FAT partition..."
   mkfs.vfat -n BOOT /dev/$(echo $Output_Device)1
# Optionally, also prepare ext3 partition
   echo "Formatting ext3 partition..."
   mkfs.ext3 -L root /dev/$(echo $Output_Device)2
   echo "Preparing device to be safely removed/ejected..."
   sync
   echo "Success! Now exiting."
else
   echo "Exiting without formatting anything."
   exit
fi