forked from chef/bento
-
Notifications
You must be signed in to change notification settings - Fork 1
/
amazonlinux-2-x86_64-virtualbox-build.sh
executable file
·76 lines (63 loc) · 2.92 KB
/
amazonlinux-2-x86_64-virtualbox-build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env bash
# Getting script directory location
SCRIPT_RELATIVE_DIR=$(dirname "${BASH_SOURCE[0]}")
cd "$SCRIPT_RELATIVE_DIR" || exit
# set tmp dir for files
AMZDIR="$(pwd)/packer_templates/amz_working_files"
# Get virtualbox vdi file name with latest version number
IMG="$(wget -q https://cdn.amazonlinux.com/os-images/latest/virtualbox/ -O - | grep ".vdi" | cut -d "\"" -f 2)"
# Download vbox vdi
echo "Downloading Vbox VDI $IMG"
wget -q -O "$AMZDIR"/amazon2_x86_64.vdi -c https://cdn.amazonlinux.com/os-images/latest/virtualbox/"$IMG"
if [ ! -f "$AMZDIR"/amazon2_x86_64.vdi ]; then
echo There must be a file named amazon2_x86_64.vdi in "$AMZDIR"!
echo You can download the vdi file at https://cdn.amazonlinux.com/os-images/latest/virtualbox/
exit 1
fi
echo "Cleaning up old files"
rm -f "$AMZDIR"/*.iso "$AMZDIR"/*.ovf "$AMZDIR"/*.vmdk
echo "Creating ISO"
if [ -x "$(command -v genisoimage)" ]; then
genisoimage -output "$AMZDIR"/seed.iso -volid cidata -joliet -rock "$AMZDIR"/../amz_seed_iso/user-data "$AMZDIR"/../amz_seed_iso/meta-data
elif [ -x "$(command -v hdiutil)" ]; then
hdiutil makehybrid -o "$AMZDIR"/seed.iso -hfs -joliet -iso -default-volume-name cidata "$AMZDIR"/../amz_seed_iso
elif [ -x "$(command -v mkisofs)" ]; then
mkfsiso9660 -o "$AMZDIR"/seed.iso "$AMZDIR"/../amz_seed_iso
else
echo "No tool found to create the seed.iso"
exit 1
fi
VM="AmazonLinuxBento"
echo Powering off and deleting any existing VMs named $VM
VBoxManage controlvm $VM poweroff --type headless 2> /dev/null
VBoxManage unregistervm $VM --delete 2> /dev/null
sleep 5
echo "Creating the VM"
# from https://www.perkin.org.uk/posts/create-virtualbox-vm-from-the-command-line.html
VBoxManage createvm --name $VM --ostype "RedHat_64" --register
VBoxManage storagectl $VM --name "SATA Controller" --add sata --controller IntelAHCI
VBoxManage storageattach $VM --storagectl "SATA Controller" --port 0 --type hdd --medium "$AMZDIR"/amazon2_x86_64.vdi
VBoxManage storageattach $VM --storagectl "SATA Controller" --port 1 --type dvddrive --medium "$AMZDIR"/seed.iso
VBoxManage modifyvm $VM --memory 2048
VBoxManage modifyvm $VM --cpus 2
VBoxManage modifyvm $VM --audio-driver none
VBoxManage modifyvm $VM --ioapic on
sleep 5
echo Sleeping for 120 seconds to let the system boot and cloud-init to run
VBoxManage startvm $VM --type headless
sleep 120
VBoxManage controlvm $VM poweroff --type headless
VBoxManage storageattach $VM --storagectl "SATA Controller" --port 1 --type dvddrive --medium none
sleep 5
echo Exporting the VM to an OVF file
vboxmanage export $VM -o "$AMZDIR"/amazon2_x86_64.ovf
sleep 5
echo Deleting the VM
vboxmanage unregistervm $VM --delete
echo starting packer build of amazonlinux
if bento build --vars vbox_source_path="$AMZDIR"/amazon2_x86_64.ovf,vbox_checksum=null "$AMZDIR"/../../os_pkrvars/amazonlinux/amazonlinux-2-x86_64.pkrvars.hcl; then
echo "Cleaning up files"
rm -f "$AMZDIR"/*.ovf "$AMZDIR"/*.vmdk "$AMZDIR"/*.iso
else
exit 1
fi