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.

154 lines
4.9 KiB

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