Shell script for formatting the SD card

Source: Internet
Author: User

--------------------

The script is first extended and then formatted. One fat extension and one ext3 Partition

-------------------

#! /Bin/sh
#
# Partition and format the SD card script
# Usage mksdboot. Sh -- device/dev/SDB

Version = "0.1"

Execute ()
{
$ *>/Dev/null
If [$? -Ne 0]; then
Echo
Echo "error: executing $ *"
Echo
Exit 1
Fi
}

Version ()
{
Echo
Echo "'basename $ 1' version $ version"
Echo

Exit 0
}

Usage ()
{
Echo"
Usage: 'basename $ 1' <Options> [files for install partition]

Mandatory options:
-- Device SD block device node (e. g/dev/SDD)

Optional options:
-- Version print version.
-- Help print this help message.
"
Exit 1
}

# Process command line...
While [$ #-GT 0]; do
Case $1 in
-- Help |-h)
Usage $0
;;
-- Device) shift; device = $1; shift ;;;
-- Version) version $0 ;;
*) Copy = "$ copy $1"; shift ;;;
Esac
Done

Test-Z $ Device & Usage $0

 
If [! -B $ device]; then
Echo "error: $ device is not a block Device File"
Exit 1;
Fi

Echo "************************************** **********************"
Echo "* This will delete all the data on $ device *"
Echo "**"
Echo "* warning! Make sure your computer does not go *"
Echo "* in to idle mode while this script is *"
Echo "* running. The script will complete ,*"
Echo "* but your SD card may be too upted .*"
Echo "**"
Echo "* press <enter> to confirm ....*"
Echo "************************************** **********************"
Read junk

For I in 'LS-1 $ device? '; Do
Echo "unmounting device '$ I '"
Umount $ I 2>/dev/null
Done

Execute "dd If =/dev/Zero of = $ device BS = 1024 COUNT = 1024"

# Get the partition information.
Total_size = 'fdisk-L $ device | grep disk | awk '{print $5 }''
Total_cyln = 'echo $ total_size/255/63/512 | bc'

# Default number of cylinder for first parition
PC1 = 5

# Calculate Number of cylinder for the second parition
If ["$ copy "! = ""]; Then
# Installer on one 4 gb sd card.
PC2 = 110
# PC2 = $ ($ total_cyln-$ pC1)/2 ))
Fi

{
Echo, $ pC1, 0x0c ,*
If ["$ PC2 "! = ""]; Then
Echo, $ PC2 ,,-
Echo ,,,-
Else
Echo ,,,-
Fi
} | Sfdisk-D-H 255-s 63-C $ total_cyln $ Device

If [$? -Ne 0]; then
Echo Error
Exit 1;
Fi

Echo "formating $ {Device} 1 ..."
Execute "mkfs. vfat-F 32-n" Boot "$ {Device} 1"
Echo "formating $ {Device} 2 ..."
Execute "mkfs. ext3-J-L" rootfs "$ {Device} 2"
If ["$ PC2 "! = ""]; Then
Echo "formating $ {Device} 3 ..."
Execute "mkfs. ext3-J-L" start_here "$ {Device} 3"
Fi

Echo "completed! "

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.