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.

134 lines
4.7 KiB

  1. #!/bin/bash
  2. # create-cumulus-vx-image.sh V0.3
  3. # HS-Fulda - sebastian.rieger@informatik.hs-fulda.de
  4. #
  5. # changelog:
  6. #
  7. # V0.2 added support to delete existing image with the same name and generating the default nova flavor
  8. # V0.3 checking whether it is safe to unmount the working directory
  9. # usage
  10. if [ ! $# -eq 2 ] ; then
  11. echo -e "usage: $0 <CumulusVX.qcow2> <new glance image name>, e.g.:\n"
  12. echo "$0 CumulusVX-2.5.3-f3df86c478e1a4ef.qcow2 CumulusVX"
  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. CUMULUS_QCOW2=$1
  21. CUMULUS_QCOW2_BASENAME=$(basename -s .qcow2 $1)
  22. CUMULUS_PATCHED_QCOW2=$1-patched.qcow2
  23. GLANCE_IMAGE_NAME=$2
  24. GLANCE_IMAGE_RELEASE=$CUMULUS_QCOW2_BASENAME
  25. TMP_NAME="CumulusVX-$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 CumulusVX image..."
  62. echo "==========================================================="
  63. qemu-img convert -O raw $CUMULUS_QCOW2 $CUMULUS_QCOW2_BASENAME.raw
  64. LOOPDEV=$(kpartx -av $CUMULUS_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. mkdir cumulusvx-boot-$TIMESTAMP
  68. mkdir cumulusvx-root-$TIMESTAMP
  69. echo
  70. echo "Injecting changes to use serial console and startup script to get switch config..."
  71. echo "=================================================================================="
  72. mount /dev/mapper/$LOOPDEV_PART1 cumulusvx-boot-$TIMESTAMP
  73. mount /dev/mapper/$LOOPDEV_PART2 cumulusvx-root-$TIMESTAMP
  74. # changing grub and inittab to use a serial console on kernel command line
  75. sed -i.bak -e s/"linux \/bzImage root=\/dev\/sda2"/"linux \/bzImage root=\/dev\/sda2 console=ttyS0 console=tty0"/g cumulusvx-root-$TIMESTAMP/vbox_grub.cfg
  76. sed -i.bak -e s/"linux \/bzImage root=\/dev\/sda2"/"linux \/bzImage root=\/dev\/sda2 console=ttyS0 console=tty0"/g cumulusvx-boot-$TIMESTAMP/grub/grub.cfg
  77. sed -i.bak -e s/"# S0:3:respawn:\/sbin\/getty -L \$(get-cmdline-console) vt100"/"S0:3:respawn:\/sbin\/getty -L \$(get-cmdline-console) vt100"/g cumulusvx-root-$TIMESTAMP/etc/inittab
  78. # append a script to import the configuration defined in VM Maestro to rc.local
  79. sed -i.bak -e s/"^exit 0$"/""/g cumulusvx-root-$TIMESTAMP/etc/rc.local
  80. cat << EOF >> cumulusvx-root-$TIMESTAMP/etc/rc.local
  81. mkdir /virl-config
  82. mount /dev/sdb1 /virl-config
  83. chmod +x /virl-config/cumulusvx.sh
  84. /virl-config/cumulusvx.sh >/var/log/virl-startup.log
  85. EOF
  86. #DEBUG:
  87. # run bash to allow manual changes to the image before packing
  88. #
  89. #bash
  90. safe_unmount cumulusvx-boot-$TIMESTAMP
  91. safe_unmount cumulusvx-root-$TIMESTAMP
  92. rm -rf cumulusvx-boot-$TIMESTAMP
  93. rm -rf cumulusvx-root-$TIMESTAMP
  94. kpartx -d $CUMULUS_QCOW2_BASENAME.raw
  95. echo
  96. echo "Saving CumulusVX image..."
  97. echo "==========================================================="
  98. qemu-img convert -O qcow2 $CUMULUS_QCOW2_BASENAME.raw $CUMULUS_PATCHED_QCOW2
  99. # use e1000 for now as with virtio we get dhcp errors due to "bad udp checksum" in Debian
  100. glance image-create --container-format bare --disk-format qcow2 --is-public true --name $GLANCE_IMAGE_NAME \
  101. --file $CUMULUS_PATCHED_QCOW2 --property hw_disk_bus=ide --property serial=1 \
  102. --property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=CumulusVX --property config_disk_type=disk
  103. # create default flavor
  104. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show CumulusVX.small 2>&1)
  105. if [ $? == 1 ]; then
  106. echo "Creating default flavor CumulusVX.small..."
  107. echo "==========================================================="
  108. nova flavor-create --is-public true CumulusVX.small auto 256 0 1
  109. fi
  110. echo
  111. echo "Cleaning up..."
  112. echo "==========================================================="
  113. rm $CUMULUS_QCOW2_BASENAME.raw