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.

130 lines
4.6 KiB

  1. #!/bin/bash
  2. # create-cumulus-vx-image.sh V0.2
  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. # usage
  9. if [ ! $# -eq 2 ] ; then
  10. echo -e "usage: $0 <CumulusVX.qcow2> <new glance image name>, e.g.:\n"
  11. echo "$0 CumulusVX-2.5.3-f3df86c478e1a4ef.qcow2 CumulusVX"
  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. CUMULUS_QCOW2=$1
  20. CUMULUS_QCOW2_BASENAME=$(basename -s .qcow2 $1)
  21. CUMULUS_PATCHED_QCOW2=$1-patched.qcow2
  22. GLANCE_IMAGE_NAME=$2
  23. GLANCE_IMAGE_RELEASE=$CUMULUS_QCOW2_BASENAME
  24. TMP_NAME="CumulusVX-$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 CumulusVX 2>&1)
  45. if [ $? == 0 ] ; then
  46. read -r -p "There is already an image with the same name in glance. Do you want to overwrite it? [y/N] " RESPONSE
  47. if [[ $RESPONSE =~ ^([yY][eE][sS]|[yY])$ ]] ; then
  48. echo "Deleting existing image $2..."
  49. echo "==========================================================="
  50. glance image-delete $2
  51. else
  52. echo "An image with the same name already exists. Either delete this image or choose another name."
  53. exit 1
  54. fi
  55. fi
  56. echo
  57. echo "Creating CumulusVX image..."
  58. echo "==========================================================="
  59. qemu-img convert -O raw $CUMULUS_QCOW2 $CUMULUS_QCOW2_BASENAME.raw
  60. LOOPDEV=$(kpartx -av $CUMULUS_QCOW2_BASENAME.raw)
  61. LOOPDEV_PART1=$(echo "$LOOPDEV" | sed '1q;d' | cut -d " " -f 3)
  62. LOOPDEV_PART2=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
  63. mkdir cumulusvx-boot-$TIMESTAMP
  64. mkdir cumulusvx-root-$TIMESTAMP
  65. echo
  66. echo "Injecting changes to use serial console and startup script to get switch config..."
  67. echo "=================================================================================="
  68. mount /dev/mapper/$LOOPDEV_PART1 cumulusvx-boot-$TIMESTAMP
  69. mount /dev/mapper/$LOOPDEV_PART2 cumulusvx-root-$TIMESTAMP
  70. # changing grub and inittab to use a serial console on kernel command line
  71. 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
  72. 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
  73. 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
  74. # append a script to import the configuration defined in VM Maestro to rc.local
  75. sed -i.bak -e s/"^exit 0$"/""/g cumulusvx-root-$TIMESTAMP/etc/rc.local
  76. cat << EOF >> cumulusvx-root-$TIMESTAMP/etc/rc.local
  77. mkdir /virl-config
  78. mount /dev/sdb1 /virl-config
  79. chmod +x /virl-config/cumulusvx.sh
  80. /virl-config/cumulusvx.sh >/var/log/virl-startup.log
  81. EOF
  82. #DEBUG:
  83. # run bash to allow manual changes to the image before packing
  84. #
  85. #bash
  86. safe_unmount cumulusvx-boot-$TIMESTAMP
  87. safe_unmount cumulusvx-root-$TIMESTAMP
  88. rm -rf cumulusvx-boot-$TIMESTAMP
  89. rm -rf cumulusvx-root-$TIMESTAMP
  90. kpartx -d $CUMULUS_QCOW2_BASENAME.raw
  91. echo
  92. echo "Saving CumulusVX image..."
  93. echo "==========================================================="
  94. qemu-img convert -O qcow2 $CUMULUS_QCOW2_BASENAME.raw $CUMULUS_PATCHED_QCOW2
  95. # use e1000 for now as with virtio we get dhcp errors due to "bad udp checksum" in Debian
  96. glance image-create --container-format bare --disk-format qcow2 --is-public true --name $GLANCE_IMAGE_NAME \
  97. --file $CUMULUS_PATCHED_QCOW2 --property hw_disk_bus=ide --property serial=1 \
  98. --property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=CumulusVX --property config_disk_type=disk
  99. # create default flavor
  100. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show CumulusVX.small 2>&1)
  101. if [ $? == 1 ]; then
  102. echo "Creating default flavor CumulusVX.small..."
  103. echo "==========================================================="
  104. nova flavor-create --is-public true CumulusVX.small auto 256 0 1
  105. fi
  106. echo
  107. echo "Cleaning up..."
  108. echo "==========================================================="
  109. rm $CUMULUS_QCOW2_BASENAME.raw