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.

152 lines
4.7 KiB

  1. #!/bin/bash
  2. # create-arista-veos-image.sh V1.1
  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. # usage
  8. if [ ! $# -eq 3 ] ; then
  9. echo -e "usage: $0 <Aboot-veos-serial-version.iso> <vEOS-version.vmdk> <new glance image name>, e.g.:\n"
  10. echo "$0 Aboot-veos-serial-2.0.8.iso vEOS-4.13.4F.vmdk vEOS"
  11. exit -1
  12. fi
  13. # sudo check
  14. if [ ! $UID -eq 0 ] ; then
  15. echo "Insufficient privileges. Please consider using sudo -s."
  16. exit -1
  17. fi
  18. ABOOT_SERIAL_ISO=$1
  19. ABOOT_SERIAL_ISO_BASENAME=$(basename -s .iso $1)
  20. VEOS_VMDK=$2
  21. VEOS_VMDK_BASENAME=$(basename -s .vmdk $2)
  22. GLANCE_IMAGE_NAME=$3
  23. GLANCE_IMAGE_RELEASE=$VEOS_VMDK_BASENAME-$ABOOT_SERIAL_ISO_BASENAME
  24. TMP_NAME="vEOS-$GLANCE_IMAGE_RELEASE"
  25. echo
  26. echo "Creating vEOS image..."
  27. echo "==========================================================="
  28. # create a copy of Aboot bootloader and extend it to 3G
  29. cp $1 $TMP_NAME.raw
  30. truncate -s +3G $TMP_NAME.raw
  31. echo
  32. echo "Extracting partitions from vEOS vmdk..."
  33. echo "==========================================================="
  34. # convert vmdk to raw and extract two partitions in it
  35. qemu-img convert -O raw $2 $VEOS_VMDK_BASENAME.raw
  36. kpartx -av $VEOS_VMDK_BASENAME.raw
  37. dd if=/dev/loop0p1 of=$VEOS_VMDK_BASENAME-p1.raw
  38. dd if=/dev/loop0p2 of=$VEOS_VMDK_BASENAME-p2.raw
  39. kpartx -d $VEOS_VMDK_BASENAME.raw
  40. echo
  41. echo "Injecting rc.eos startup script to get switch config..."
  42. echo "==========================================================="
  43. # inject rc.eos script in first partition of the image, to get switch config defined in VM Maestro (config-drive)
  44. mkdir swi
  45. mount -o loop $VEOS_VMDK_BASENAME-p1.raw swi
  46. cd swi
  47. cat << EOF > rc.eos
  48. #!/bin/sh
  49. #
  50. # startup script to get node configs from VM Maestro
  51. #
  52. echo "Getting switch config from config drive..."
  53. echo "=========================================="
  54. mkdir /config-drive
  55. mount /dev/sdb1 /config-drive
  56. echo "Getting ip address for ma1 via dhcp..."
  57. echo "=========================================="
  58. dhclient -r ma1
  59. dhclient -1 -v ma1 >/mnt/flash/dhclient.log
  60. IP=\$(ip addr show ma1 | grep inet | tr -s ' ' | cut -d ' ' -f 3 | sed s/"\/"/"\\\\\\\\\/"/g)
  61. echo \$IP
  62. sed s/"! ip of ma1 configured on launch"/"ip address \$IP"/g /config-drive/veos_config.txt >/mnt/flash/startup-config.tmp
  63. cat /mnt/flash/startup-config.tmp
  64. echo
  65. echo "Copying switch config from config drive..."
  66. echo "=========================================="
  67. cp /mnt/flash/startup-config.tmp /mnt/flash/startup-config
  68. EOF
  69. chmod 755 rc.eos
  70. cd ..
  71. umount swi
  72. rmdir swi
  73. echo
  74. echo "Injecting new partitions from vEOS vmdk in Aboot image..."
  75. echo "==========================================================="
  76. # calulate size of the two partitions
  77. PART1_START=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw1" | tr -s " " | cut -d ' ' -f 3)
  78. PART1_END=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw1" | tr -s " " | cut -d ' ' -f 4)
  79. PART1_LENGTH=$(expr $PART1_END - $PART1_START)
  80. PART2_START=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw2" | tr -s " " | cut -d ' ' -f 2)
  81. PART2_END=$(fdisk -l $VEOS_VMDK_BASENAME.raw | grep "\.raw2" | tr -s " " | cut -d ' ' -f 3)
  82. PART2_LENGTH=$(expr $PART2_END - $PART2_START)
  83. # append the two partitions from vmdk in the bootloader iso
  84. echo -e "n
  85. p
  86. +$PART1_LENGTH
  87. t
  88. 2
  89. c
  90. a
  91. 2
  92. n
  93. p
  94. +$PART2_LENGTH
  95. t
  96. 3
  97. 12
  98. w" | fdisk $TMP_NAME.raw >/dev/null
  99. # copy the partitions from vEOS vmdk to new image
  100. kpartx -av $TMP_NAME.raw
  101. dd if=$VEOS_VMDK_BASENAME-p1.raw of=/dev/loop0p2
  102. dd if=$VEOS_VMDK_BASENAME-p2.raw of=/dev/loop0p3
  103. kpartx -d $TMP_NAME.raw
  104. echo
  105. echo "Convert new image to qcow2..."
  106. echo "==========================================================="
  107. # convert raw to qcow2
  108. qemu-img convert -O qcow2 $TMP_NAME.raw $TMP_NAME.qcow2
  109. echo
  110. echo "Cleaning up..."
  111. echo "==========================================================="
  112. #cleanup
  113. rm $TMP_NAME.raw
  114. rm $VEOS_VMDK_BASENAME-p1.raw
  115. rm $VEOS_VMDK_BASENAME-p2.raw
  116. rm $VEOS_VMDK_BASENAME.raw
  117. echo
  118. echo "Importing image into glance..."
  119. echo "==========================================================="
  120. glance image-create --container-format bare --disk-format qcow2 --is-public true --name $GLANCE_IMAGE_NAME --file $TMP_NAME.qcow2 --property hw_disk_bus=ide --property serial=1 --property hw_vif_model=e1000 --property hw_cdrom_type=ide --property release="$GLANCE_IMAGE_RELEASE" --property subtype=IOSv --property config_disk_type=disk
  121. #testing:
  122. #
  123. # 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
  124. #
  125. # using VM Maestro, the image can be chosen as "VM image", e.g., for an IOSv or IOSvL2 node