-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall
executable file
·111 lines (91 loc) · 2.41 KB
/
install
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
#!/usr/bin/env bash
CONFIG=config
TEMPDIR=$(mktemp -d isoXXXX)
if [ -f "$CONFIG" ]; then
. "$CONFIG"
else
echo "No config file found."
exit 1
fi
INSTANCE_ID=v01
if [ -z "${1}" ]; then
USER_DATA=user-data
else
USER_DATA="$1"
fi
cp "$USER_DATA" "$TEMPDIR"
if [ -z "${POOL_NAME}" ]; then
POOL_NAME=default
fi
if [ -z "${POOL_PATH}" ]; then
POOL_PATH=$( virsh pool-dumpxml $POOL_NAME | xmllint --xpath '//target/path/text()' - | head -n 1 )
fi
if [ -z "${RAM}" ]; then
RAM=1024
fi
if [ -z "${VCPUS}" ]; then
VCPUS=1
fi
if [ -z "${GRAPHICS}" ]; then
GRAPHICS=false
fi
if [ ${GRAPHICS} = true ]; then
GRAPHICS="vnc,listen=0.0.0.0"
else
GRAPHICS=none
fi
# Scrape hostname from user-data file
# From RFC 952 http://tools.ietf.org/html/rfc1123
HOSTNAME_REGEX='(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'
HOSTNAME=$( grep -i 'hostname' "$USER_DATA" | grep -iEo "$HOSTNAME_REGEX" | tail -1 )
cat <<EOF > ${TEMPDIR}/meta-data
instance-id: ${HOSTNAME}-${INSTANCE_ID}
local-hostname: $HOSTNAME
hostname: $HOSTNAME
EOF
cd $TEMPDIR
genisoimage -output "${HOSTNAME}-config.iso" -volid cidata -joliet -rock user-data meta-data
cd - > /dev/null
virsh vol-create-as \
--pool $POOL_NAME \
--name "${HOSTNAME}-config.iso" \
--capacity 5242880 \
--format raw
virsh vol-upload \
--vol ${HOSTNAME}-config.iso \
--pool $POOL_NAME \
--file "${TEMPDIR}/${HOSTNAME}-config.iso"
rm -rf $TEMPDIR
BASE_SIZE=$( qemu-img info "$BASE_IMG" | grep -Eo '[0-9]+ bytes' | grep -Eo '[0-9]+' )
virsh vol-create-as \
--pool $POOL_NAME \
--name ${HOSTNAME}.img \
--capacity $BASE_SIZE \
--format qcow2 \
--prealloc-metadata
virsh vol-upload \
--vol ${HOSTNAME}.img \
--pool $POOL_NAME \
--file "$BASE_IMG"
if [ -n "${DISK}" ]; then
virsh vol-resize \
--vol ${HOSTNAME}.img \
--pool $POOL_NAME \
--capacity "$DISK"
fi
echo Installing:
echo "Hostname: $HOSTNAME"
echo "Pool Name: $POOL_NAME"
echo "Pool Path: $POOL_PATH"
virt-install \
--name $HOSTNAME \
--ram $RAM \
--vcpus $VCPUS \
--cpu host \
--graphics ${GRAPHICS} \
--autostart \
--memballoon virtio \
--network bridge=br0,model=virtio \
--boot hd \
--disk vol=${POOL_NAME}/${HOSTNAME}.img,format=qcow2,bus=virtio,cache=writeback \
--disk vol=${POOL_NAME}/${HOSTNAME}-config.iso,bus=virtio