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.

231 lines
7.8 KiB

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