-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlkhh-mount
More file actions
executable file
·99 lines (87 loc) · 2.04 KB
/
lkhh-mount
File metadata and controls
executable file
·99 lines (87 loc) · 2.04 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /bin/bash
_rootdir=${_rootdir:-"$(dirname "$(readlink -f "$0")")"}
_ver="0.0.1-dev"
#_author="Wei Peng <me@1pengw.com>"
source "${_rootdir}/_shared_prologue"
#
# parameters
#
declare -i nbd=${nbd:-1}
declare image=${image:-arch}
declare part=${part:-2}
#
# getopt processing
#
function my_getopt_cont () {
local n_shift=1 # number of shifts in the outer loop; shift by 1 by default
case "$1" in
-n|--nbd)
shift
nbd=$1
n_shift=2
;;
-i|--image)
shift
image=$1
n_shift=2
;;
-p|--part)
shift
part=$1
n_shift=2
;;
esac
return $n_shift
}
my_getopt "n:i:p:" "nbd:,image:,part:" "my_getopt_cont" "Mount OS root partition from image." "$(
cat <<END
[-n|--nbd=<nbd>] [-i|--image=<image>] [-p|--part=<partition>]
END
)" "$(
cat <<END
-n --nbd Network Block Device (NBD) for loop-mount the image (default: $nbd).
-i --image Image (.img) under $LKHH_IMAGE that hosts the OS (default: $image).
-p --part Root partition number inside the image (default: $part).
END
)" "$@"
ret=$?
eval set -- "$_getopt"
shift $ret
# sanity check
vecho 0 <<END
$(
dumpparam <<END1
$(
for name in nbd image part; do
echo "$name=$(echo $(eval "echo \$$name"))"
done
)
END1
)
END
image_path="$LKHH_IMAGE/$image.img"
[[ -f "$image_path" ]] || die 1 <<END
ERROR: $image_path is not an existing regular file.
END
nbd_device="/dev/nbd${nbd}"
[[ -b "$nbd_device" ]] || die 1 <<END
ERROR: $nbd_device is not a block special device.
END
mnt="$LKHH_IMAGE/mnt/$nbd"
mkdir -p "$mnt"
[[ -d "$mnt" ]] || die 1 <<END
ERROR: $mnt is not an existing directory.
END
for exe in qemu-nbd partprobe sudo; do
type -p $exe >/dev/null || die 1 <<END
ERROR: $exe is not on PATH.
END
done
vecho 0 <<END
sudo qemu-nbd -c "$nbd_device" "$image_path"
sudo partprobe "$nbd_device"
sudo mount "${nbd_device}p${part}" "$mnt"
END
sudo qemu-nbd -c "$nbd_device" "$image_path"
sudo partprobe "$nbd_device"
sudo mount "${nbd_device}p${part}" "$mnt"