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.

166 lines
5.6 KiB

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