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
38 USAGE="<VDI image> <mountpoint> [-o options,...]
41 partition=<number> Specify partition to mount
42 fstype=<fstype> Specify FS type instead of autodetecting
45 # This is braindead, two first args are dev and mountpoint if invoked from
46 # mount(1), but could be option if invoked from mount.vdi(1)
47 if [ -n "$1" ] && [[ "$1" != -* ]]; then
51 if [ -n "$1" ] && [[ "$1" != -* ]]; then
59 echo >&2 "Usage: $PROGRAM $USAGE"
65 MOUNTARGS="$MOUNTARGS $1"
68 echo >&2 "$PROGRAM: unrecognized option: $1"
76 if [ -z "$DEVICE" ]; then
77 echo >&2 "$PROGRAM: device to mount not specified"
81 if [ -z "$MOUNTPOINT" ]; then
82 echo >&2 "$PROGRAM: mount point not specified"
86 if [ ! -f "$DEVICE" ]; then
87 echo >&2 "$PROGRAM: $DEVICE is not a file"
91 if [ ! -d "$MOUNTPOINT" ]; then
92 echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
97 magic=$(head -n 1 "$DEVICE")
99 "<<< innotek VirtualBox Disk Image >>>")
101 "<<< Sun VirtualBox Disk Image >>>")
104 echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
113 for opt in $OPTIONS; do
124 if [ -z "$MOUNTOPTIONS" ]; then
127 MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
134 if [ -z "$PARTITION" ]; then
135 echo >&2 "$PROGRAM: missing partition option"
139 # offset when disk starts
142 # find free loop device. XXX race possible
143 imgdev=$($LOSETUP -f)
144 $LOSETUP $imgdev -o $dskoff "$DEVICE"
145 # http://vserver.13thfloor.at/Stuff/QEMU/hdloop.sh
146 sfd=$($SFDISK -dump $imgdev 2>/dev/null | $SED -n '
147 /:/ s/[a-zA-Z]*=\ *\([0-9a-f]*\)\(,\|$\)/\1/g;
148 T; s/^.*:\ //; p' | tr ' ' '.')
156 offset=$((pstart*blksize))
157 size=$((psize*blksize))
158 echo $((offset+dskoff))
163 if [ "$p" = "$PARTITION" ]; then
164 OFFSET=$(getoffset $(echo "$a" | tr '.' ' '))
169 if [ -z "$OFFSET" ]; then
170 echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
174 if [ -z "$MOUNTOPTIONS" ]; then
175 MOUNTOPTIONS="loop,offset=$OFFSET"
177 MOUNTOPTIONS="$MOUNTOPTIONS,loop,offset=$OFFSET"
180 # $MOUNTPOINT might not exist as mount can try to read it from /etc/fstab
181 $MOUNT $MOUNTARGS ${FSTYPE:+-t "$FSTYPE"} ${MOUNTOPTIONS:+-o "$MOUNTOPTIONS"} "$DEVICE" "$MOUNTPOINT"
182 if [ $? -ne 0 ]; then
183 echo >&2 "$PROGRAM: error mounting $DEVICE"