Advertisement
Guest User

Untitled

a guest
Jun 8th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. #!/bin/bash -eux
  2. # vi: ts=4 noexpandtab
  3. #
  4. # Generate a generic Vagrant Box.
  5. #
  6. # Vagrant images are essentially nothing more than OVA's with extra-metadata.
  7. #
  8. # We can't use the OVA's for Vagrant since Vagrant uses SSH to modify the instance.
  9. # This build step creates a cloud-config ISO so that Cloud-Init will configure
  10. # the initial user, creates meta-data that tells Vagrant how to interact with
  11. # the cloud-init created users, and finally create the OVA.
  12. #
  13. # For this step, we re-use the VMDK's made in 040-vmdk-image.binary
  14.  
  15. cur_d=${PWD}
  16. my_d=$(dirname $(readlink -f ${0}))
  17.  
  18. base_vmdk="livecd.ubuntu-cpc.disk1.vmdk"
  19.  
  20. case $ARCH in
  21. amd64|i386) ;;
  22. *)
  23. echo "Vagrant images are not supported for $ARCH"
  24. exit 0
  25. esac
  26.  
  27. if [ ! -e ${base_vmdk} ]; then
  28. echo "Did not find VMDK to produce Vagrant images."
  29. exit 0
  30. fi
  31.  
  32. . /build/config/functions
  33.  
  34. # Virtualbox is needed for making a small VMDK
  35. apt-get -qqy install genisoimage qemu-utils
  36.  
  37. # Lets be safe about this
  38. box_d=$(mktemp -d)
  39. seed_d=$(mktemp -d)
  40. trap "rm -rf ${box_d} ${seed_d}" EXIT
  41.  
  42. # Used to identify bits
  43. suite=$(chroot chroot lsb_release -c -s)
  44. version=$(chroot chroot lsb_release --release --short)
  45. distro=$(chroot chroot lsb_release --id --short | tr [:upper:] [:lower:])
  46.  
  47. # Get the VMDK in place
  48. prefix="${distro}-${suite}-${version}-cloudimg"
  49. vmdk_f="${box_d}/${prefix}.vmdk"
  50. cp ${base_vmdk} ${vmdk_f}
  51.  
  52. # Vagrant needs a base user. We either inject the well-known SSH key
  53. # or use password authentication. Both are ugly. So we'll use a password
  54. # and make it random. This obviously is insecure...but at least its
  55. # better than the alternatives.
  56. ubuntu_user_pass=$(openssl rand -hex 12)
  57.  
  58. ####################################
  59. # Create the ConfigDrive
  60. # This is a cloud-init piece that instructs cloud-init to configure
  61. # a default user at first boot.
  62.  
  63. cdrom_vmdk_f="${box_d}/${prefix}-configdrive.vmdk"
  64.  
  65. # Create the user-data. This is totally insecure, but so is Vagrant. To
  66. # mitigate this insecurity, the vagrant instance is not accessible
  67. # except via local host.
  68. cat > ${seed_d}/user-data <<END
  69. #cloud-config
  70. system_info:
  71. default_user:
  72. name: "vagrant"
  73. password: ${ubuntu_user_pass}
  74. chpasswd: { expire: False }
  75. ssh_pwauth: True
  76. END
  77.  
  78. # Create the fake meta-data
  79. cat > ${seed_d}/meta-data <<END
  80. instance-id: iid-$(openssl rand -hex 8)
  81. local-hostname: ubuntu-${suite}
  82. END
  83.  
  84. # Pad the cdrom, otherwise the VMDK will be invalid
  85. dd if=/dev/zero of=${seed_d}/bloat_file bs=1M count=10
  86.  
  87. # Create the ISO
  88. genisoimage \
  89. -output ${seed_d}/seed.iso \
  90. -volid cidata \
  91. -joliet -rock \
  92. -input-charset utf-8 \
  93. ${seed_d}/user-data \
  94. ${seed_d}/meta-data
  95.  
  96. # Make a VMDK out of the seed file.
  97. create_vmdk ${seed_d}/seed.iso ${cdrom_vmdk_f} 10
  98.  
  99. ### END Create ConfigDrive
  100. ##########################
  101.  
  102. ##########################
  103. # VAGRANT meta-data
  104.  
  105. # Create the Vagrant file. This file is used by Vagrant to define how
  106. # Vagrant uses Virtualbox and how Vagrant interacts with the host.
  107. macaddr="02$(openssl rand -hex 5 | tr [:lower:] [:upper:])"
  108. cat > ${box_d}/Vagrantfile <<EOF
  109. # Front load the includes
  110. include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
  111. load include_vagrantfile if File.exist?(include_vagrantfile)
  112.  
  113. Vagrant.configure("2") do |config|
  114. config.vm.base_mac = "${macaddr}"
  115. config.ssh.username = "vagrant"
  116. config.ssh.password = "${ubuntu_user_pass}"
  117. config.vm.synced_folder '.', '/vagrant', disabled: true
  118.  
  119. config.vm.provider "virtualbox" do |vb|
  120. vb.name = "${prefix}"
  121. vb.customize [ "modifyvm", :id, "--uart1", "0x3F8", "4" ]
  122. vb.customize [ "modifyvm", :id, "--uartmode1", "file", File.join(Dir.pwd, "%s-console.log" % vb.name) ]
  123. end
  124.  
  125.  
  126. end
  127. EOF
  128.  
  129. # Tag it as a Virtualbox Vagrant
  130. cat > ${box_d}/metadata.json <<EOF
  131. {
  132. "provider": "virtualbox"
  133. }
  134. EOF
  135.  
  136. # END
  137. ##########################
  138.  
  139. ##########################
  140. # Create the actual box
  141.  
  142. # Get information about the disks for the OVF
  143. vmdk_size=$(du -b "${vmdk_f}" | cut -f1)
  144. vmdk_capacity=$(qemu-img info "${vmdk_f}" | awk '-F[\( ]' '$1 ~ /virtual/ && $NF ~ /bytes.*/ {print$(NF-1)}')
  145. vmdk_sha256=$(sha256sum ${vmdk_f} | cut -d' ' -f1)
  146.  
  147. cdrom_size=$(du -b "${cdrom_vmdk_f}" | cut -f1)
  148. cdrom_capacity=$(qemu-img info "${cdrom_vmdk_f}" | awk '-F[\( ]' '$1 ~ /virtual/ && $NF ~ /bytes.*/ {print$(NF-1)}')
  149. cdrom_sha256=$(sha256sum ${cdrom_vmdk_f} | cut -d' ' -f1)
  150.  
  151. # Populate the OVF template
  152. ovf="${box_d}/box.ovf"
  153. cp ${my_d}/ovf/ubuntu-ova-v1-cloudcfg-vmdk.tmpl ${ovf}
  154. serial_stamp=$(date +%Y%m%d)
  155. sed -i "${ovf}" \
  156. -e "s/@@NAME@@/${prefix}-${serial_stamp}/g" \
  157. -e "s/@@FILENAME1@@/${vmdk_f##*/}/g" \
  158. -e "s/@@VMDK_FILE_SIZE@@/${vmdk_size}/g" \
  159. -e "s/@@VMDK_CAPACITY@@/${vmdk_capacity}/g" \
  160. -e "s/@@FILENAME2@@/${cdrom_vmdk_f##*/}/g" \
  161. -e "s/@@VMDK_FILE_SIZE2@@/${cdrom_size}/g" \
  162. -e "s/@@VMDK_CAPACITY2@@/${cdrom_capacity}/g" \
  163. -e "s/@@NUM_CPUS@@/2/g" \
  164. -e "s/@@VERSION@@/${version}/g" \
  165. -e "s/@@DATE@@/${serial_stamp}/g" \
  166. -e "s/@@MEM_SIZE@@/1024/g"
  167.  
  168. ovf_sha256=$(sha256sum ${ovf} | cut -d' ' -f1)
  169.  
  170. # Generate the manifest
  171. manifest="${box_d}/${prefix}.mf"
  172. cat > "${manifest}" <<EOF
  173. SHA256(${vmdk_f##*/})= ${vmdk_sha256}
  174. SHA256(${cdrom_vmdk_f##*/})= ${cdrom_sha256}
  175. SHA256(${ovf##*/}.ovf)= ${ovf_sha256}
  176. EOF
  177.  
  178. # Now create the box
  179. echo "Creating OVA with the following attributes:"
  180. cat <<EOM
  181. OVA information:
  182. Name: ${prefix}
  183. Size: ${vmdk_size}
  184. VMDK Name: ${vmdk_f##*/}
  185. VMDK Capacity: ${vmdk_capacity}
  186. VMDK SHA256: ${vmdk_sha256}
  187. CDROM Name: ${cdrom_vmdk_f##*/}
  188. CDROM Capacity: ${cdrom_capacity}
  189. CDROM SHA256: ${cdrom_sha256}
  190. EOM
  191.  
  192. tar -C ${box_d} \
  193. -cf ${cur_d}/livecd.ubuntu-cpc.vagrant.box \
  194. box.ovf \
  195. Vagrantfile \
  196. metadata.json \
  197. ${prefix}.mf \
  198. ${vmdk_f##*/} \
  199. ${cdrom_vmdk_f##*/}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement