A custom modification of the virt-install-cloud.sh script located at https://github.com/clauded/virt-tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virt-install-cloud.sh 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/bash
  2. # Heavily Modified Script By: Brielle Bruns
  3. # Date: 07/03/2018
  4. # URL: https://git.sosdg.org/brielle/virt-install-cloud
  5. # Version: 1.1.1
  6. #
  7. # Originally based on:
  8. # virt-install-cloud.sh : script to start an OpenStack cloud image on kvm
  9. # version : 1.2.1
  10. #
  11. # Author : Claude Durocher
  12. # License : GPLv3
  13. #
  14. # ref. http://mojodna.net/2014/05/14/kvm-libvirt-and-ubuntu-14-04.html
  15. #
  16. # requires the following packages on Ubuntu host:
  17. # wget qemu-kvm libvirt-bin virtinst bridge-utils genisoimage|xorriso
  18. # requires the following packages on CentOS host:
  19. # wget qemu-kvm libvirt virt-install bridge-utils genisoimage|xorriso
  20. #
  21. # check if the script is run as root user
  22. if [[ $USER != "root" ]]; then
  23. echo "This script must be run as root!" && exit 1
  24. fi
  25. WORKING_DIR=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
  26. usage() {
  27. echo "${0} Help"
  28. echo -e "\t-h show this help"
  29. echo -e "\t-n hostname *"
  30. echo -e "\t-d domainame *"
  31. echo -e "\t-s disk size (num + count size - M=Megabytes G=Gigabytes) *"
  32. echo -e "\t-r ram size (in megabytes) *"
  33. echo -e "\t-c number of vcpus"
  34. echo -e "\t-b bridge to attach to *"
  35. echo -e "\t-x network info"
  36. echo -e "\t (type:ip:netmask:gateway - type=eni/1, ip=dhcp or address, default 1:dhcp)"
  37. echo -e "\t-i distribution to use"
  38. echo -e "\t (see script for supported options) - default is stretch"
  39. echo -e "\t-o OS type and variant (default: generic:generic - also set by distro option when available)"
  40. }
  41. while getopts "c:r:s:n:d:i:o:b:x:h" opt; do
  42. case $opt in
  43. c) cpucount=$OPTARG;;
  44. r) ram=$OPTARG;;
  45. s) storage=$OPTARG;;
  46. n) hostname=$OPTARG;;
  47. d) domain=$OPTARG;;
  48. i) distro=$OPTARG;;
  49. o) ostype=$OPTARG;;
  50. b) bridge=$OPTARG;;
  51. x) netinfo=$OPTARG;;
  52. h) help="yes";;
  53. esac
  54. done
  55. if [[ -z $ram || -z $storage || -z $hostname || -z $domain || -z $bridge || $help == "yes" ]]; then
  56. echo "Error: Incorrect command usage, must provide all * flags."
  57. usage
  58. exit 1
  59. fi
  60. if [[ -f ${WORKING_DIR}/config ]]; then
  61. source ${WORKING_DIR}/config
  62. else
  63. echo "Error: Missing ${WORKING_DIR}/config !"
  64. exit 1
  65. fi
  66. if [[ -f ${WORKING_DIR}/config.local ]]; then
  67. source ${WORKING_DIR}/config.local
  68. fi
  69. if [[ -z $distro ]]; then
  70. distro="stretch"
  71. fi
  72. if [[ -z $cpucount ]]; then
  73. cpucount="2"
  74. fi
  75. if [[ -z $netinfo ]]; then
  76. netinfo="1:dhcp"
  77. fi
  78. if [[ ! -z $netinfo ]]; then
  79. OIFS=${IFS}
  80. IFS=:
  81. read -ra netinfo_config <<< "$netinfo"
  82. virt_netinfo_type=${netinfo_config[0]}
  83. virt_netinfo_address=${netinfo_config[1]}
  84. virt_netinfo_netmask=${netinfo_config[2]}
  85. virt_netinfo_gateway=${netinfo_config[3]}
  86. IFS=${OIFS}
  87. echo "Configuring network as type=${virt_netinfo_type} addr=${virt_netinfo_address}/${virt_netinfo_netmask}/${virt_netinfo_gateway}"
  88. fi
  89. if [[ ! -z $ostype ]]; then
  90. OIFS=${IFS}
  91. IFS=:
  92. read -ra virt_os <<< "$ostype"
  93. virt_ostype=${virt_os[0]}
  94. virt_osvariant=${virt_os[1]}
  95. IFS=${OIFS}
  96. else
  97. virt_ostype="generic"
  98. virt_osvariant="generic"
  99. fi
  100. arch="amd64"
  101. source distros
  102. sed -e "s/%GUEST_NAME%/${hostname}/g" ${WORKING_DIR}/meta-data > ${WORKING_DIR}/output/meta-data
  103. sed -e "s/%FQDN%/${hostname}.${domain}/g" -e "s/%DOMAIN%/${domain}/g" -e "s/%NAMESERVER1%/${nameserver1}/g" -e "s/%NAMESERVER2%/${nameserver2}/g" ${WORKING_DIR}/user-data > ${WORKING_DIR}/output/user-data
  104. if [[ ${virt_netinfo_type} == "eni" ]]; then
  105. cat ${WORKING_DIR}/network/legacy-eni >> ${WORKING_DIR}/output/meta-data
  106. if [[ ${virt_netinfo_address} == "dhcp" ]]; then
  107. echo -e "\n iface eth0 inet dhcp" >> ${WORKING_DIR}/output/meta-data
  108. else
  109. echo -e "\n iface eth0 inet static" >> ${WORKING_DIR}/output/meta-data
  110. echo " address ${virt_netinfo_address}" >> ${WORKING_DIR}/output/meta-data
  111. echo " netmask ${virt_netinfo_netmask}" >> ${WORKING_DIR}/output/meta-data
  112. echo " gateway ${virt_netinfo_gateway}" >> ${WORKING_DIR}/output/meta-data
  113. fi
  114. elif [[ $virt_netinfo_type == "1" ]]; then
  115. if [[ ${virt_netinfo_address} == "dhcp" ]]; then
  116. sed -e "s/%DOMAIN%/${domain}/" ${WORKING_DIR}/network/network-config-dhcp-v1 > ${WORKING_DIR}/output/network-config
  117. extra_iso_files=" -map $WORKING_DIR/output/network-config network-config"
  118. else
  119. sed -e "s/%DOMAIN%/${domain}/g" -e "s/%IP_ADDRESS%/${virt_netinfo_address}/g" -e "s/%NETMASK%/${virt_netinfo_netmask}/g" \
  120. -e "s/%GATEWAY%/${virt_netinfo_gateway}/g" -e "s/%NAMESERVER1%/${nameserver1}/g" -e "s/%NAMESERVER2%/${nameserver2}/g" \
  121. ${WORKING_DIR}/network/network-config-v1 > ${WORKING_DIR}/output/network-config
  122. extra_iso_files_xorriso=" -map $WORKING_DIR/output/network-config network-config"
  123. extra_iso_files_genisoimage="${WORKING_DIR}/output/network-config"
  124. extra_iso_files_cloud_localds="--network-config ${WORKING_DIR}/output/network-config"
  125. fi
  126. elif [[ $virt_netinfo_type == "2" ]]; then
  127. echo "Not implemented yet!"
  128. fi
  129. if [[ ! -f images/${IMG_NAME} ]]; then
  130. echo "Downloading image ${IMG_NAME}..."
  131. wget ${IMG_URL}/${IMG_NAME} -O images/${IMG_NAME}
  132. chmod 644 ${IMG_NAME}
  133. else
  134. echo "Using existing image ${IMG_NAME}..."
  135. fi
  136. # check if pool exists, otherwise create it
  137. if [[ "$(virsh pool-list|grep ${POOL} -c)" -ne "1" ]]; then
  138. echo "Creating pool ${POOL}..."
  139. virsh pool-define-as --name ${POOL} --type dir --target ${POOL_PATH}
  140. virsh pool-autostart ${POOL}
  141. virsh pool-build ${POOL}
  142. virsh pool-start ${POOL}
  143. fi
  144. # write the two cloud-init files into an ISO
  145. echo "Preparing ISO file required by cloud-init..."
  146. case $iso_method in
  147. genisoimage)
  148. genisoimage -input-charset utf8 -output configuration.iso -volid cidata -joliet -rock ${WORKING_DIR}/output/user-data ${WORKING_DIR}/output/meta-data ${extra_iso_files_genisoimage}
  149. ;;
  150. xorriso)
  151. xorriso -in_charset utf8 -outdev configuration.iso -volid cidata -joliet on -rockridge on -map output/user-data user-data -map output/meta-data meta-data ${extra_iso_files_xorriso}
  152. ;;
  153. cloud-localds)
  154. cloud-localds ${extra_iso_files_cloud_localds} ${WORKING_DIR}/configuration.iso ${WORKING_DIR}/output/user-data ${WORKING_DIR}/output/meta-data
  155. ;;
  156. esac
  157. # keep a backup of the files for future reference
  158. if [[ "${BACKUP_ISO_FILES}" == "yes" ]]; then
  159. cp -f ${WORKING_DIR}/output/user-data ${WORKING_DIR}/backups/${hostname}.${domain}.user-data
  160. cp -f ${WORKING_DIR}/output/meta-data ${WORKING_DIR}/backups/${hostname}.${domain}.meta-data
  161. if [[ -f ${WORKING_DIR}/output/network-config ]]; then
  162. cp -f ${WORKING_DIR}/output/network-config ${WORKING_DIR}/backups/${hostname}.${domain}.network-config
  163. chmod 640 ${WORKING_DIR}/backups/${hostname}.${domain}.network-config
  164. fi
  165. chmod 640 ${WORKING_DIR}/backups/${hostname}.${domain}.user-data ${WORKING_DIR}/backups/${hostname}.${domain}.meta-data
  166. fi
  167. # copy ISO into libvirt's directory
  168. cp configuration.iso ${POOL_PATH}/${hostname}.${domain}.configuration.iso
  169. virsh pool-refresh ${POOL}
  170. # copy image to libvirt's pool
  171. if [[ ! -f ${POOL_PATH}/${IMG_NAME} ]]; then
  172. cp images/${IMG_NAME} ${POOL_PATH}
  173. virsh pool-refresh ${POOL}
  174. fi
  175. # clone cloud image
  176. virsh vol-clone --pool ${POOL} ${IMG_NAME} ${hostname}.${domain}.root.img
  177. virsh vol-resize --pool ${POOL} ${hostname}.${domain}.root.img ${storage}
  178. # convert image format
  179. if [[ "${CONVERT}" == "yes" ]]; then
  180. echo "Converting image to format ${FORMAT}..."
  181. qemu-img convert -O ${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT}
  182. rm ${POOL_PATH}/${hostname}.${domain}.root.img
  183. mv ${POOL_PATH}/${hostname}.${domain}.root.img.${FORMAT} ${POOL_PATH}/${hostname}.${domain}.root.img
  184. virsh pool-refresh ${POOL}
  185. fi
  186. echo "Creating guest ${hostname}.${domain}..."
  187. virt-install \
  188. --name ${hostname}.${domain} \
  189. --cpu ${cpumodel} \
  190. --memory ${ram} \
  191. --vcpus=${cpucount} \
  192. --noautoconsole \
  193. --nographics \
  194. --video=vga \
  195. --memballoon virtio \
  196. --network ${net_interface} \
  197. --boot hd \
  198. --disk vol=${POOL}/${hostname}.${domain}.root.img,format=${FORMAT},bus=virtio,cache=none \
  199. --disk vol=${POOL}/${hostname}.${domain}.configuration.iso,device=cdrom \
  200. --console pty,target_type=serial \
  201. --os-type=${virt_ostype} --os-variant=${virt_osvariant}
  202. sleep 5
  203. virsh change-media ${hostname}.${domain} hda --eject --config
  204. # cleanup
  205. rm configuration.iso output/meta-data output/user-data output/network-config