]> git.pld-linux.org Git - packages/VirtualBox.git/blob - mount.vdi
- handle mount -n
[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
31 PROGRAM=${0##*/}
32 MOUNTARGS=
33 OPTIONS=
34 FSTYPE=
35
36 USAGE="VDI image mountpoint [-o options,...]
37
38 Options:
39       partition=<number>              Specify partition to mount
40       fstype=<fstype>                 Specify FS type instead of autodetecting
41 "
42
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
46         DEVICE=$1
47         shift
48 fi
49 if [ -n "$1" ] && [[ "$1" != -* ]]; then
50         MOUNTPOINT=$1
51         shift
52 fi
53
54 while :; do
55         case "$1" in
56         -h | "-?" )
57                 echo >&2 "Usage: $PROGRAM $USAGE"
58                 exit 0 ;;
59         -o)
60                 OPTIONS="$2";
61                 shift ;;
62         -n)
63                 MOUNTARGS="$MOUNTARGS $1"
64                 ;;
65         -?* )
66                 echo >&2 "$PROGRAM: unrecognized option: $1"
67                 exit 1 ;;
68         *)
69                 break ;;
70         esac
71         shift
72 done
73
74 if [ -z "$DEVICE" ]; then
75         echo >&2 "$PROGRAM: device to mount not specified"
76         exit 1
77 fi
78
79 if [ -z "$MOUNTPOINT" ]; then
80         echo >&2 "$PROGRAM: mount point not specified"
81         exit 1
82 fi
83
84 if [ ! -f "$DEVICE" ]; then
85         echo >&2 "$PROGRAM: $DEVICE is not a file"
86         exit 1
87 fi
88
89 if [ ! -d "$MOUNTPOINT" ]; then
90         echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
91         exit 1
92 fi
93
94 # check magic
95 magic=$(head -n 1 "$DEVICE")
96 if [ "$magic" != "<<< innotek VirtualBox Disk Image >>>" ]; then
97         echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
98         exit 1
99 fi
100
101 MOUNTOPTIONS=
102 PARTITION=
103 OFFSET=
104 OLDIFS=$IFS
105 IFS=", "
106 for opt in $OPTIONS; do
107     KEY=${opt%=*};
108     VAL=${opt#*=};
109         case "$KEY" in
110         part|partition)
111                 PARTITION="$VAL"
112                 ;;
113         fstype)
114                 FSTYPE="$VAL"
115                 ;;
116         *)
117                 if [ -z "$MOUNTOPTIONS" ]; then
118                         MOUNTOPTIONS="$opt"
119                 else
120                         MOUNTOPTIONS="$MOUNTOPTIONS,$opt"
121                 fi
122         ;;
123         esac
124 done
125 IFS="$OLDIFS"
126
127 if [ -z "$PARTITION" ]; then
128         echo >&2 "$PROGRAM: missing partition option"
129         exit 1
130 fi
131
132 # offset when disk starts
133 dskoff=8704
134
135 # find free loop device. XXX race possible
136 imgdev=$(losetup -f)
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 ' ' '.')
142 losetup -d $imgdev
143
144 getoffset() {
145         pstart=$1
146         psize=$2
147
148         blksize=512
149         offset=$((pstart*blksize))
150         size=$((psize*blksize))
151         echo $((offset+dskoff))
152 }
153
154 p=1
155 for a in $sfd; do
156         if [ "$p" = "$PARTITION" ]; then
157                 OFFSET=$(getoffset $(echo "$a" | tr '.' ' '))
158         fi
159         x=$((p++))
160 done
161
162 if [ -z "$OFFSET" ]; then
163         echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
164         exit 1
165 fi
166
167 if [ -z "$MOUNTOPTIONS" ]; then
168         MOUNTOPTIONS="offset=$OFFSET"
169 else
170         MOUNTOPTIONS="$MOUNTOPTIONS,offset=$OFFSET"
171 fi
172
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"
177 fi
This page took 0.035872 seconds and 4 git commands to generate.