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.

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