Last active
August 29, 2015 14:00
-
-
Save greghaynes/11125437 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
set -o pipefail | |
scriptname=$(basename $0) | |
base_dir=/home/greghaynes | |
if [ -z "$(which virt-install)" ]; then | |
echo "virt-install not found" | |
exit 1 | |
fi | |
function usage() { | |
exitval=${1:-1} | |
echo "usage: $scriptname vm-name disk net_bridge [passthrough]" | |
echo "" | |
echo "vm-name: A name for the vm. The build dir and hostname are named" | |
echo " this." | |
echo "disk: A raw disk for the vm containing a cloud image. Can be" | |
echo " a block device or qcow2 image." | |
echo "net_bridge: Name of bridge interface for the vm." | |
echo "passthrough: These are passed through to the virt-install command." | |
exit $exitval | |
} | |
name="${1:-}" | |
if [ -z "$name" ]; then | |
echo "vm-name required" | |
usage | |
fi | |
disk="${2:-}" | |
if [ -z "$disk" ]; then | |
echo "disk required" | |
usage | |
fi | |
net_bridge="${3:-}" | |
if [ -z "$net_bridge" ]; then | |
echo "network bridge required" | |
usage | |
fi | |
build_dir=$base_dir/$name | |
metadata_path=$build_dir/meta-data | |
userdata_path=$build_dir/user-data | |
ci_iso=$build_dir/seed.iso | |
instance_id=$(uuidgen) | |
mkdir $build_dir | |
cat > $metadata_path <<EOF | |
instance-id: $instance_id | |
local-hostname: $name | |
EOF | |
cat > $userdata_path <<EOF | |
#cloud-config | |
users: | |
- name: greghaynes | |
gecos: Gregory Haynes | |
ssh-authorized-keys: | |
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGMbMD1SAzlRPPBkGxUcv2279Yis/0GgjueFFiOdACyKytuWT8pMuPs+kZqwe10X5yNil0j7S4IBaKtRG06IsbBTtMosu5eVTuxt4uudXBsEaLIajVPtJ8ClDTbUt3+OfHgmPcICWPp/JnRS9GvvTukTj7PRYOeMFOI8nGwcBz0pSGA5jxgbRSF+rYz1UOlSNqbIUWlKqvqOOCmRtKdWyUmQnxPDUKt2jag9lRG9lSCTo9u9nuh626MILUOxWEwLDanYwh6bRAxKvkFV/xVxtG1qpxE2Q6krhseZyhXUsH6WWLqGkCqx/Pjspocnaa5ahHfGGVQBRswe0e8YrVf90V root@missingno | |
sudo: ALL=(ALL) NOPASSWD:ALL | |
lock-passwd: true | |
EOF | |
set -x | |
genisoimage -output $ci_iso -volid cidata -joliet -rock $userdata_path $metadata_path | |
virt-install --virt-type kvm --name $name -mirror --ram 1024 --disk=$disk --disk=$ci_iso --boot hd --network "bridge=$net_bridge,mac=RANDOM" ${@:4} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment