1 #!/bin/sh 2 3 # Support formatting of partitions. 4 # 5 # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 6 # 7 # This program is free software; you can redistribute it and/or modify it under 8 # the terms of the GNU General Public License as published by the Free Software 9 # Foundation; either version 3 of the License, or (at your option) any later 10 # version. 11 # 12 # This program is distributed in the hope that it will be useful, but WITHOUT 13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 15 # details. 16 # 17 # You should have received a copy of the GNU General Public License along with 18 # this program. If not, see <http://www.gnu.org/licenses/>. 19 20 PROGNAME=`basename "$0"` 21 THISDIR=`dirname "$0"` 22 23 COMMON="$THISDIR/makesd-common" 24 FSCK="/sbin/fsck" 25 MKFS="/sbin/mkfs" 26 MKSWAP="/sbin/mkswap" 27 28 . "$COMMON" 29 30 31 32 # format <device> <type> 33 34 format() 35 { 36 DEVICE=$1 37 TYPE=$2 38 39 if [ ! -e "$DEVICE" ] ; then 40 echo "$DEVICE does not exist and therefore cannot be formatted." 1>&2 41 return 1 42 fi 43 44 case "$DEVICE" in 45 ( fat ) 46 "${MKFS}.vfat" -F 32 -n "boot" "$DEVICE" && "${FSCK}.vfat" -a -y "$DEVICE" 47 ;; 48 ( ext* ) 49 "${MKFS}.$TYPE" "$DEVICE" && "${FSCK}.$TYPE" -y "$DEVICE" 50 ;; 51 ( swap ) 52 "$MKSWAP" "$DEVICE" 53 ;; 54 esac 55 } 56 57 58 59 # Emit the help message if requested. 60 61 if [ "$1" = '--help' ] ; then 62 cat 1>&2 <<EOF 63 Usage: $PROGNAME 64 65 The input for this program is supplied via standard input as partition 66 definitions employing the following pertinent properties: 67 68 type: <type> 69 70 Types recognised include ext, ext2, ext3, ext4, fat and swap. 71 EOF 72 exit 0 73 fi 74 75 # Obtain details of the selected device. 76 77 check_device 78 79 # Read partition types, one per line. 80 81 PARTNUM=0 82 SECTION= 83 84 while read LINE ; do 85 86 if [ "$LINE" ] && [ ! "$SECTION" ] ; then 87 SECTION=$LINE 88 PARTNUM=$(($PARTNUM + 1)) 89 90 elif [ ! "$LINE" ] ; then 91 SECTION= 92 93 elif [ "$SECTION" ] ; then 94 FIELD=`get_field "$LINE"` 95 VALUE=`get_value "$LINE"` 96 97 if [ "$FIELD" = 'type' ] ; then 98 DEVICE="${DEV}${PARTNUM}" 99 format "$DEVICE" "$TYPE" 100 fi 101 fi 102 done