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.

164 lines
5.9 KiB

  1. #!/bin/bash
  2. # create-cumulus-vx-image.sh
  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. # V0.3.1 added support for newer glance releases (e.g. kilo) used in VIRL 1.0.0
  10. # V0.4 fixed check for existing images, added support for Cumulus VX 3.x
  11. # usage
  12. if [ ! $# -eq 2 ] ; then
  13. echo -e "usage: $0 <CumulusVX.qcow2> <new glance image name>, e.g.:\n"
  14. echo "$0 CumulusVX-2.5.3-f3df86c478e1a4ef.qcow2 CumulusVX"
  15. exit -1
  16. fi
  17. # sudo check
  18. if [ ! $UID -eq 0 ] ; then
  19. echo "Insufficient privileges. Please consider using sudo -s."
  20. exit -1
  21. fi
  22. CUMULUS_QCOW2=$1
  23. CUMULUS_QCOW2_BASENAME=$(basename -s .qcow2 $1)
  24. CUMULUS_PATCHED_QCOW2=$1-patched.qcow2
  25. GLANCE_IMAGE_NAME=$2
  26. GLANCE_IMAGE_RELEASE=$CUMULUS_QCOW2_BASENAME
  27. TMP_NAME="CumulusVX-$GLANCE_IMAGE_RELEASE"
  28. TIMESTAMP=$(date +%Y%m%d%H%M%S)
  29. function safe_unmount() {
  30. echo -n "Unmounting $1..."
  31. RETRY=0
  32. until umount $1 &>/dev/null
  33. do
  34. echo -n "."
  35. sleep 1
  36. RETRY=$((RETRY+1))
  37. if [ "$RETRY" -ge "5" ] ; then
  38. echo
  39. echo "ERROR: unable to unmount working directory $1"
  40. exit 1
  41. fi
  42. done
  43. echo
  44. return 0
  45. }
  46. # check for an existing image with the same name and offer to delete it prior to creating a new one
  47. CHECK_FOR_EXISTING_IMAGE=$(glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME 2>&1)
  48. if [ $? == 0 ] ; then
  49. glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME
  50. echo
  51. echo
  52. read -r -p "There is already an image with the same name in glance. Do you want to overwrite it? [y/N] " RESPONSE
  53. if [[ $RESPONSE =~ ^([yY][eE][sS]|[yY])$ ]] ; then
  54. echo "Deleting existing image $GLANCE_IMAGE_NAME..."
  55. echo "==========================================================="
  56. glance --os-image-api-version 1 image-delete $GLANCE_IMAGE_NAME
  57. else
  58. echo "An image with the same name already exists. Either delete this image or choose another name."
  59. exit 1
  60. fi
  61. fi
  62. echo
  63. echo "Creating CumulusVX image..."
  64. echo "==========================================================="
  65. # check version
  66. if [[ "$CUMULUS_QCOW2_BASENAME" =~ ^cumulus-vx-2\.5 ]] ; then
  67. # handle 2.5.x versions
  68. CUMULUS_VERSION=2
  69. elif [[ "$CUMULUS_QCOW2_BASENAME" =~ ^cumulus-linux-3\. ]] ; then
  70. # handle 3.x versions
  71. CUMULUS_VERSION=3
  72. else
  73. echo "Unsupported version of Cumulus VX, currently this script supports versions 2.5.x and 3.x"
  74. exit 1
  75. fi
  76. qemu-img convert -O raw $CUMULUS_QCOW2 $CUMULUS_QCOW2_BASENAME.raw
  77. LOOPDEV=$(kpartx -av $CUMULUS_QCOW2_BASENAME.raw)
  78. if [ $CUMULUS_VERSION == 2 ]; then
  79. # partition layout Cumulus VX 2.5.x: part1 = boot, part2 = root
  80. LOOPDEV_PART_BOOT=$(echo "$LOOPDEV" | sed '1q;d' | cut -d " " -f 3)
  81. LOOPDEV_PART_ROOT=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
  82. elif [ $CUMULUS_VERSION == 3 ]; then
  83. # partition layout Cumulus VX 3.x: part1 = grubboot, part2 = onieboot, part3 = boot, part4 = root
  84. LOOPDEV_PART_BOOT=$(echo "$LOOPDEV" | sed '3q;d' | cut -d " " -f 3)
  85. LOOPDEV_PART_ROOT=$(echo "$LOOPDEV" | sed '4q;d' | cut -d " " -f 3)
  86. fi
  87. mkdir cumulusvx-boot-$TIMESTAMP
  88. mkdir cumulusvx-root-$TIMESTAMP
  89. echo
  90. echo "Injecting changes to use serial console and startup script to get switch config..."
  91. echo "=================================================================================="
  92. mount /dev/mapper/$LOOPDEV_PART_BOOT cumulusvx-boot-$TIMESTAMP
  93. mount /dev/mapper/$LOOPDEV_PART_ROOT cumulusvx-root-$TIMESTAMP
  94. if [ $CUMULUS_VERSION == 2 ]; then
  95. # changing grub and inittab to use a serial console on kernel command line
  96. 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
  97. 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
  98. 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
  99. elif [ $CUMULUS_VERSION == 3 ]; then
  100. # changing grub to show boot log
  101. sed -i.bak -e s/"console=tty0 quiet"/"console=tty0"/g cumulusvx-boot-$TIMESTAMP/grub/grub.cfg
  102. fi
  103. # append a script to import the configuration defined in VM Maestro to rc.local
  104. sed -i.bak -e s/"^exit 0$"/""/g cumulusvx-root-$TIMESTAMP/etc/rc.local
  105. cat << EOF >> cumulusvx-root-$TIMESTAMP/etc/rc.local
  106. mkdir /virl-config
  107. mount /dev/sdb1 /virl-config
  108. chmod +x /virl-config/cumulusvx.sh
  109. /virl-config/cumulusvx.sh >/var/log/virl-startup.log
  110. EOF
  111. #DEBUG:
  112. # run bash to allow manual changes to the image before packing
  113. #
  114. bash
  115. safe_unmount cumulusvx-boot-$TIMESTAMP
  116. safe_unmount cumulusvx-root-$TIMESTAMP
  117. rm -rf cumulusvx-boot-$TIMESTAMP
  118. rm -rf cumulusvx-root-$TIMESTAMP
  119. kpartx -d $CUMULUS_QCOW2_BASENAME.raw
  120. echo
  121. echo "Saving CumulusVX image..."
  122. echo "==========================================================="
  123. qemu-img convert -O qcow2 $CUMULUS_QCOW2_BASENAME.raw $CUMULUS_PATCHED_QCOW2
  124. # use e1000 for now as with virtio we get dhcp errors due to "bad udp checksum" in Debian
  125. glance image-create --container-format bare --disk-format qcow2 --visibility public --name $GLANCE_IMAGE_NAME \
  126. --file $CUMULUS_PATCHED_QCOW2 --property hw_disk_bus=ide --property serial=1 \
  127. --property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=CumulusVX --property config_disk_type=disk
  128. # create default flavor
  129. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show CumulusVX.small 2>&1)
  130. if [ $? == 1 ]; then
  131. echo "Creating default flavor CumulusVX.small..."
  132. echo "==========================================================="
  133. nova flavor-create --is-public true CumulusVX.small auto 256 0 1
  134. fi
  135. echo
  136. echo "Cleaning up..."
  137. echo "==========================================================="
  138. rm $CUMULUS_QCOW2_BASENAME.raw