2 #==============================================================================
4 # Copyright (C) Elan Ruusamäe <glen@pld-linux.org>, 2008
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of
9 # the License, or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public
17 # License along with this program; if not, write to:
18 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 # Boston, MA 02110-1301 USA
21 # -- For details, see the file named "LICENSE.GPLv2"
22 #==============================================================================
24 # Sanitize environment
25 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
36 USAGE="VDI image mountpoint [-o options,...]
39 partition=<number> Specify partition to mount
40 fstype=<fstype> Specify FS type instead of autodetecting
43 # This is braindead, two first args are dev and mountpoint if invoked from
44 # mount(1), but could be option if invoked from mount.vdi(1)
45 if [ -n "$1" ] && [[ "$1" != -* ]]; then
49 if [ -n "$1" ] && [[ "$1" != -* ]]; then
57 echo >&2 "Usage: $PROGRAM $USAGE"
63 MOUNTARGS="$MOUNTARGS $1"
66 echo >&2 "$PROGRAM: unrecognized option: $1"
74 if [ -z "$DEVICE" ]; then
75 echo >&2 "$PROGRAM: device to mount not specified"
79 if [ -z "$MOUNTPOINT" ]; then
80 echo >&2 "$PROGRAM: mount point not specified"
84 if [ ! -f "$DEVICE" ]; then
85 echo >&2 "$PROGRAM: $DEVICE is not a file"
89 if [ ! -d "$MOUNTPOINT" ]; then
90 echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
95 magic=$(head -n 1 "$DEVICE")
96 if [ "$magic" != "<<< innotek VirtualBox Disk Image >>>" ]; then
97 echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
106 for opt in $OPTIONS; do
117 if [ -z "$MOUNTOPTIONS" ]; then
120 MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
127 if [ -z "$PARTITION" ]; then
128 echo >&2 "$PROGRAM: missing partition option"
132 # offset when disk starts
135 # find free loop device. XXX race possible
137 losetup $imgdev -o $dskoff "$DEVICE"
138 # http://vserver.13thfloor.at/Stuff/QEMU/hdloop.sh
139 sfd=$(/sbin/sfdisk -dump $imgdev 2>/dev/null | sed -n '
140 /:/ s/[a-zA-Z]*=\ *\([0-9a-f]*\)\(,\|$\)/\1/g;
141 T; s/^.*:\ //; p' | tr ' ' '.')
149 offset=$((pstart*blksize))
150 size=$((psize*blksize))
151 echo $((offset+dskoff))
156 if [ "$p" = "$PARTITION" ]; then
157 OFFSET=$(getoffset $(echo "$a" | tr '.' ' '))
162 if [ -z "$OFFSET" ]; then
163 echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
167 if [ -z "$MOUNTOPTIONS" ]; then
168 MOUNTOPTIONS="offset=$OFFSET"
170 MOUNTOPTIONS="$MOUNTOPTIONS,offset=$OFFSET"
173 # $MOUNTPOINT might not exist as mount can try to read it from /etc/fstab
174 "$MOUNT" $MOUNTARGS ${FSTYPE:+-t "$FSTYPE"} ${MOUNTOPTIONS:+-o "$MOUNTOPTIONS"} "$DEVICE" "$MOUNTPOINT"
175 if [ $? -ne 0 ]; then
176 echo >&2 "$PROGRAM: error mounting $DEVICE"