-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkvm-mount
More file actions
executable file
·77 lines (59 loc) · 1.76 KB
/
kvm-mount
File metadata and controls
executable file
·77 lines (59 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
#
# Mount a raw kvm image. Assumes it's partitioned, and the partition to mount is the last one.
PARTCMD='tail -n1'
# Set usage output
USAGE="[-h |--help] [-f | --first-partition ] [<img-file>]"
LONGUSAGE="\t-h, --help\n\t\tPrint this help message
\t-f, --first-partition\n\t\tMount the first partition, rather than the last
\t<img-file>\n\t\tImage file to mount (defaults to IMGFILE from kvm-run.conf)"
# Standard functions
source ${HOME}/.scripts/functions.sh
# Script name
ME=$(basename $0)
# Parse arguments
ARGS=`getopt -o hf --long help,first-partition -n "${ME}" -- "$@"`
if [ $? != 0 ] ; then
usage "invalid arguments"
fi
eval set -- "$ARGS"
while true ; do
case "$1" in
-h|--help) usage; shift ;;
-f|--first-partition) FIRSTPART='yes'; shift ;;
--) shift ; break ;;
* ) usage "Invalid argument $1";;
esac
done
# Remaining arguments are in $1, $2, etc. as normal
if [ -n "${1}" ]; then
IMGFILE="${1}"
elif [ ! -f kvm-run.conf ]; then
die "No img-file given, and no kvm-run.conf"
else
source kvm-run.conf
# Sets IMGFILE
fi
if [ -z "${IMGFILE}" ]; then
die "IMGFILE empty!"
fi
if [ -z "$(file ${IMGFILE} | grep "x86 boot sector")" ]; then
die "${IMGFILE} is not a raw image file"
fi
if [ -n "$FIRSTPART" ]; then
PARTCMD='head -n1'
fi
STARTSECTOR=$(file ${IMGFILE} | grep -o "startsector [0-9]*" | ${PARTCMD} | awk '{print $2}')
ROOTOFFSET=$((${STARTSECTOR} * 512))
if [ ! -d mnt ]; then
sudo mkdir mnt
fi
sudo mount -o loop,offset=${ROOTOFFSET} ${IMGFILE} mnt/
if [ ! -d mnt/boot ]; then
die "No boot; not mounted"
fi
if [ -z "$FIRSTPART" ]; then
STARTSECTOR=$(file ${IMGFILE} | grep -o "startsector [0-9]*" | head -n 1 | awk '{print $2}')
BOOTOFFSET=$((${STARTSECTOR} * 512))
sudo mount -o loop,offset=${BOOTOFFSET} ${IMGFILE} mnt/boot/
fi