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.

227 lines
7.5 KiB

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