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

Last change on this file since 86 was 86, checked in by bcornec, 14 years ago

apply patch r83:85 from the 2.04_berlios branch

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
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
96if [ "`arch`" != "ia64" ] ; then
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
106fi
107[ ! -e "/mnt/tmpK" ] && mkdir /mnt/tmpK
108mount $device -t vfat /mnt/tmpK || Die "Can't mount device $device"
109size=`df -m /mnt/tmpK | tr -s ' ' ' ' | cut -d' ' -f2 | tail -n 1`
110umount /mnt/tmpK || Die "Can't unmount /mnt/tmpK"
111
112ideal_size=`cat /tmp/mountlist.txt | grep "$device " | tr -s ' ' ' ' | cut -d' ' -f4`
113if [ "$ideal_size" = "" ]; then 
114    LogIt "format-and-kludge-vfat --- can't find $device in mountlist" 1
115    exit 0
116fi
117LogIt "ideal_size = $ideal_size" 2
118ideal_size=$(($ideal_size/1024))
119difference=$(($ideal_size-$size))
120[ "$difference" -lt "0" ] && difference=$(((-1)*$difference))
121LogIt "ideal_size=$ideal_size; size=$size; difference=$difference"
122if [ "$difference" -gt "64" ] ; then
123    retval=$(($retval+1))
124    LogIt "(format-and-kludge-vfat-new) Size mismatch. Did kludge work?" 1
125else
126    LogIt "Size matches. (Well, nearly.) Good." 2
127fi
128
129if [ "$retval" -eq "0" ] ; then
130    LogIt "$device was successfully formatted and prepared" 1
131else
132    LogIt "$device failed to be formatted and prepared" 1
133    cat /tmp/fakv.log
134fi
135LogIt "(by format-and-kludge-vfat)"
136
137LogIt "format-and-kludge-vfat --- leaving (retval=$retval)"
138exit $retval
Note: See TracBrowser for help on using the repository browser.