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.

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