-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_image.sh
executable file
·86 lines (72 loc) · 2.07 KB
/
create_image.sh
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
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
function do_help() {
cat <<EOF
usage: $(basename $0) [-h] [-z <zfs>] [-v <version>] -t <file name> -n <image name>
required options:
-t <name> : name of the target zvol file and json manifest file for image
-n <name> : name of the image
options:
-z <zfs> : ZFS zvol name (default: zones/win_sysprep)
-v <version> : version of the image (default: YYMMDD)
-h : Display this help message
EOF
}
while getopts ":hz:n:t:v:" opt
do
case ${opt} in
h ) do_help
exit 0
;;
z ) VM_ZPOOL="${OPTARG}"
;;
t ) TARGET="${OPTARG}"
;;
v ) VERSION="${OPTARG}"
;;
n ) NAME="${OPTARG}"
;;
\? ) echo "Invalid option: $OPTARG" 1>&2
exit 1
;;
: ) echo "Invalid option: $OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
# defaults
_VM_ZPOOL=${VM_ZPOOL:-"zones/win_sysprep"}
TS=$(date +"%Y%m%d")
_VERSION=${VERSION:-"$TS"}
#input checks
zfs list "${_VM_ZPOOL}" 2> /dev/null 1>&2 || ( echo "ZFS ${_VM_ZPOOL} does not exist. Sysprep one first" 1>&2; exit 1)
_TARGET_ZVOL_FILE="${TARGET}.zvol"
_TARGET_JSON_FILE="${TARGET}.json"
[[ ${TARGET} =~ \.zvol$ ]] && _TARGET_ZVOL_FILE="${TARGET}"
[[ ${TARGET} =~ \.zvol$ ]] && _TARGET_JSON_FILE="${TARGET/zvol$/json}"
[ -f "${_TARGET_ZVOL_FILE}" ] && ( echo "Target ${_TARGET_ZVOL_FILE} already exists." 1>&2; exit 1)
[ -f "${_TARGET_JSON_FILE}" ] && ( echo "Target ${_TARGET_JSON_FILE} already exists." 1>&2; exit 1)
echo -n "Sending zvol to file: ${_TARGET_ZVOL_FILE} ... "
SHA=$( zfs send ${_VM_ZPOOL} | tee "${_TARGET_ZVOL_FILE}" | digest -a sha1 )
echo "done"
SIZE=$( ls -l "${_TARGET_ZVOL_FILE}" | awk '{print $5}')
UUID=$( uuidgen )
echo "Creating image manifest"
cat <<EOF > "${_TARGET_JSON_FILE}"
{
"v": 2,
"uuid": "${UUID}",
"name": "${NAME}",
"version": "${_VERSION}",
"type": "zvol",
"os": "windows",
"files": [ {
"sha1": "${SHA}",
"size": ${SIZE},
"compression": "none"
} ]
}
EOF
echo "Install image manually with imgadm install or with the install_images.sh script to generate vmadm json at the same time"