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