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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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-create "${XML_DIR}/network.xml" &>> "${LOG_FILE}"
  85. fi
  86. }
  87. pool_declare() {
  88. log Validating virsh pool setup
  89. if ! virsh pool-uuid "${VIRSH_POOL}" &> /dev/null; then
  90. log Creating pool "${VIRSH_POOL}"
  91. virsh pool-create-as --name "${VIRSH_POOL}" --type dir --target "${VIRSH_POOL_PATH}" &>> "${LOG_FILE}"
  92. fi
  93. }
  94. vm_clean() {
  95. NAME=${1}
  96. if virsh list --name | grep "${NAME}" &> /dev/null; then
  97. virsh destroy "${NAME}" &>> "${LOG_FILE}"
  98. fi
  99. if virsh list --name --all | grep "${NAME}" &> /dev/null; then
  100. log Removing VM "${NAME}"
  101. virsh undefine --remove-all-storage --domain "${NAME}" &>> "${LOG_FILE}"
  102. fi
  103. }
  104. vm_clean_all() {
  105. log Removing all VMs
  106. VM_NAMES=($(config_vm_names))
  107. for NAME in ${VM_NAMES[*]}
  108. do
  109. vm_clean "${NAME}"
  110. done
  111. wait
  112. }
  113. vm_create() {
  114. NAME=${1}
  115. MAC_ADDRESS=$(config_vm_mac "${NAME}")
  116. IO_PROF=$(config_vm_io "${NAME}")
  117. if [[ "$IO_PROF" == "fast" ]]
  118. then
  119. DISK_OPTS="bus=virtio,cache=none,format=qcow2,io=native"
  120. elif [[ "$IO_PROF" == "safe" ]]
  121. then
  122. DISK_OPTS="bus=virtio,cache=directsync,discard=unmap,format=qcow2,io=native"
  123. else
  124. DISK_OPTS="bus=virtio,format=qcow2"
  125. fi
  126. vol_create_root "${NAME}"
  127. wait
  128. if [[ "$(config_vm_bootstrap ${NAME})" == "true" ]]; then
  129. iso_gen "${NAME}" "$(config_vm_userdata ${NAME})"
  130. wait
  131. log Creating VM "${NAME}" and bootstrapping the boot drive
  132. virt-install \
  133. --name "${NAME}" \
  134. --virt-type kvm \
  135. --cpu ${VIRSH_CPU_OPTS} \
  136. --graphics vnc,listen=0.0.0.0 \
  137. --noautoconsole \
  138. --network "network=airship_gate,model=virtio" \
  139. --mac="${MAC_ADDRESS}" \
  140. --vcpus "$(config_vm_vcpus ${NAME})" \
  141. --memory "$(config_vm_memory ${NAME})" \
  142. --import \
  143. --disk "vol=${VIRSH_POOL}/airship-gate-${NAME}.img,${DISK_OPTS}" \
  144. --disk "vol=${VIRSH_POOL}/cloud-init-${NAME}.iso,device=cdrom" &>> "${LOG_FILE}"
  145. ssh_wait "${NAME}"
  146. ssh_cmd "${NAME}" cloud-init status --wait
  147. ssh_cmd "${NAME}" sync
  148. else
  149. log Creating VM "${NAME}"
  150. virt-install \
  151. --name "${NAME}" \
  152. --virt-type kvm \
  153. --cpu ${VIRSH_CPU_OPTS} \
  154. --graphics vnc,listen=0.0.0.0 \
  155. --noautoconsole \
  156. --network "network=airship_gate,model=virtio" \
  157. --mac="${MAC_ADDRESS}" \
  158. --vcpus "$(config_vm_vcpus ${NAME})" \
  159. --memory "$(config_vm_memory ${NAME})" \
  160. --import \
  161. --disk "vol=${VIRSH_POOL}/airship-gate-${NAME}.img,${DISK_OPTS}" &>> "${LOG_FILE}"
  162. fi
  163. }
  164. vm_create_validate() {
  165. NAME=${1}
  166. vm_create "${name}"
  167. if [[ "$(config_vm_bootstrap ${name})" == "true" ]]
  168. then
  169. vm_validate "${name}"
  170. fi
  171. }
  172. vm_create_all() {
  173. log Starting all VMs
  174. VM_NAMES=($(config_vm_names))
  175. for name in ${VM_NAMES[*]}
  176. do
  177. vm_create_validate "${name}" &
  178. done
  179. wait
  180. }
  181. vm_start() {
  182. NAME=${1}
  183. log Starting VM "${NAME}"
  184. virsh start "${NAME}" &>> "${LOG_FILE}"
  185. ssh_wait "${NAME}"
  186. }
  187. vm_stop() {
  188. NAME=${1}
  189. log Stopping VM "${NAME}"
  190. virsh destroy "${NAME}" &>> "${LOG_FILE}"
  191. }
  192. vm_stop_non_genesis() {
  193. log Stopping all non-genesis VMs in parallel
  194. for NAME in $(config_non_genesis_vms); do
  195. vm_stop "${NAME}" &
  196. done
  197. wait
  198. }
  199. vm_restart_all() {
  200. for NAME in $(config_vm_names); do
  201. vm_stop "${NAME}" &
  202. done
  203. wait
  204. for NAME in $(config_vm_names); do
  205. vm_start "${NAME}" &
  206. done
  207. wait
  208. }
  209. vm_validate() {
  210. NAME=${1}
  211. if ! virsh list --name | grep "${NAME}" &> /dev/null; then
  212. log VM "${NAME}" did not start correctly.
  213. exit 1
  214. fi
  215. }
  216. vol_create_root() {
  217. NAME=${1}
  218. if virsh vol-list --pool "${VIRSH_POOL}" | grep "airship-gate-${NAME}.img" &> /dev/null; then
  219. log Deleting previous volume "airship-gate-${NAME}.img"
  220. virsh vol-delete --pool "${VIRSH_POOL}" "airship-gate-${NAME}.img" &>> "${LOG_FILE}"
  221. fi
  222. log Creating root volume for "${NAME}"
  223. if [[ "$(config_vm_bootstrap ${NAME})" == "true" ]]; then
  224. virsh vol-create-as \
  225. --pool "${VIRSH_POOL}" \
  226. --name "airship-gate-${NAME}.img" \
  227. --capacity 64G \
  228. --format qcow2 \
  229. --backing-vol 'airship-gate-base.img' \
  230. --backing-vol-format qcow2 &>> "${LOG_FILE}"
  231. else
  232. virsh vol-create-as \
  233. --pool "${VIRSH_POOL}" \
  234. --name "airship-gate-${NAME}.img" \
  235. --capacity 64G \
  236. --format qcow2 &>> "${LOG_FILE}"
  237. fi
  238. }
  239. #Find the correct group name for libvirt access
  240. get_libvirt_group() {
  241. grep -oE '^libvirtd?:' /etc/group | tr -d ':'
  242. }
  243. # Make a user 'virtmgr' if it does not exist and add it to the libvirt group
  244. make_virtmgr_account() {
  245. for libvirt_group in $(get_libvirt_group)
  246. do
  247. if [[ -z "$(grep -oE '^virtmgr:' /etc/passwd)" ]]
  248. then
  249. sudo useradd -m -s /bin/sh -g "${libvirt_group}" virtmgr
  250. else
  251. sudo usermod -g "${libvirt_group}" virtmgr
  252. fi
  253. done
  254. }
  255. # Generate a new keypair
  256. gen_libvirt_key() {
  257. log Removing any existing virtmgr SSH keys
  258. sudo rm -rf ~virtmgr/.ssh
  259. log Generating new SSH keypair for virtmgr
  260. sudo mkdir -p ~virtmgr/.ssh
  261. sudo ssh-keygen -N '' -b 2048 -t rsa -f ~virtmgr/.ssh/airship_gate &>> "${LOG_FILE}"
  262. }
  263. # Install private key into site definition
  264. install_libvirt_key() {
  265. export PUB_KEY=$(sudo cat ~virtmgr/.ssh/airship_gate.pub)
  266. mkdir -p ${TEMP_DIR}/tmp
  267. envsubst < "${TEMPLATE_DIR}/authorized_keys.sub" > ${TEMP_DIR}/tmp/virtmgr.authorized_keys
  268. sudo cp ${TEMP_DIR}/tmp/virtmgr.authorized_keys ~virtmgr/.ssh/authorized_keys
  269. sudo chown -R virtmgr ~virtmgr/.ssh
  270. sudo chmod 700 ~virtmgr/.ssh
  271. sudo chmod 600 ~virtmgr/.ssh/authorized_keys
  272. mkdir -p "${GATE_DEPOT}"
  273. cat << EOF > ${GATE_DEPOT}/airship_drydock_kvm_ssh_key.yaml
  274. ---
  275. schema: deckhand/CertificateKey/v1
  276. metadata:
  277. schema: metadata/Document/v1
  278. name: airship_drydock_kvm_ssh_key
  279. layeringDefinition:
  280. layer: site
  281. abstract: false
  282. storagePolicy: cleartext
  283. data: |-
  284. EOF
  285. sudo cat ~virtmgr/.ssh/airship_gate | sed -e 's/^/ /' >> ${GATE_DEPOT}/airship_drydock_kvm_ssh_key.yaml
  286. }