]> git.pld-linux.org Git - packages/VirtualBox.git/blob - mount.vdi
- specify -o loop to be sure
[packages/VirtualBox.git] / mount.vdi
1 #!/bin/sh
2 #==============================================================================
3 # mount.vdi
4 #   Copyright (C) Elan Ruusamäe <glen@pld-linux.org>, 2008
5 #
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.
10 #
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.
15 #
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
20 #
21 #   -- For details, see the file named "LICENSE.GPLv2"
22 #==============================================================================
23
24 # Sanitize environment
25 export PATH=/bin:/sbin:/usr/bin:/usr/sbin
26
27 # Commands
28 LOSETUP=/sbin/losetup
29 MOUNT=/bin/mount
30 SFDISK=/sbin/sfdisk
31 SED=/bin/sed
32
33 PROGRAM=${0##*/}
34 MOUNTARGS=
35 OPTIONS=
36 FSTYPE=
37
38 USAGE="<VDI image> <mountpoint> [-o options,...]
39
40 Options:
41       partition=<number>              Specify partition to mount
42       fstype=<fstype>                 Specify FS type instead of autodetecting
43 "
44
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
48         DEVICE=$1
49         shift
50 fi
51 if [ -n "$1" ] && [[ "$1" != -* ]]; then
52         MOUNTPOINT=$1
53         shift
54 fi
55
56 while :; do
57         case "$1" in
58         -h | "-?" )
59                 echo >&2 "Usage: $PROGRAM $USAGE"
60                 exit 0 ;;
61         -o)
62                 OPTIONS="$2";
63                 shift ;;
64         -n)
65                 MOUNTARGS="$MOUNTARGS $1"
66                 ;;
67         -?* )
68                 echo >&2 "$PROGRAM: unrecognized option: $1"
69                 exit 1 ;;
70         *)
71                 break ;;
72         esac
73         shift
74 done
75
76 if [ -z "$DEVICE" ]; then
77         echo >&2 "$PROGRAM: device to mount not specified"
78         exit 1
79 fi
80
81 if [ -z "$MOUNTPOINT" ]; then
82         echo >&2 "$PROGRAM: mount point not specified"
83         exit 1
84 fi
85
86 if [ ! -f "$DEVICE" ]; then
87         echo >&2 "$PROGRAM: $DEVICE is not a file"
88         exit 1
89 fi
90
91 if [ ! -d "$MOUNTPOINT" ]; then
92         echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
93         exit 1
94 fi
95
96 # check magic
97 magic=$(head -n 1 "$DEVICE")
98 if [ "$magic" != "<<< innotek VirtualBox Disk Image >>>" ]; then
99         echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
100         exit 1
101 fi
102
103 MOUNTOPTIONS=
104 PARTITION=
105 OFFSET=
106 OLDIFS=$IFS
107 IFS=", "
108 for opt in $OPTIONS; do
109     KEY=${opt%=*};
110     VAL=${opt#*=};
111         case "$KEY" in
112         part|partition)
113                 PARTITION="$VAL"
114                 ;;
115         fstype)
116                 FSTYPE="$VAL"
117                 ;;
118         *)
119                 if [ -z "$MOUNTOPTIONS" ]; then
120                         MOUNTOPTIONS="$opt"
121                 else
122                         MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
123                 fi
124         ;;
125         esac
126 done
127 IFS="$OLDIFS"
128
129 if [ -z "$PARTITION" ]; then
130         echo >&2 "$PROGRAM: missing partition option"
131         exit 1
132 fi
133
134 # offset when disk starts
135 dskoff=8704
136
137 # find free loop device. XXX race possible
138 imgdev=$($LOSETUP -f)
139 $LOSETUP $imgdev -o $dskoff "$DEVICE"
140 # http://vserver.13thfloor.at/Stuff/QEMU/hdloop.sh
141 sfd=$($SFDISK -dump $imgdev 2>/dev/null | $SED -n '
142         /:/ s/[a-zA-Z]*=\ *\([0-9a-f]*\)\(,\|$\)/\1/g;
143         T;  s/^.*:\ //; p' | tr ' ' '.')
144 $LOSETUP -d $imgdev
145
146 getoffset() {
147         pstart=$1
148         psize=$2
149
150         blksize=512
151         offset=$((pstart*blksize))
152         size=$((psize*blksize))
153         echo $((offset+dskoff))
154 }
155
156 p=1
157 for a in $sfd; do
158         if [ "$p" = "$PARTITION" ]; then
159                 OFFSET=$(getoffset $(echo "$a" | tr '.' ' '))
160         fi
161         x=$((p++))
162 done
163
164 if [ -z "$OFFSET" ]; then
165         echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
166         exit 1
167 fi
168
169 if [ -z "$MOUNTOPTIONS" ]; then
170         MOUNTOPTIONS="loop,offset=$OFFSET"
171 else
172         MOUNTOPTIONS="$MOUNTOPTIONS,loop,offset=$OFFSET"
173 fi
174
175 # $MOUNTPOINT might not exist as mount can try to read it from /etc/fstab
176 $MOUNT $MOUNTARGS ${FSTYPE:+-t "$FSTYPE"} ${MOUNTOPTIONS:+-o "$MOUNTOPTIONS"} "$DEVICE" "$MOUNTPOINT"
177 if [ $? -ne 0 ]; then
178     echo >&2 "$PROGRAM: error mounting $DEVICE"
179 fi
This page took 0.039059 seconds and 4 git commands to generate.