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.

232 lines
7.9 KiB

  1. #!/bin/bash
  2. # create-arista-veos-image.sh
  3. # HS-Fulda - sebastian.rieger@informatik.hs-fulda.de
  4. #
  5. # changelog:
  6. # V1.1 added injection of config defined in VM Maestro using config-drivex
  7. # V1.1.1 fixed device mapping of extracted partitions, fixed problems with stale swi directory
  8. # V1.1.2 rc.eos now supports e1000 and virtio as vnic types (virtio is supported in vEOS >=4.14.5F)
  9. # V1.2 added dynamic handling of device mapping of extacted partitions
  10. # V1.21 checking whether it safe to unmount working directories
  11. # V1.3 added support to delete existing image with the same name and generating the default nova flavor
  12. # V1.3.1 added support for newer glance releases (e.g. kilo) used in VIRL 1.0.0
  13. # V1.3.2 changed the extension of the bootloader iso to match the size of the partitions to be injected
  14. # V1.4 support for variable VEOS image sizes (as requested by @Jade_Deane to use custom VEOS images)
  15. # V1.4.1 fixed check for existing images
  16. # usage
  17. if [ ! $# -eq 3 ] ; then
  18. echo -e "usage: $0 <Aboot-veos-serial-version.iso> <vEOS-version.vmdk> <new glance image name>, e.g.:\n"
  19. echo "$0 Aboot-veos-serial-2.0.8.iso vEOS-4.13.4F.vmdk vEOS"
  20. exit -1
  21. fi
  22. # sudo check
  23. if [ ! $UID -eq 0 ] ; then
  24. echo "Insufficient privileges. Please consider using sudo -s."
  25. exit -1
  26. fi
  27. ABOOT_SERIAL_ISO=$1
  28. ABOOT_SERIAL_ISO_BASENAME=$(basename -s .iso $1)
  29. VEOS_VMDK=$2
  30. VEOS_VMDK_BASENAME=$(basename -s .vmdk $2)
  31. GLANCE_IMAGE_NAME=$3
  32. GLANCE_IMAGE_RELEASE=$VEOS_VMDK_BASENAME-$ABOOT_SERIAL_ISO_BASENAME
  33. TMP_NAME="vEOS-$GLANCE_IMAGE_RELEASE"
  34. TIMESTAMP=$(date +%Y%m%d%H%M%S)
  35. function safe_unmount() {
  36. echo -n "Unmounting $1..."
  37. RETRY=0
  38. until umount $1 &>/dev/null
  39. do
  40. echo -n "."
  41. sleep 1
  42. RETRY=$((RETRY+1))
  43. if [ "$RETRY" -ge "5" ] ; then
  44. echo
  45. echo "ERROR: unable to unmount working directory $1"
  46. exit 1
  47. fi
  48. done
  49. echo
  50. return 0
  51. }
  52. # check for an existing image with the same name and offer to delete it prior to creating a new one
  53. CHECK_FOR_EXISTING_IMAGE=$(glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME 2>&1)
  54. if [ $? == 0 ] ; then
  55. glance --os-image-api-version 1 image-show $GLANCE_IMAGE_NAME
  56. echo
  57. echo
  58. read -r -p "There is already an image with the same name in glance. Do you want to overwrite it? [y/N] " RESPONSE
  59. if [[ $RESPONSE =~ ^([yY][eE][sS]|[yY])$ ]] ; then
  60. echo "Deleting existing image $GLANCE_IMAGE_NAME..."
  61. echo "==========================================================="
  62. glance --os-image-api-version 1 image-delete $GLANCE_IMAGE_NAME
  63. else
  64. echo "An image with the same name already exists. Either delete this image or choose another name."
  65. exit 1
  66. fi
  67. fi
  68. echo
  69. echo "Creating vEOS image..."
  70. echo "==========================================================="
  71. # create a copy of Aboot bootloader and extend it to 3G
  72. cp $1 $TMP_NAME.raw
  73. echo
  74. echo "Extracting partitions from vEOS vmdk..."
  75. echo "==========================================================="
  76. # convert vmdk to raw and extract two partitions in it
  77. qemu-img convert -O raw $2 $VEOS_VMDK_BASENAME.raw
  78. LOOPDEV=$(kpartx -av $VEOS_VMDK_BASENAME.raw)
  79. LOOPDEV_PART1=$(echo "$LOOPDEV" | sed '1q;d' | cut -d " " -f 3)
  80. LOOPDEV_PART2=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
  81. dd if=/dev/mapper/$LOOPDEV_PART1 of=$VEOS_VMDK_BASENAME-p1.raw
  82. dd if=/dev/mapper/$LOOPDEV_PART2 of=$VEOS_VMDK_BASENAME-p2.raw
  83. kpartx -d $VEOS_VMDK_BASENAME.raw
  84. echo
  85. echo "Injecting rc.eos startup script to get switch config..."
  86. echo "==========================================================="
  87. # inject rc.eos script in first partition of the image, to get switch config defined in VM Maestro (config-drive)
  88. mkdir swi-$TIMESTAMP
  89. mount -o loop $VEOS_VMDK_BASENAME-p1.raw swi-$TIMESTAMP
  90. cd swi-$TIMESTAMP
  91. cat << EOF > rc.eos
  92. #!/bin/sh
  93. #
  94. # startup script to get node configs from VM Maestro
  95. #
  96. echo "Getting switch config from config drive..."
  97. echo "=========================================="
  98. mkdir /config-drive
  99. mount /dev/sdb1 /config-drive
  100. echo "Getting ip address for ma1 via dhcp..."
  101. echo "=========================================="
  102. MANAGEMENT_INTERFACE="ma1"
  103. ip link show \$MANAGEMENT_INTERFACE
  104. if [ \$? -ne 0 ]; then
  105. # if using virtio ma1 will not be up during Eos Init 1, hence we use eth0
  106. MANAGEMENT_INTERFACE="eth0"
  107. fi
  108. dhclient -r \$MANAGEMENT_INTERFACE
  109. dhclient -1 -v \$MANAGEMENT_INTERFACE >/mnt/flash/dhclient.log
  110. IP=\$(ip addr show \$MANAGEMENT_INTERFACE | grep inet | tr -s ' ' | cut -d ' ' -f 3 | sed s/"\/"/"\\\\\\\\\/"/g)
  111. echo \$IP
  112. sed s/"! ip of ma1 configured on launch"/"ip address \$IP"/g /config-drive/veos_config.txt >/mnt/flash/startup-config.tmp
  113. cat /mnt/flash/startup-config.tmp
  114. echo
  115. echo "Copying switch config from config drive..."
  116. echo "=========================================="
  117. cp /mnt/flash/startup-config.tmp /mnt/flash/startup-config
  118. EOF
  119. chmod 755 rc.eos
  120. cd ..
  121. safe_unmount swi-$TIMESTAMP
  122. rm -rf swi-$TIMESTAMP
  123. echo
  124. echo "Injecting new partitions from vEOS vmdk in Aboot image..."
  125. echo "==========================================================="
  126. # calulate size of the two partitions
  127. PART1_START=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw1" | tr -s " " | cut -d ' ' -f 3)
  128. PART1_END=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw1" | tr -s " " | cut -d ' ' -f 4)
  129. PART1_LENGTH=$(expr $PART1_END - $PART1_START)
  130. PART2_START=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw2" | tr -s " " | cut -d ' ' -f 2)
  131. PART2_END=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw2" | tr -s " " | cut -d ' ' -f 3)
  132. PART2_LENGTH=$(expr $PART2_END - $PART2_START)
  133. # extend the bootloader iso to be able to append the two partitions
  134. EXTENSION_SIZE=$(ls -lk $VEOS_VMDK_BASENAME.raw | tr -s " " | cut -d " " -f 5)
  135. truncate -s +$EXTENSION_SIZE $TMP_NAME.raw
  136. # append the two partitions from vmdk in the bootloader iso
  137. echo -e "n
  138. p
  139. +$PART1_LENGTH
  140. t
  141. 2
  142. c
  143. a
  144. 2
  145. n
  146. p
  147. +$PART2_LENGTH
  148. t
  149. 3
  150. 12
  151. w" | fdisk $TMP_NAME.raw >/dev/null
  152. # copy the partitions from vEOS vmdk to new image
  153. LOOPDEV=$(kpartx -av $TMP_NAME.raw)
  154. LOOPDEV_PART2=$(echo "$LOOPDEV" | sed '2q;d' | cut -d " " -f 3)
  155. LOOPDEV_PART3=$(echo "$LOOPDEV" | sed '3q;d' | cut -d " " -f 3)
  156. dd if=$VEOS_VMDK_BASENAME-p1.raw of=/dev/mapper/$LOOPDEV_PART2
  157. dd if=$VEOS_VMDK_BASENAME-p2.raw of=/dev/mapper/$LOOPDEV_PART3
  158. kpartx -d $TMP_NAME.raw
  159. echo
  160. echo "Convert new image to qcow2..."
  161. echo "==========================================================="
  162. # convert raw to qcow2
  163. qemu-img convert -O qcow2 $TMP_NAME.raw $TMP_NAME.qcow2
  164. echo
  165. echo "Cleaning up..."
  166. echo "==========================================================="
  167. #cleanup
  168. rm $TMP_NAME.raw
  169. rm $VEOS_VMDK_BASENAME-p1.raw
  170. rm $VEOS_VMDK_BASENAME-p2.raw
  171. rm $VEOS_VMDK_BASENAME.raw
  172. echo
  173. echo "Importing image into glance..."
  174. echo "==========================================================="
  175. glance image-create --container-format bare --disk-format qcow2 --visibility public --name $GLANCE_IMAGE_NAME \
  176. --file $TMP_NAME.qcow2 --property hw_disk_bus=ide --property serial=1 \
  177. --property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=IOSv --property config_disk_type=disk
  178. # create default flavor
  179. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show vEOS.small 2>&1)
  180. if [ $? == 1 ]; then
  181. echo "Creating default flavor vEOS.small..."
  182. echo "==========================================================="
  183. nova flavor-create --is-public true vEOS.small auto 1024 0 1
  184. fi
  185. CHECKING_FOR_EXISTING_FLAVOR=$(nova flavor-show vEOS.medium 2>&1)
  186. if [ $? == 1 ]; then
  187. echo "Creating default flavor vEOS.medium..."
  188. echo "==========================================================="
  189. nova flavor-create --is-public true vEOS.medium auto 2048 0 1
  190. fi
  191. #testing:
  192. #
  193. # nova boot --image "Arista vEOS Disk" --flavor m1.small veos --nic net-id=abc7ad47-55fd-4396-8d31-91dd4d41a18a --nic net-id=abc7ad47-55fd-4396-8d31-91dd4d41a18a --nic net-id=abc7ad47-55fd-4396-8d31-91dd4d41a18a --nic net-id=abc7ad47-55fd-4396-8d31-91dd4d41a18a --nic net-id=abc7ad47-55fd-4396-8d31-91dd4d41a18a
  194. #
  195. # using VM Maestro, the image can be chosen as "VM image", e.g., for an IOSv or IOSvL2 node