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.

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