[1] | 1 | #!/bin/sh
|
---|
| 2 | #
|
---|
| 3 | # MondoRescue script: format-and-kludge-vfat
|
---|
| 4 | #
|
---|
| 5 | # $1 = device, e.g. /dev/hda1
|
---|
| 6 | #
|
---|
| 7 | # This script requires parted and ms-sys
|
---|
| 8 | #
|
---|
| 9 |
|
---|
| 10 | LogIt() {
|
---|
| 11 | echo "$1" >> /dev/stderr
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | ExitHere() {
|
---|
| 16 | echo "Exiting here for testing. "
|
---|
| 17 | exit 1
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | Die() {
|
---|
| 21 | LogIt "(format-and-kludge-vfat) $1" 1
|
---|
| 22 | cat /tmp/fakv.log
|
---|
| 23 | exit 1
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | GetPartitionFdiskType() {
|
---|
| 27 | local drive device ftype
|
---|
| 28 | device=$1
|
---|
| 29 | drive=$2
|
---|
[196] | 30 | ftype=`parted2fdisk -l $drive | grep $device | tr '*' ' ' | tr -s '\t' ' ' | cut -d' ' -f5`
|
---|
[1] | 31 | case $ftype in
|
---|
| 32 | # "b" | "c")
|
---|
| 33 | # the above is correct but mondorestore creates partition with FAT12, so:
|
---|
| 34 | "b" | "c" | "1")
|
---|
| 35 | echo "fat32"
|
---|
| 36 | ;;
|
---|
| 37 | "4" | "e")
|
---|
| 38 | echo "fat16"
|
---|
| 39 | ;;
|
---|
| 40 | *)
|
---|
| 41 | Die "Error: Unsuitable partition type $ftype (or device $device doesn't exist)"
|
---|
| 42 | ;;
|
---|
| 43 | esac
|
---|
| 44 | exit 0
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | # -------------- main ------------
|
---|
| 49 |
|
---|
| 50 | LogIt "format-and-kludge-vfat --- starting"
|
---|
| 51 |
|
---|
| 52 | touch /tmp/fakv.log
|
---|
| 53 |
|
---|
| 54 | if [ "$#" -eq "2" ] ; then
|
---|
| 55 | LogIt "Note: Script called with two parameters. Only one needed - the device." 1
|
---|
| 56 | LogIt " Embleer files are gorn - please ask Hugo to update his code. ;-)" 1
|
---|
| 57 | elif [ "$#" -ne "1" ] ; then
|
---|
| 58 | LogIt "Usage: format-and-kludge-vfat <device>" 1
|
---|
| 59 | exit 1
|
---|
| 60 | fi
|
---|
| 61 |
|
---|
| 62 | retval=0
|
---|
| 63 |
|
---|
| 64 | device=$1
|
---|
| 65 | drive=`echo $device | sed s/[0-9]*[0-9]//`
|
---|
| 66 | partition=`echo $device | sed s\%$drive\%\%`
|
---|
| 67 |
|
---|
| 68 | type=`GetPartitionFdiskType $device $drive`
|
---|
| 69 | res=$?
|
---|
| 70 | retval=$(($retval+$res))
|
---|
| 71 | if [ "$res" -ne "0" ]; then
|
---|
| 72 | exit 1
|
---|
| 73 | fi
|
---|
| 74 | LogIt "Formatting device $device (drive $drive, partition $partition, type $type)" 2
|
---|
| 75 | parted $drive mkfs $partition $type 2> /tmp/fakv.log > /dev/null
|
---|
| 76 | res=$?
|
---|
| 77 | retval=$(($retval+$res))
|
---|
| 78 | if [ "$res" -ne "0" ]; then
|
---|
| 79 | LogIt "Errors occurred while formatting $device!" 1
|
---|
| 80 | cat /tmp/fakv.log
|
---|
| 81 | exit 1
|
---|
| 82 | fi
|
---|
| 83 | if [ "$device" != "/dev/hda1" ] && [ "$device" != "/dev/sda1" ] ; then
|
---|
| 84 | LogIt "OK, this is not /dev/hda1 or /dev/sda1; so, I shan't kludge it." 1
|
---|
| 85 | exit 0
|
---|
| 86 | fi
|
---|
| 87 | LogIt "Setting the boot flag for $device" 2
|
---|
| 88 | parted $drive set $partition boot on 2> /tmp/fakv.log > /dev/null
|
---|
| 89 | res=$?
|
---|
| 90 | retval=$(($retval+$res))
|
---|
| 91 | if [ "$res" -ne "0" ]; then
|
---|
| 92 | LogIt "Errors occurred while setting the boot flag for $device!" 1
|
---|
| 93 | cat /tmp/fakv.log
|
---|
| 94 | exit 1
|
---|
| 95 | fi
|
---|
[1565] | 96 | if [ "`uname -m`" != "ia64" ] ; then
|
---|
[85] | 97 | LogIt "Fixing the boot block of $device with ms-sys" 2
|
---|
| 98 | ms-sys -w $device 2> /tmp/fakv.log > /dev/null
|
---|
| 99 | res=$?
|
---|
| 100 | retval=$(($retval+$res))
|
---|
| 101 | if [ "$res" -ne "0" ]; then
|
---|
| 102 | LogIt "Errors occurred while fixing the boot block of $device!" 1
|
---|
| 103 | cat /tmp/fakv.log
|
---|
| 104 | exit 1
|
---|
| 105 | fi
|
---|
[1] | 106 | fi
|
---|
| 107 | [ ! -e "/mnt/tmpK" ] && mkdir /mnt/tmpK
|
---|
| 108 | mount $device -t vfat /mnt/tmpK || Die "Can't mount device $device"
|
---|
[305] | 109 | size=`df -m -P /mnt/tmpK | tr -s ' ' ' ' | cut -d' ' -f2 | tail -n 1`
|
---|
[1] | 110 | umount /mnt/tmpK || Die "Can't unmount /mnt/tmpK"
|
---|
| 111 |
|
---|
[273] | 112 | ideal_size=`grep "$device " /tmp/mountlist.txt | tr -s ' ' ' ' | cut -d' ' -f4`
|
---|
[1] | 113 | if [ "$ideal_size" = "" ]; then
|
---|
| 114 | LogIt "format-and-kludge-vfat --- can't find $device in mountlist" 1
|
---|
| 115 | exit 0
|
---|
| 116 | fi
|
---|
| 117 | LogIt "ideal_size = $ideal_size" 2
|
---|
| 118 | ideal_size=$(($ideal_size/1024))
|
---|
| 119 | difference=$(($ideal_size-$size))
|
---|
| 120 | [ "$difference" -lt "0" ] && difference=$(((-1)*$difference))
|
---|
| 121 | LogIt "ideal_size=$ideal_size; size=$size; difference=$difference"
|
---|
| 122 | if [ "$difference" -gt "64" ] ; then
|
---|
| 123 | retval=$(($retval+1))
|
---|
| 124 | LogIt "(format-and-kludge-vfat-new) Size mismatch. Did kludge work?" 1
|
---|
| 125 | else
|
---|
| 126 | LogIt "Size matches. (Well, nearly.) Good." 2
|
---|
| 127 | fi
|
---|
| 128 |
|
---|
| 129 | if [ "$retval" -eq "0" ] ; then
|
---|
| 130 | LogIt "$device was successfully formatted and prepared" 1
|
---|
| 131 | else
|
---|
| 132 | LogIt "$device failed to be formatted and prepared" 1
|
---|
| 133 | cat /tmp/fakv.log
|
---|
| 134 | fi
|
---|
| 135 | LogIt "(by format-and-kludge-vfat)"
|
---|
| 136 |
|
---|
| 137 | LogIt "format-and-kludge-vfat --- leaving (retval=$retval)"
|
---|
| 138 | exit $retval
|
---|