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.

167 lines
5.7 KiB

  1. #!/bin/bash
  2. # create-f5-big-ip-image.sh
  3. # HS-Fulda - sebastian.rieger@informatik.hs-fulda.de
  4. #
  5. # changelog:
  6. #
  7. # V0.1 initial version
  8. # V0.2 changed hw_disk_bus to ide to fix resource provisioning
  9. # usage
  10. if [ ! $# -eq 2 ] ; then
  11. echo -e "usage: $0 <BIGIP-12.0.0.0.0.606.qcow2> <new glance image name>, e.g.:\n"
  12. echo "$0 BIGIP-12.0.0.0.0.606.qcow2 F5-BIGIP"
  13. exit -1
  14. fi
  15. # sudo check
  16. if [ ! $UID -eq 0 ] ; then
  17. echo "Insufficient privileges. Please consider using sudo -s."
  18. exit -1
  19. fi
  20. BIGIP_QCOW2=$1
  21. BIGIP_QCOW2_BASENAME=$(basename -s .qcow2 $1)
  22. BIGIP_PATCHED_QCOW2=$1-patched.qcow2
  23. GLANCE_IMAGE_NAME=$2
  24. GLANCE_IMAGE_RELEASE=$BIGIP_QCOW2_BASENAME
  25. TMP_NAME="BIGIP-$GLANCE_IMAGE_RELEASE"
  26. TIMESTAMP=$(date +%Y%m%d%H%M%S)
  27. function safe_unmount() {
  28. echo -n "Unmounting $1..."
  29. RETRY=0
  30. until umount $1 &>/dev/null
  31. do
  32. echo -n "."
  33. sleep 1
  34. RETRY=$((RETRY+1))
  35. if [ "$RETRY" -ge "5" ] ; then
  36. echo
  37. echo "ERROR: unable to unmount working directory $1"
  38. exit 1
  39. fi
  40. done
  41. echo
  42. return 0
  43. }
  44. # check for an existing image with the same name and offer to delete it prior to creating a new one
  45. CHECK_FOR_EXISTING_IMAGE=$(glance image-show $GLANCE_IMAGE_NAME 2>&1)
  46. if [ $? == 0 ] ; then
  47. glance image-show $GLANCE_IMAGE_NAME
  48. echo
  49. echo
  50. read -r -p "There is already an image with the same name in glance. Do you want to overwrite it? [y/N] " RESPONSE
  51. if [[ $RESPONSE =~ ^([yY][eE][sS]|[yY])$ ]] ; then
  52. echo "Deleting existing image $GLANCE_IMAGE_NAME..."
  53. echo "==========================================================="
  54. glance image-delete $GLANCE_IMAGE_NAME
  55. else
  56. echo "An image with the same name already exists. Either delete this image or choose another name."
  57. exit 1
  58. fi
  59. fi
  60. echo
  61. echo "Creating F5 BIGIP image..."
  62. echo "==========================================================="
  63. qemu-img convert -O raw $BIGIP_QCOW2 $BIGIP_QCOW2_BASENAME.raw
  64. LOOPDEV=$(kpartx -av $BIGIP_QCOW2_BASENAME.raw)
  65. LOOPDEV_PART1=$(echo "$LOOPDEV" | sed '1q;d' | cut -d " " -f 3)
  66. LOOPDEV_PART2=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
  67. LOOPDEV_PART3=$(echo "$LOOPDEV" | sed '3q;d' | cut -d " " -f 3)
  68. mkdir bigip-part1-boot-$TIMESTAMP
  69. # part2 is swap
  70. #mkdir bigip-part3-lvm-dat.log.1-$TIMESTAMP
  71. #mkdir bigip-part3-lvm-dat.maint.1-$TIMESTAMP
  72. #mkdir bigip-part3-lvm-dat.share.1-$TIMESTAMP
  73. #mkdir bigip-part3-lvm-dat.swapvol.1-$TIMESTAMP
  74. mkdir bigip-part3-lvm-set.1._config-$TIMESTAMP
  75. #mkdir bigip-part3-lvm-set.1._usr-$TIMESTAMP
  76. #mkdir bigip-part3-lvm-set.1._var-$TIMESTAMP
  77. #mkdir bigip-part3-lvm-set.1.root-$TIMESTAMP
  78. echo
  79. echo "Injecting changes to use serial console and startup script to get config..."
  80. echo "=================================================================================="
  81. mount /dev/mapper/$LOOPDEV_PART1 bigip-part1-boot-$TIMESTAMP
  82. # scan for new lvm volumes in part3
  83. pvscan
  84. # activate the new volume groups
  85. vgchange -ay
  86. mount /dev/mapper/vg--db--hda-set.1._config bigip-part3-lvm-set.1._config-$TIMESTAMP
  87. ## change grub to add kernel config console=ttyS0
  88. sed -i.bak -e s/"^splashimage="/"#splashimage="/g bigip-part1-boot-$TIMESTAMP/grub/grub.conf
  89. sed -i.bak -e s/"^timeout=8"/"timeout=8\nserial --unit=0 --speed=115200\nterminal --timeout=2 serial console"/g bigip-part1-boot-$TIMESTAMP/grub/grub.conf
  90. sed -i.bak -e s/"default_cpu_order quiet"/"default_cpu_order quiet console=ttyS0"/g bigip-part1-boot-$TIMESTAMP/grub/grub.conf
  91. ## append call to userscript in /config/startup
  92. cat << EOF >> bigip-part3-lvm-set.1._config-$TIMESTAMP/startup
  93. mkdir /virl-config
  94. mount /dev/hdd1 /virl-config
  95. chmod +x /virl-config/bigip-config.sh
  96. /virl-config/bigip-config.sh >/var/log/virl-startup.log
  97. EOF
  98. #DEBUG:
  99. # run bash to allow manual changes to the image before packing
  100. #
  101. #bash
  102. safe_unmount bigip-part1-boot-$TIMESTAMP
  103. #part2 is swap
  104. #safe_unmount bigip-part3-lvm-dat.log.1-$TIMESTAMP
  105. #safe_unmount bigip-part3-lvm-dat.maint.1-$TIMESTAMP
  106. #safe_unmount bigip-part3-lvm-dat.share.1-$TIMESTAMP
  107. #safe_unmount bigip-part3-lvm-dat.swapvol.1-$TIMESTAMP
  108. safe_unmount bigip-part3-lvm-set.1._config-$TIMESTAMP
  109. #safe_unmount bigip-part3-lvm-set.1._usr-$TIMESTAMP
  110. #safe_unmount bigip-part3-lvm-set.1._var-$TIMESTAMP
  111. #safe_unmount bigip-part3-lvm-set.1.root-$TIMESTAMP
  112. rm -rf bigip-part1-boot-$TIMESTAMP
  113. #part2 is swap
  114. #rm -rf bigip-part3-lvm-dat.log.1-$TIMESTAMP
  115. #rm -rf bigip-part3-lvm-dat.maint.1-$TIMESTAMP
  116. #rm -rf bigip-part3-lvm-dat.share.1-$TIMESTAMP
  117. #rm -rf bigip-part3-lvm-dat.swapvol.1-$TIMESTAMP
  118. rm -rf bigip-part3-lvm-set.1._config-$TIMESTAMP
  119. #rm -rf bigip-part3-lvm-set.1._usr-$TIMESTAMP
  120. #rm -rf bigip-part3-lvm-set.1._var-$TIMESTAMP
  121. #rm -rf bigip-part3-lvm-set.1.root-$TIMESTAMP
  122. # deactive part3 volume groups
  123. vgchange -an
  124. kpartx -d $BIGIP_QCOW2_BASENAME.raw
  125. echo
  126. echo "Saving F5 BIGIP image..."
  127. echo "==========================================================="
  128. qemu-img convert -O qcow2 $BIGIP_QCOW2_BASENAME.raw $BIGIP_PATCHED_QCOW2
  129. # use recommendations from https://support.f5.com/kb/en-us/products/big-ip_ltm/manuals/product/bigip-ve-kvm-setup-11-3-0/2.html#r_ve_vmware_1022_esx_mach_reqs
  130. glance image-create --container-format bare --disk-format qcow2 --visibility public --name $GLANCE_IMAGE_NAME \
  131. --file $BIGIP_PATCHED_QCOW2 --property hw_disk_bus=ide --property serial=1 \
  132. --property hw_vif_model=virtio --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=F5-BIGIP --property config_disk_type=disk
  133. # create default flavor
  134. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show F5-BIGIP.small 2>&1)
  135. if [ $? == 1 ]; then
  136. echo "Creating default flavor F5-BIGIP.small..."
  137. echo "==========================================================="
  138. nova flavor-create --is-public true F5-BIGIP.small auto 4096 0 2
  139. fi
  140. echo
  141. echo "Cleaning up..."
  142. echo "==========================================================="
  143. rm $BIGIP_QCOW2_BASENAME.raw