source: MondoRescue/trunk/mindi/aux-tools/sbin/format-and-kludge-vfat@ 1

Last change on this file since 1 was 1, checked in by bcornec, 19 years ago

Initial import from latest mondo-2.04_cvs_20050503/mindi-1.04_cvs_20050503 on http://www.mondorescue.org

File size: 3.6 KB
RevLine 
[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
10LogIt() {
11 echo "$1" >> /dev/stderr
12}
13
14
15ExitHere() {
16 echo "Exiting here for testing. "
17 exit 1
18}
19
20Die() {
21 LogIt "(format-and-kludge-vfat) $1" 1
22 cat /tmp/fakv.log
23 exit 1
24}
25
26GetPartitionFdiskType() {
27 local drive device ftype
28 device=$1
29 drive=$2
30 ftype=`fdisk -l $drive | grep $device | tr '*' ' ' | tr -s '\t' ' ' | cut -d' ' -f5`
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
50LogIt "format-and-kludge-vfat --- starting"
51
52touch /tmp/fakv.log
53
54if [ "$#" -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
57elif [ "$#" -ne "1" ] ; then
58 LogIt "Usage: format-and-kludge-vfat <device>" 1
59 exit 1
60fi
61
62retval=0
63
64device=$1
65drive=`echo $device | sed s/[0-9]*[0-9]//`
66partition=`echo $device | sed s\%$drive\%\%`
67
68type=`GetPartitionFdiskType $device $drive`
69res=$?
70retval=$(($retval+$res))
71if [ "$res" -ne "0" ]; then
72 exit 1
73fi
74LogIt "Formatting device $device (drive $drive, partition $partition, type $type)" 2
75parted $drive mkfs $partition $type 2> /tmp/fakv.log > /dev/null
76res=$?
77retval=$(($retval+$res))
78if [ "$res" -ne "0" ]; then
79 LogIt "Errors occurred while formatting $device!" 1
80 cat /tmp/fakv.log
81 exit 1
82fi
83if [ "$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
86fi
87LogIt "Setting the boot flag for $device" 2
88parted $drive set $partition boot on 2> /tmp/fakv.log > /dev/null
89res=$?
90retval=$(($retval+$res))
91if [ "$res" -ne "0" ]; then
92 LogIt "Errors occurred while setting the boot flag for $device!" 1
93 cat /tmp/fakv.log
94 exit 1
95fi
96LogIt "Fixing the boot block of $device with ms-sys" 2
97ms-sys -w $device 2> /tmp/fakv.log > /dev/null
98res=$?
99retval=$(($retval+$res))
100if [ "$res" -ne "0" ]; then
101 LogIt "Errors occurred while fixing the boot block of $device!" 1
102 cat /tmp/fakv.log
103 exit 1
104fi
105[ ! -e "/mnt/tmpK" ] && mkdir /mnt/tmpK
106mount $device -t vfat /mnt/tmpK || Die "Can't mount device $device"
107size=`df -m /mnt/tmpK | tr -s ' ' ' ' | cut -d' ' -f2 | tail -n 1`
108umount /mnt/tmpK || Die "Can't unmount /mnt/tmpK"
109
110ideal_size=`cat /tmp/mountlist.txt | grep "$device " | tr -s ' ' ' ' | cut -d' ' -f4`
111if [ "$ideal_size" = "" ]; then
112 LogIt "format-and-kludge-vfat --- can't find $device in mountlist" 1
113 exit 0
114fi
115LogIt "ideal_size = $ideal_size" 2
116ideal_size=$(($ideal_size/1024))
117difference=$(($ideal_size-$size))
118[ "$difference" -lt "0" ] && difference=$(((-1)*$difference))
119LogIt "ideal_size=$ideal_size; size=$size; difference=$difference"
120if [ "$difference" -gt "64" ] ; then
121 retval=$(($retval+1))
122 LogIt "(format-and-kludge-vfat-new) Size mismatch. Did kludge work?" 1
123else
124 LogIt "Size matches. (Well, nearly.) Good." 2
125fi
126
127if [ "$retval" -eq "0" ] ; then
128 LogIt "$device was successfully formatted and prepared" 1
129else
130 LogIt "$device failed to be formatted and prepared" 1
131 cat /tmp/fakv.log
132fi
133LogIt "(by format-and-kludge-vfat)"
134
135LogIt "format-and-kludge-vfat --- leaving (retval=$retval)"
136exit $retval
Note: See TracBrowser for help on using the repository browser.