Integrated deployment configuration and documentation.
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.

virsh.sh 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. img_base_declare() {
  2. log Validating base image exists
  3. if ! virsh vol-key --pool "${VIRSH_POOL}" --vol airship-gate-base.img > /dev/null; then
  4. log Installing base image from "${BASE_IMAGE_URL}"
  5. cd "${TEMP_DIR}"
  6. curl -q -L -o base.img "${BASE_IMAGE_URL}"
  7. {
  8. virsh vol-create-as \
  9. --pool "${VIRSH_POOL}" \
  10. --name airship-gate-base.img \
  11. --format qcow2 \
  12. --capacity "${BASE_IMAGE_SIZE}" \
  13. --prealloc-metadata
  14. virsh vol-upload \
  15. --vol airship-gate-base.img \
  16. --file base.img \
  17. --pool "${VIRSH_POOL}"
  18. } &>> "${LOG_FILE}"
  19. fi
  20. }
  21. iso_gen() {
  22. NAME=${1}
  23. ADDL_USERDATA="${2}"
  24. if virsh vol-key --pool "${VIRSH_POOL}" --vol "cloud-init-${NAME}.iso" &> /dev/null; then
  25. log Removing existing cloud-init ISO for "${NAME}"
  26. virsh vol-delete \
  27. --pool "${VIRSH_POOL}" \
  28. --vol "cloud-init-${NAME}.iso" &>> "${LOG_FILE}"
  29. fi
  30. log "Creating cloud-init ISO for ${NAME}"
  31. ISO_DIR=${TEMP_DIR}/iso/${NAME}
  32. mkdir -p "${ISO_DIR}"
  33. cd "${ISO_DIR}"
  34. BR_IP_NODE=$(config_vm_ip "${NAME}")
  35. SSH_PUBLIC_KEY=$(ssh_load_pubkey)
  36. export BR_IP_NODE
  37. export NAME
  38. export SSH_PUBLIC_KEY
  39. export NTP_POOLS=$(join_array ',' $NTP_POOLS)
  40. export NTP_SERVERS=$(join_array ',' $NTP_SERVERS)
  41. envsubst < "${TEMPLATE_DIR}/user-data.sub" > user-data
  42. if [[ ! -z "${ADDL_USERDATA}" ]]
  43. then
  44. echo >> user-data
  45. echo -e "${ADDL_USERDATA}" >> user-data
  46. fi
  47. envsubst < "${TEMPLATE_DIR}/meta-data.sub" > meta-data
  48. export DNS_SERVERS=$(join_array ',' $UPSTREAM_DNS)
  49. envsubst < "${TEMPLATE_DIR}/network-config.sub" > network-config
  50. {
  51. genisoimage \
  52. -V cidata \
  53. -input-charset utf-8 \
  54. -joliet \
  55. -rock \
  56. -o cidata.iso \
  57. meta-data \
  58. network-config \
  59. user-data
  60. virsh vol-create-as \
  61. --pool "${VIRSH_POOL}" \
  62. --name "cloud-init-${NAME}.iso" \
  63. --capacity "$(stat -c %s "${ISO_DIR}/cidata.iso")" \
  64. --format raw
  65. virsh vol-upload \
  66. --pool "${VIRSH_POOL}" \
  67. --vol "cloud-init-${NAME}.iso" \
  68. --file "${ISO_DIR}/cidata.iso"
  69. } &>> "${LOG_FILE}"
  70. }
  71. iso_path() {
  72. NAME=${1}
  73. echo "${TEMP_DIR}/iso/${NAME}/cidata.iso"
  74. }
  75. net_clean() {
  76. if virsh net-list --name | grep ^airship_gate$ > /dev/null; then
  77. log Destroying Airship gate network
  78. virsh net-destroy "${XML_DIR}/network.xml" &>> "${LOG_FILE}"
  79. fi
  80. }
  81. net_declare() {
  82. if ! virsh net-list --name | grep ^airship_gate$ > /dev/null; then
  83. log Creating Airship gate network
  84. virsh net-define "${XML_DIR}/network.xml" &>> "${LOG_FILE}"
  85. virsh net-start airship_gate
  86. virsh net-autostart airship_gate
  87. fi
  88. }
  89. pool_declare() {
  90. log Validating virsh pool setup
  91. if ! virsh pool-uuid "${VIRSH_POOL}" &> /dev/null; then
  92. log Creating pool "${VIRSH_POOL}"
  93. virsh pool-define-as --name "${VIRSH_POOL}" --type dir --target "${VIRSH_POOL_PATH}" &>> "${LOG_FILE}"
  94. virsh pool-start "${VIRSH_POOL}"
  95. virsh pool-autostart "${VIRSH_POOL}"
  96. fi
  97. }
  98. vm_clean() {
  99. NAME=${1}
  100. if virsh list --name | grep "${NAME}" &> /dev/null; then
  101. virsh destroy "${NAME}" &>> "${LOG_FILE}"
  102. fi
  103. if virsh list --name --all | grep "${NAME}" &> /dev/null; then
  104. log Removing VM "${NAME}"
  105. virsh undefine --remove-all-storage --domain "${NAME}" &>> "${LOG_FILE}"
  106. fi
  107. }
  108. vm_clean_all() {
  109. log Removing all VMs
  110. VM_NAMES=($(config_vm_names))
  111. for NAME in ${VM_NAMES[*]}
  112. do
  113. vm_clean "${NAME}"
  114. done
  115. wait
  116. }
  117. vm_create() {
  118. NAME=${1}
  119. MAC_ADDRESS=$(config_vm_mac "${NAME}")
  120. IO_PROF=$(config_vm_io "${NAME}")
  121. if [[ "$IO_PROF" == "fast" ]]
  122. then
  123. DISK_OPTS="bus=virtio,cache=none,format=qcow2,io=native"
  124. elif [[ "$IO_PROF" == "safe" ]]
  125. then
  126. DISK_OPTS="bus=virtio,cache=directsync,discard=unmap,format=qcow2,io=native"
  127. else
  128. DISK_OPTS="bus=virtio,format=qcow2"
  129. fi
  130. vol_create_root "${NAME}"
  131. wait
  132. if [[ "$(config_vm_bootstrap ${NAME})" == "true" ]]; then
  133. iso_gen "${NAME}" "$(config_vm_userdata ${NAME})"
  134. wait
  135. log Creating VM "${NAME}" and bootstrapping the boot drive
  136. virt-install \
  137. --name "${NAME}" \
  138. --virt-type kvm \
  139. --cpu ${VIRSH_CPU_OPTS} \
  140. --graphics vnc,listen=0.0.0.0 \
  141. --noautoconsole \
  142. --network "network=airship_gate,model=virtio" \
  143. --mac="${MAC_ADDRESS}" \
  144. --vcpus "$(config_vm_vcpus ${NAME})" \
  145. --memory "$(config_vm_memory ${NAME})" \
  146. --import \
  147. --disk "vol=${VIRSH_POOL}/airship-gate-${NAME}.img,${DISK_OPTS}" \
  148. --disk "vol=${VIRSH_POOL}/cloud-init-${NAME}.iso,device=cdrom" &>> "${LOG_FILE}"
  149. ssh_wait "${NAME}"
  150. ssh_cmd "${NAME}" cloud-init status --wait
  151. ssh_cmd "${NAME}" sync
  152. else
  153. log Creating VM "${NAME}"
  154. virt-install \
  155. --name "${NAME}" \
  156. --virt-type kvm \
  157. --cpu ${VIRSH_CPU_OPTS} \
  158. --graphics vnc,listen=0.0.0.0 \
  159. --noautoconsole \
  160. --network "network=airship_gate,model=virtio" \
  161. --mac="${MAC_ADDRESS}" \
  162. --vcpus "$(config_vm_vcpus ${NAME})" \
  163. --memory "$(config_vm_memory ${NAME})" \
  164. --import \
  165. --disk "vol=${VIRSH_POOL}/airship-gate-${NAME}.img,${DISK_OPTS}" &>> "${LOG_FILE}"
  166. fi
  167. virsh autostart "${NAME}"
  168. }
  169. vm_create_validate() {
  170. NAME=${1}
  171. vm_create "${name}"
  172. if [[ "$(config_vm_bootstrap ${name})" == "true" ]]
  173. then
  174. vm_validate "${name}"
  175. fi
  176. }
  177. vm_create_all() {
  178. log Starting all VMs
  179. VM_NAMES=($(config_vm_names))
  180. for name in ${VM_NAMES[*]}
  181. do
  182. vm_create_validate "${name}" &
  183. done
  184. wait
  185. }
  186. vm_start() {
  187. NAME=${1}
  188. log Starting VM "${NAME}"
  189. virsh start "${NAME}" &>> "${LOG_FILE}"
  190. ssh_wait "${NAME}"
  191. }
  192. vm_stop() {
  193. NAME=${1}
  194. log Stopping VM "${NAME}"
  195. virsh destroy "${NAME}" &>> "${LOG_FILE}"
  196. }
  197. vm_stop_non_genesis() {
  198. log Stopping all non-genesis VMs in parallel
  199. for NAME in $(config_non_genesis_vms); do
  200. vm_stop "${NAME}" &
  201. done
  202. wait
  203. }
  204. vm_restart_all() {
  205. for NAME in $(config_vm_names); do
  206. vm_stop "${NAME}" &
  207. done
  208. wait
  209. for NAME in $(config_vm_names); do
  210. vm_start "${NAME}" &
  211. done
  212. wait
  213. }
  214. vm_validate() {
  215. NAME=${1}
  216. if ! virsh list --name | grep "${NAME}" &> /dev/null; then
  217. log VM "${NAME}" did not start correctly.
  218. exit 1
  219. fi
  220. }
  221. vol_create_root() {
  222. NAME=${1}
  223. if virsh vol-list --pool "${VIRSH_POOL}" | grep "airship-gate-${NAME}.img" &> /dev/null; then
  224. log Deleting previous volume "airship-gate-${NAME}.img"
  225. virsh vol-delete --pool "${VIRSH_POOL}" "airship-gate-${NAME}.img" &>> "${LOG_FILE}"
  226. fi
  227. log Creating root volume for "${NAME}"
  228. if [[ "$(config_vm_bootstrap ${NAME})" == "true" ]]; then
  229. virsh vol-create-as \
  230. --pool "${VIRSH_POOL}" \
  231. --name "airship-gate-${NAME}.img" \
  232. --capacity 64G \
  233. --format qcow2 \
  234. --backing-vol 'airship-gate-base.img' \
  235. --backing-vol-format qcow2 &>> "${LOG_FILE}"
  236. else
  237. virsh vol-create-as \
  238. --pool "${VIRSH_POOL}" \
  239. --name "airship-gate-${NAME}.img" \
  240. --capacity 64G \
  241. --format qcow2 &>> "${LOG_FILE}"
  242. fi
  243. }
  244. #Find the correct group name for libvirt access
  245. get_libvirt_group() {
  246. grep -oE '^libvirtd?:' /etc/group | tr -d ':'
  247. }
  248. # Make a user 'virtmgr' if it does not exist and add it to the libvirt group
  249. make_virtmgr_account() {
  250. for libvirt_group in $(get_libvirt_group)
  251. do
  252. if [[ -z "$(grep -oE '^virtmgr:' /etc/passwd)" ]]
  253. then
  254. sudo useradd -m -s /bin/sh -g "${libvirt_group}" virtmgr
  255. else
  256. sudo usermod -g "${libvirt_group}" virtmgr
  257. fi
  258. done
  259. }
  260. # Generate a new keypair
  261. gen_libvirt_key() {
  262. log Removing any existing virtmgr SSH keys
  263. sudo rm -rf ~virtmgr/.ssh
  264. log Generating new SSH keypair for virtmgr
  265. sudo mkdir -p ~virtmgr/.ssh
  266. sudo ssh-keygen -N '' -b 2048 -t rsa -f ~virtmgr/.ssh/airship_gate &>> "${LOG_FILE}"
  267. }
  268. # Install private key into site definition
  269. install_libvirt_key() {
  270. export PUB_KEY=$(sudo cat ~virtmgr/.ssh/airship_gate.pub)
  271. mkdir -p ${TEMP_DIR}/tmp
  272. envsubst < "${TEMPLATE_DIR}/authorized_keys.sub" > ${TEMP_DIR}/tmp/virtmgr.authorized_keys
  273. sudo cp ${TEMP_DIR}/tmp/virtmgr.authorized_keys ~virtmgr/.ssh/authorized_keys
  274. sudo chown -R virtmgr ~virtmgr/.ssh
  275. sudo chmod 700 ~virtmgr/.ssh
  276. sudo chmod 600 ~virtmgr/.ssh/authorized_keys
  277. mkdir -p "${GATE_DEPOT}"
  278. cat << EOF > ${GATE_DEPOT}/airship_drydock_kvm_ssh_key.yaml
  279. ---
  280. schema: deckhand/CertificateKey/v1
  281. metadata:
  282. schema: metadata/Document/v1
  283. name: airship_drydock_kvm_ssh_key
  284. layeringDefinition:
  285. layer: site
  286. abstract: false
  287. storagePolicy: cleartext
  288. data: |-
  289. EOF
  290. sudo cat ~virtmgr/.ssh/airship_gate | sed -e 's/^/ /' >> ${GATE_DEPOT}/airship_drydock_kvm_ssh_key.yaml
  291. }