Browse Source

Update to include getopts use, add distros config

Brielle Bruns 10 months ago
parent
commit
0e6a6e540c
2 changed files with 147 additions and 105 deletions
  1. 64
    0
      distros
  2. 83
    105
      virt-install-cloud.sh

+ 64
- 0
distros View File

@@ -0,0 +1,64 @@
1
+case $distro in
2
+  trusty)   IMG_USER="ubuntu"
3
+            IMG_URL="http://cloud-images.ubuntu.com/releases/14.04/release"
4
+            IMG_NAME="ubuntu-14.04-server-cloudimg-${ARCH}-disk1.img"
5
+            ;;
6
+  xenial)   IMG_USER="ubuntu"
7
+            IMG_URL="http://cloud-images.ubuntu.com/releases/16.04/release"
8
+            IMG_NAME="ubuntu-16.04-server-cloudimg-${ARCH}-disk1.img"
9
+            ;;
10
+  artful)   IMG_USER="ubuntu"
11
+            IMG_URL="http://cloud-images.ubuntu.com/releases/17.10/release"
12
+            IMG_NAME="ubuntu-17.10-server-cloudimg-${ARCH}-disk1.img"
13
+            ;;
14
+  bionic)    IMG_USER="ubuntu"
15
+            IMG_URL="http://cloud-images.ubuntu.com/releases/18.04/release"
16
+            IMG_NAME="ubuntu-18.04-server-cloudimg-${ARCH}.img"
17
+            ;;
18
+  centos6)  IMG_USER="centos"
19
+            IMG_URL="https://cloud.centos.org/centos/6/images"
20
+            if [[ $arch = "amd64" ]]; then
21
+              IMG_NAME="CentOS-6-x86_64-GenericCloud.qcow2"
22
+            else
23
+              echo "Cloud image not available!"; exit 1
24
+            fi
25
+            ;;
26
+  centos7)  IMG_USER="centos"
27
+            IMG_URL="https://cloud.centos.org/centos/7/images"
28
+            if [[ $arch = "amd64" ]]; then
29
+              IMG_NAME="CentOS-7-x86_64-GenericCloud.qcow2"
30
+            else
31
+              echo "Cloud image not available!"; exit 1
32
+            fi
33
+            ;;
34
+  fedora27) IMG_USER="fedora"
35
+            if [[ $arch = "amd64" ]]; then
36
+              IMG_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/27/CloudImages/x86_64/images/"
37
+              IMG_NAME="Fedora-Cloud-Base-27-1.6.x86_64.qcow2"
38
+            else
39
+              echo "Cloud image not available!"; exit 1
40
+            fi
41
+            ;;
42
+  jessie)   IMG_USER="debian"
43
+            if [[ $arch = "amd64" ]]; then
44
+              IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-8"
45
+              IMG_NAME="debian-8-openstack-amd64.qcow2"
46
+				virt_ostype="debian"
47
+				virt_osvariant="debian8"
48
+            else
49
+              echo "Cloud image not available!"; exit 1
50
+            fi
51
+            ;;
52
+  stretch)  IMG_USER="debian"
53
+            if [[ $arch = "amd64" ]]; then
54
+              IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-9"
55
+              IMG_NAME="debian-9-openstack-amd64.qcow2"
56
+				virt_ostype="debian"
57
+				virt_osvariant="debian9"
58
+            else
59
+              echo "Cloud image not available!"; exit 1
60
+            fi
61
+            ;;
62
+  *)        echo "Cloud image not available!"; exit 1
63
+            ;;
64
+esac

+ 83
- 105
virt-install-cloud.sh View File

@@ -1,16 +1,16 @@
1 1
 #!/bin/bash
2
+# Heavily Modified Script By:  Brielle Bruns
3
+# Date: 04/11/2018
4
+# URL: https://git.sosdg.org/brielle/virt-install-cloud
5
+# Version: 1.0
2 6
 #
7
+# Originally based on:
3 8
 # virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
4 9
 # version : 1.1
5 10
 #
6 11
 # Author : Claude Durocher
7 12
 # License : GPLv3
8 13
 #
9
-#
10
-# Heavily Modified By:  Brielle Bruns
11
-# Date: 04/10/2018
12
-# URL: https://git.sosdg.org/brielle/virt-install-cloud
13
-#
14 14
 # ref. http://mojodna.net/2014/05/14/kvm-libvirt-and-ubuntu-14-04.html
15 15
 #
16 16
 # requires the following packages on Ubuntu host:
@@ -19,33 +19,76 @@
19 19
 #  wget qemu-kvm libvirt virt-install bridge-utils genisoimage|xorriso
20 20
 #
21 21
 
22
-# Example CLI
23
-# virt-install-cloud.sh hostname domain disk-size ramsize
24
-
25 22
 # check if the script is run as root user
26 23
 if [[ $USER != "root" ]]; then
27 24
   echo "This script must be run as root!" && exit 1
28 25
 fi
29 26
 
30
-# image selection : trusty, precise, centos7, fedora26, ...
31
-IMG="stretch"
32
-# architecture : amd64 or i386
33
-ARCH="amd64"
27
+while getopts "cr:s:n:d:iob" opt; do
28
+    case $opt in
29
+	c) cpucount=("$OPTARG");;
30
+    r) ram=("$OPTARG");;
31
+	s) storage=("$OPTARG");;
32
+    n) hostname=("$OPTARG");;
33
+    d) domain=("$OPTARG");;
34
+	i) distro=("$OPTARG");;
35
+	o) ostype=("$OPTARG");;
36
+	b) bridge=("$OPTARG");;
37
+	h) usage;;
38
+    esac
39
+done
40
+
41
+usage() {
42
+	echo "${0} Help"
43
+	echo "-h show this help"
44
+	echo "-n hostname *"
45
+	echo "-d domainame *"
46
+	echo "-s disk size (num + count size - M=Megabytes G=Gigabytes) *"
47
+	echo "-r ram size (in kilobytes) *"
48
+	echo "-c number of vcpus"
49
+	echo "-b bridge to attach to *"
50
+	echo "-i distribution to use (see script for supported options) - default is stretch"
51
+	echo "-o OS type and variant (default: generic:generic - also set by distro option when available)"
52
+}
53
+
54
+if [[ -z $ram || -z $storage || -z $hostname || -z $domain  || -z $bridge ]]; then
55
+	echo "Error: Incorrect command usage, must provide all * flags."
56
+	usage
57
+	exit 1
58
+fi
59
+
60
+if [[ -z $distro ]]; then
61
+	distro="stretch"
62
+fi
63
+
64
+if [[ -z $cpucount ]]; then
65
+	cpucount="2"
66
+fi
67
+
68
+if [[ ! -z $ostype ]]; then
69
+	OIFS=${IFS}
70
+	IFS=:
71
+	read -ra virt_os <<< "$ostype"
72
+	virt_ostype=${virt_os[0]}
73
+	virt_osvariant=${virt_os[1]}
74
+	IFS=${OIFS}
75
+else
76
+	virt_ostype="generic"
77
+	virt_osvariant="generic"
78
+fi
79
+
80
+arch="amd64"
81
+
82
+source distros
34 83
 
35
-OS_TYPE="debian"
36
-OS_VARIANT="debian9"
37 84
 
38 85
 # kvm defaults pool paths
39 86
 DEF_POOL=images
40 87
 DEF_POOL_PATH=/data/images
41 88
 # vm prefs : specify vm preferences for your guest
42 89
 BACKUP_ISO_FILES=no # yes or no
43
-GUEST_NAME=${1}
44
-DOMAIN=${2}
45
-VROOTDISKSIZE=${3}
46
-VCPUS=2
47
-VMEM=${4}
48
-NETWORK="bridge=vlan2-bridge,model=virtio"
90
+
91
+net_interface="bridge=${bridge},model=virtio"
49 92
 # guest image format: qcow2 or raw
50 93
 FORMAT=raw
51 94
 # convert image format : yes or no
@@ -54,69 +97,9 @@ CONVERT=yes
54 97
 POOL=images
55 98
 POOL_PATH=/data/images
56 99
 
57
-sed -e "s/%GUEST_NAME%/${GUEST_NAME}/" meta-data > output/meta-data
58
-sed -e "s/%FQDN%/${GUEST_NAME}.${DOMAIN}/" user-data > output/user-data
59
-
60
-case $IMG in
61
-  trusty)   IMG_USER="ubuntu"
62
-            IMG_URL="http://cloud-images.ubuntu.com/releases/14.04/release"
63
-            IMG_NAME="ubuntu-14.04-server-cloudimg-${ARCH}-disk1.img"
64
-            ;;
65
-  xenial)   IMG_USER="ubuntu"
66
-            IMG_URL="http://cloud-images.ubuntu.com/releases/16.04/release"
67
-            IMG_NAME="ubuntu-16.04-server-cloudimg-${ARCH}-disk1.img"
68
-            ;;
69
-  artful)   IMG_USER="ubuntu"
70
-            IMG_URL="http://cloud-images.ubuntu.com/releases/17.10/release"
71
-            IMG_NAME="ubuntu-17.10-server-cloudimg-${ARCH}-disk1.img"
72
-            ;;
73
-  bionic)    IMG_USER="ubuntu"
74
-            IMG_URL="http://cloud-images.ubuntu.com/releases/18.04/release"
75
-            IMG_NAME="ubuntu-18.04-server-cloudimg-${ARCH}.img"
76
-            ;;
77
-  centos6)  IMG_USER="centos"
78
-            IMG_URL="https://cloud.centos.org/centos/6/images"
79
-            if [[ $ARCH = "amd64" ]]; then
80
-              IMG_NAME="CentOS-6-x86_64-GenericCloud.qcow2"
81
-            else
82
-              echo "Cloud image not available!"; exit 1
83
-            fi
84
-            ;;
85
-  centos7)  IMG_USER="centos"
86
-            IMG_URL="https://cloud.centos.org/centos/7/images"
87
-            if [[ $ARCH = "amd64" ]]; then
88
-              IMG_NAME="CentOS-7-x86_64-GenericCloud.qcow2"
89
-            else
90
-              echo "Cloud image not available!"; exit 1
91
-            fi
92
-            ;;
93
-  fedora27) IMG_USER="fedora"
94
-            if [[ $ARCH = "amd64" ]]; then
95
-              IMG_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/27/CloudImages/x86_64/images/"
96
-              IMG_NAME="Fedora-Cloud-Base-27-1.6.x86_64.qcow2"
97
-            else
98
-              echo "Cloud image not available!"; exit 1
99
-            fi
100
-            ;;
101
-  jessie)   IMG_USER="debian"
102
-            if [[ $ARCH = "amd64" ]]; then
103
-              IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-8"
104
-              IMG_NAME="debian-8-openstack-amd64.qcow2"
105
-            else
106
-              echo "Cloud image not available!"; exit 1
107
-            fi
108
-            ;;
109
-  stretch)  IMG_USER="debian"
110
-            if [[ $ARCH = "amd64" ]]; then
111
-              IMG_URL="https://cdimage.debian.org/cdimage/openstack/current-9"
112
-              IMG_NAME="debian-9-openstack-amd64.qcow2"
113
-            else
114
-              echo "Cloud image not available!"; exit 1
115
-            fi
116
-            ;;
117
-  *)        echo "Cloud image not available!"; exit 1
118
-            ;;
119
-esac
100
+sed -e "s/%GUEST_NAME%/${hostname}/" meta-data > output/meta-data
101
+sed -e "s/%FQDN%/${hostname}.${domain}/" user-data > output/user-data
102
+
120 103
 
121 104
 if [[ ! -f images/${IMG_NAME} ]]; then
122 105
   echo "Downloading image ${IMG_NAME}..."
@@ -142,12 +125,12 @@ xorriso -in_charset utf8 -outdev configuration.iso -volid cidata -joliet on -roc
142 125
 
143 126
 # keep a backup of the files for future reference
144 127
 if [[ "${BACKUP_ISO_FILE}" == "yes" ]]; then
145
-  cp user-data ${GUEST_NAME}.user-data
146
-  cp meta-data ${GUEST_NAME}.meta-data
147
-  chmod 640 ${GUEST_NAME}.user-data ${GUEST_NAME}.meta-data
128
+  cp user-data ${hostname}.user-data
129
+  cp meta-data ${hostname}.meta-data
130
+  chmod 640 ${hostname}.user-data ${hostname}.meta-data
148 131
 fi
149 132
 # copy ISO into libvirt's directory
150
-cp configuration.iso ${POOL_PATH}/${GUEST_NAME}.configuration.iso
133
+cp configuration.iso ${POOL_PATH}/${hostname}.${domain}.configuration.iso
151 134
 virsh pool-refresh ${POOL}
152 135
 
153 136
 # copy image to libvirt's pool
@@ -157,38 +140,33 @@ if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
157 140
 fi
158 141
 
159 142
 # clone cloud image
160
-virsh vol-clone --pool ${POOL} ${IMG_NAME} ${GUEST_NAME}.root.img
161
-virsh vol-resize --pool ${POOL} ${GUEST_NAME}.root.img ${VROOTDISKSIZE}
143
+virsh vol-clone --pool ${POOL} ${IMG_NAME} ${hostname}.${domain}.root.img
144
+virsh vol-resize --pool ${POOL} ${hostname}.${domain}.root.img ${storage}
162 145
 
163 146
 # convert image format
164 147
 if [[ "${CONVERT}" == "yes" ]]; then
165 148
   echo "Converting image to format ${FORMAT}..."
166
-  qemu-img convert -O ${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT}
167
-  rm ${POOL_PATH}/${GUEST_NAME}.root.img
168
-  mv ${POOL_PATH}/${GUEST_NAME}.root.img.${FORMAT} ${POOL_PATH}/${GUEST_NAME}.root.img
149
+  qemu-img convert -O ${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT}
150
+  rm ${POOL_PATH}/${hostname}.${domain}.root.img
151
+  mv ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img
169 152
   virsh pool-refresh ${POOL}
170 153
 fi
171 154
 
172
-echo "Creating guest ${GUEST_NAME}..."
155
+echo "Creating guest ${hostname}.${domain}..."
173 156
 virt-install \
174
-  --name ${GUEST_NAME} \
157
+  --name ${hostname}.${domain} \
175 158
   --cpu host \
176
-  --ram ${VMEM} \
177
-  --vcpus=${VCPUS} \
159
+  --ram ${ram} \
160
+  --vcpus=${cpucount} \
178 161
   --noautoconsole \
179 162
   --memballoon virtio \
180
-  --network ${NETWORK} \
163
+  --network ${net_interface} \
181 164
   --boot hd \
182
-  --disk vol=${POOL}/${GUEST_NAME}.root.img,format=${FORMAT},bus=virtio,cache=none \
183
-  --disk vol=${POOL}/${GUEST_NAME}.configuration.iso,bus=virtio \
165
+  --disk vol=${POOL}/${hostname}.${domain}.root.img,format=${FORMAT},bus=virtio,cache=none \
166
+  --disk vol=${POOL}/${hostname}.${domain}.configuration.iso,bus=virtio \
184 167
   --console pty,target_type=serial \
185
-  --os-type=${OS_TYPE} --os-variant=${OS_VARIANT}
168
+  --os-type=${virt_ostype} --os-variant=${virt_osvariant}
186 169
 
187
-# display result
188
-echo
189
-echo "List of running VMs :"
190
-echo
191
-virsh list
192 170
 
193 171
 # cleanup
194 172
 rm configuration.iso output/meta-data output/user-data

Loading…
Cancel
Save