-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkali-vmdk2tar.in
118 lines (104 loc) · 2.84 KB
/
kali-vmdk2tar.in
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
m4_include(utils)
usage() {
cat <<EOF
usage: ${0##*/} input [output]
-h Print this help message
-n <number> Partition number for rootfs, defaults to 1
EOF
}
partition=1
while getopts ':hn:' flag; do
case $flag in
h)
usage
exit 0
;;
n)
partition=$OPTARG
[[ $partition -ge 1 ]] || die "%s: invalid partition number" "${0##*/}"
;;
:)
die '%s: option requires an argument -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
?)
die '%s: invalid option -- '\''%s'\' "${0##*/}" "$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
die "%s: invalid parameters" "${0##*/}"
fi
input=$(realpath "$1" 2>/dev/null)
input_extension="${input##*.}"
output=$(realpath "$2" 2>/dev/null)
if [[ "$2" == "" ]]; then
output=$(basename "$input" .$input_extension)
output=$(dirname "$input")"/$output.tar.zst"
fi
compression=${output##*.}
taropt=""
case $compression in
zst | zstd)
taropt="--zstd"
;;
bz2 | bzip2)
taropt="--bzip2"
;;
xz)
taropt="--xz"
;;
lz)
taropt="--lzip"
;;
lzma)
taropt="--lzma"
;;
lzop)
taropt="--lzop"
;;
gz)
taropt="--gzip"
;;
esac
(( EUID == 0 )) || die 'This script must be run with root privileges'
[[ -f "$input" ]] || die "input is not a file"
[[ ! -e "$output" ]] || die "output file exists"
# load nbd kernel module
# first check if it has been loaded before
if lsmod | grep "nbd" &> /dev/null; then
nbdloaded="n"
else
modprobe nbd || die "%s: cannot load nbd kernel module" "${0##*/}"
while [ ! -e "/dev/nbd0" ]; do
sleep 1
done
nbdloaded="y"
fi
# https://stackoverflow.com/a/22608727
for x in /sys/class/block/nbd* ; do
S=$(cat $x/size)
if [ "$S" == "0" ] ; then
block=$(basename $x)
break
fi
done
# check for qemu-nbd
program_exists qemu-nbd || die "%s: this script depends on qemu-nbd, please install it before trying again" "${0##*/}"
qemu-nbd -c "/dev/$block" "$input" || die "%s: cannot connect '%s' to '%s'" "${0##*/}" "${block}p${partition}" "$input"
# wait until partition becomes available
while [ ! -e "/dev/${block}p${partition}" ]; do
sleep 1
done
mountpoint=$(mktemp -d /tmp/vmdk2tar.XXXXXXXXX)
mount -t ext4 -o ro "/dev/${block}p${partition}" "$mountpoint" || die "%s: cannot mount '%s'" "${0##*/}" "${block}p${partition}"
previouspwd=$PWD
cd $mountpoint
tar $taropt --one-file-system -cf $output . || die "%s: cannot make an archive from given rootfs"
cd $previouspwd
umount "$mountpoint" || die "%s: cannot unmount '%s'" "${0##*/}" "$mountpoint"
rmdir "$mountpoint" || true
qemu-nbd -d "/dev/$block" || die "%s: cannot disconnect '%s'" "${0##*/}" "${block}p${partition}"
# unload nbd module if it was loaded by this script
[[ $nbdloaded == "y" ]] && (rmmod nbd || die "%s: cannot unload nbd kernel module" "${0##*/}")