]> git.pld-linux.org Git - packages/VirtualBox.git/blame - mount.vdi
- handle mount -n
[packages/VirtualBox.git] / mount.vdi
CommitLineData
299eae76
ER
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
25export PATH=/bin:/sbin:/usr/bin:/usr/sbin
26
27# Commands
28LOSETUP=/sbin/losetup
29MOUNT=/bin/mount
30
31PROGRAM=${0##*/}
7e899501 32MOUNTARGS=
299eae76
ER
33OPTIONS=
34FSTYPE=
35
36USAGE="VDI image mountpoint [-o options,...]
37
38Options:
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)
45if [ -n "$1" ] && [[ "$1" != -* ]]; then
46 DEVICE=$1
47 shift
48fi
49if [ -n "$1" ] && [[ "$1" != -* ]]; then
50 MOUNTPOINT=$1
51 shift
52fi
53
54while :; do
55 case "$1" in
56 -h | "-?" )
57 echo >&2 "Usage: $PROGRAM $USAGE"
58 exit 0 ;;
7e899501 59 -o)
299eae76
ER
60 OPTIONS="$2";
61 shift ;;
7e899501
ER
62 -n)
63 MOUNTARGS="$MOUNTARGS $1"
64 ;;
299eae76
ER
65 -?* )
66 echo >&2 "$PROGRAM: unrecognized option: $1"
67 exit 1 ;;
68 *)
69 break ;;
70 esac
71 shift
72done
73
74if [ -z "$DEVICE" ]; then
75 echo >&2 "$PROGRAM: device to mount not specified"
76 exit 1
77fi
78
79if [ -z "$MOUNTPOINT" ]; then
80 echo >&2 "$PROGRAM: mount point not specified"
81 exit 1
82fi
83
84if [ ! -f "$DEVICE" ]; then
85 echo >&2 "$PROGRAM: $DEVICE is not a file"
86 exit 1
87fi
88
89if [ ! -d "$MOUNTPOINT" ]; then
90 echo >&2 "$PROGRAM: $MOUNTPOINT is not a directory"
91 exit 1
92fi
93
94# check magic
95magic=$(head -n 1 "$DEVICE")
96if [ "$magic" != "<<< innotek VirtualBox Disk Image >>>" ]; then
97 echo >&2 "$PROGRAM: $DEVICE: bad magic, not VDI image"
98 exit 1
99fi
100
101MOUNTOPTIONS=
102PARTITION=
103OFFSET=
104OLDIFS=$IFS
105IFS=", "
106for 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
124done
125IFS="$OLDIFS"
126
127if [ -z "$PARTITION" ]; then
128 echo >&2 "$PROGRAM: missing partition option"
129 exit 1
130fi
131
132# offset when disk starts
133dskoff=8704
134
135# find free loop device. XXX race possible
136imgdev=$(losetup -f)
137losetup $imgdev -o $dskoff "$DEVICE"
138# http://vserver.13thfloor.at/Stuff/QEMU/hdloop.sh
139sfd=$(/sbin/sfdisk -dump $imgdev 2>/dev/null | sed -n '
140 /:/ s/[a-zA-Z]*=\ *\([0-9a-f]*\)\(,\|$\)/\1/g;
141 T; s/^.*:\ //; p' | tr ' ' '.')
142losetup -d $imgdev
143
144getoffset() {
145 pstart=$1
146 psize=$2
147
148 blksize=512
149 offset=$((pstart*blksize))
150 size=$((psize*blksize))
151 echo $((offset+dskoff))
152}
153
154p=1
155for a in $sfd; do
156 if [ "$p" = "$PARTITION" ]; then
157 OFFSET=$(getoffset $(echo "$a" | tr '.' ' '))
158 fi
159 x=$((p++))
160done
161
162if [ -z "$OFFSET" ]; then
163 echo >&2 "$PROGRAM: couldn't figure out offset, perhaps out of range?"
164 exit 1
165fi
166
167if [ -z "$MOUNTOPTIONS" ]; then
168 MOUNTOPTIONS="offset=$OFFSET"
169else
170 MOUNTOPTIONS="$MOUNTOPTIONS,offset=$OFFSET"
171fi
172
173# $MOUNTPOINT might not exist as mount can try to read it from /etc/fstab
7e899501 174"$MOUNT" $MOUNTARGS ${FSTYPE:+-t "$FSTYPE"} ${MOUNTOPTIONS:+-o "$MOUNTOPTIONS"} "$DEVICE" "$MOUNTPOINT"
299eae76
ER
175if [ $? -ne 0 ]; then
176 echo >&2 "$PROGRAM: error mounting $DEVICE"
177fi
This page took 0.241888 seconds and 4 git commands to generate.