-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.vdi
166 lines (146 loc) · 3.71 KB
/
mount.vdi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/sh
#==============================================================================
# mount.vdi
# Copyright (C) Elan Ruusamäe <glen@pld-linux.org>, 2008
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to:
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA 02110-1301 USA
#
# -- For details, see the file named "LICENSE.GPLv2"
#==============================================================================
# Sanitize environment
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# Commands
LOSETUP=/sbin/losetup
MOUNT=/bin/mount
PARTED=/usr/sbin/parted
SED=/bin/sed
PROGRAM=${0##*/}
MOUNTARGS=
OPTIONS=
FSTYPE=
USAGE="<VDI image> <mountpoint> [-o options,...]
Options:
partition=<number> Specify partition to mount
fstype=<fstype> Specify FS type instead of autodetecting
"
# This is braindead, two first args are dev and mountpoint if invoked from
# mount(1), but could be option if invoked from mount.vdi(1)
if [ -n "$1" ] && [[ "$1" != -* ]]; then
DEVICE=$1
shift
fi
if [ -n "$1" ] && [[ "$1" != -* ]]; then
MOUNTPOINT=$1
shift
fi
while :; do
case "$1" in
-h | "-?" )
echo >&2 "Usage: $PROGRAM $USAGE"
exit 0 ;;
-o)
OPTIONS="$2";
shift ;;
-n)
MOUNTARGS="$MOUNTARGS $1"
;;
-?* )
echo >&2 "$PROGRAM: unrecognized option: $1"
exit 1 ;;
*)
break ;;
esac
shift
done
if [ -z "$DEVICE" ]; then
echo >&2 "$PROGRAM: device to mount not specified"
exit 1
fi
if [ -z "$MOUNTPOINT" ]; then
echo >&2 "$PROGRAM: mount point not specified"
exit 1
fi
if [ ! -f "$DEVICE" ]; then
echo >&2 "$PROGRAM: $DEVICE is not a file"
exit 1
fi
if [ ! -d "$MOUNTPOINT" ]; then
echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
exit 1
fi
# check magic
magic=$(head -n 1 "$DEVICE")
case "$magic" in
"<<< innotek VirtualBox Disk Image >>>")
;;
"<<< Sun VirtualBox Disk Image >>>")
;;
*)
echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
exit 1
esac
MOUNTOPTIONS=
PARTITION=
OFFSET=
OLDIFS=$IFS
IFS=", "
for opt in $OPTIONS; do
KEY=${opt%=*};
VAL=${opt#*=};
case "$KEY" in
part|partition)
PARTITION="$VAL"
;;
fstype)
FSTYPE="$VAL"
;;
*)
if [ -z "$MOUNTOPTIONS" ]; then
MOUNTOPTIONS="$opt"
else
MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
fi
;;
esac
done
IFS="$OLDIFS"
if [ -z "$PARTITION" ]; then
echo >&2 "$PROGRAM: missing partition option"
exit 1
fi
# offset when disk starts
dskoff=8704
# find free loop device. XXX race possible
imgdev=$($LOSETUP -f) || exit 1
$LOSETUP $imgdev -o $dskoff "$DEVICE"
dump=$($PARTED -s -m $imgdev unit B print)
$LOSETUP -d $imgdev || exit 1
partoff=$(echo "$dump" | $SED -ne "s/^$PARTITION:"'\([0-9]*\)B:.*/\1/p')
OFFSET=$((dskoff + partoff))
if [ -z "$partoff" -o -z "$OFFSET" ]; then
echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
exit 1
fi
if [ -z "$MOUNTOPTIONS" ]; then
MOUNTOPTIONS="loop,offset=$OFFSET"
else
MOUNTOPTIONS="$MOUNTOPTIONS,loop,offset=$OFFSET"
fi
# $MOUNTPOINT might not exist as mount can try to read it from /etc/fstab
$MOUNT $MOUNTARGS ${FSTYPE:+-t "$FSTYPE"} ${MOUNTOPTIONS:+-o "$MOUNTOPTIONS"} "$DEVICE" "$MOUNTPOINT"
if [ $? -ne 0 ]; then
echo >&2 "$PROGRAM: error mounting $DEVICE"
fi