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.

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