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