Skip to content

Commit

Permalink
add -k/--ssh-key option
Browse files Browse the repository at this point in the history
  • Loading branch information
larsks committed Dec 9, 2014
1 parent 7e996ac commit 18f9124
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions virt-boot
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

boot_vol_bus=virtio

ARGS=$(getopt -o 67b:c:u:n --long no-snap,bus,base-image:,config:,user-data: -n virt-boot -- "$@")
ARGS=$(getopt -o s:b:c:u:ik: --long ssh-key:,size:,import,bus,base-image:,config:,user-data: -n virt-boot -- "$@")

if [ $? -ne 0 ]; then
echo "usage: $myname --base-image <baseimage> <name>"
Expand All @@ -19,10 +19,18 @@ while :; do
boot_vol_bus="$2"
shift 2
;;
-k|--ssh-key)
ssh_key="$2"
shift 2
;;
-b|--base-image)
base_image="$2"
shift 2
;;
-s|--image-size)
base_image_size="$2"
shift 2
;;
-c|--config)
config_dir="$2"
shift 2
Expand All @@ -31,7 +39,7 @@ while :; do
user_data="$2"
shift 2
;;
-n|--no-snap)
-i|--import)
no_snapshot=1
shift
;;
Expand All @@ -44,6 +52,10 @@ done
dom_name=$1
shift

if [ "$ssh_key" ] && [ -f "$ssh_key" ]; then
ssh_key_data=$(cat "$ssh_key")
fi

(
virsh destroy $dom_name
virsh undefine $dom_name
Expand All @@ -52,33 +64,48 @@ virsh undefine $dom_name
if [ "$no_snapshot" = 1 ]; then
boot_vol=$base_image
else
boot_vol=$(virt-vol $base_image $dom_name.qcow2)
boot_vol=$(virt-vol ${base_image_size:+-s $base_image_size} \
$base_image $dom_name.qcow2)

if [ $? -ne 0 ]; then
echo "ERROR: failed to create boot volume" >&2
exit 1
fi
fi

if [ "$user_data" ] && [ -f "$user_data" ]; then
if ! [ "$config_dir" ]; then
echo "generating openstack metadata layout"
config_dir=$(mktemp -t -d configXXXXXX)
clean_config_dir=1
trap 'rm -rf $config_dir' EXIT
mkdir -p $config_dir/openstack/latest
cp $user_data $config_dir/openstack/latest/user_data

if [ "$user_data" ] && [ -f "$user_data" ]; then
cp $user_data $config_dir/openstack/latest/user_data
fi
cat > $config_dir/openstack/latest/meta_data.json <<-EOF
{
"uuid": "$(uuidgen)",
"hostname": "$dom_name",
"name": "$dom_name"
}
{
EOF

if [ "$ssh_key_data" ]; then
cat >> $config_dir/openstack/latest/meta_data.json <<-EOF
"public_keys": {
"mykey": "$ssh_key_data"
},
EOF
fi

cat >> $config_dir/openstack/latest/meta_data.json <<-EOF
"uuid": "$(uuidgen)",
"hostname": "$dom_name",
"name": "$dom_name"
}
EOF
fi

if [ "$config_dir" ] && [ -d "$config_dir" ]; then
echo "generating configuration ISO image"
config_image=$(mktemp -t configXXXXXX.iso)
config_image=/tmp/$dom_name-config.iso
if ! mkisofs -o $config_image -r -J $config_dir; then
echo "ERROR: failed to create config ISO image" >&2
exit 1
Expand Down

0 comments on commit 18f9124

Please sign in to comment.