2015-02-18 11:22:43 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
|
|
error() {
|
|
|
|
|
local msg="${1}"
|
|
|
|
|
echo "==> ${msg}"
|
|
|
|
|
exit 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
echo "Usage: ${0} IMAGE [BOX]"
|
|
|
|
|
echo
|
|
|
|
|
echo "Package a qcow2 image into a vagrant-libvirt reusable box"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Print the image's backing file
|
|
|
|
|
backing(){
|
|
|
|
|
local img=${1}
|
2015-02-26 09:46:04 +01:00
|
|
|
qemu-img info "$img" | grep 'backing file:' | cut -d ':' -f2
|
2015-02-18 11:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Rebase the image
|
|
|
|
|
rebase(){
|
|
|
|
|
local img=${1}
|
2015-02-26 09:46:04 +01:00
|
|
|
qemu-img rebase -p -b "" "$img"
|
2015-02-18 11:22:43 +01:00
|
|
|
[[ "$?" -ne 0 ]] && error "Error during rebase"
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-19 08:58:49 +01:00
|
|
|
# Is absolute path
|
|
|
|
|
isabspath(){
|
|
|
|
|
local path=${1}
|
2015-02-26 09:46:04 +01:00
|
|
|
[[ "$path" =~ ^/.* ]]
|
2015-02-19 08:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
2015-02-18 11:22:43 +01:00
|
|
|
if [ -z "$1" ]; then
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
IMG=$(readlink -e "$1")
|
|
|
|
|
[[ "$?" -ne 0 ]] && error "'$1': No such image"
|
2015-02-18 11:22:43 +01:00
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
IMG_DIR=$(dirname "$IMG")
|
|
|
|
|
IMG_BASENAME=$(basename "$IMG")
|
2015-02-18 11:22:43 +01:00
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
BOX=${2:-}
|
2015-02-18 11:22:43 +01:00
|
|
|
# If no box name is supplied infer one from image name
|
2015-02-26 09:46:04 +01:00
|
|
|
if [[ -z "$BOX" ]]; then
|
2015-02-18 11:22:43 +01:00
|
|
|
BOX_NAME=${IMG_BASENAME%.*}
|
|
|
|
|
BOX=$BOX_NAME.box
|
|
|
|
|
else
|
2015-02-26 09:46:04 +01:00
|
|
|
BOX_NAME=$(basename "${BOX%.*}")
|
2015-02-18 11:22:43 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
[[ -f "$BOX" ]] && error "'$BOX': Already exists"
|
|
|
|
|
|
|
|
|
|
CWD=$(pwd)
|
2015-02-26 09:46:04 +01:00
|
|
|
TMP_DIR="$CWD/_tmp_package"
|
|
|
|
|
TMP_IMG="$TMP_DIR/box.img"
|
2015-02-18 11:22:43 +01:00
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
mkdir -p "$TMP_DIR"
|
2015-02-18 11:22:43 +01:00
|
|
|
|
|
|
|
|
# We move / copy (when the image has master) the image to the tempdir
|
|
|
|
|
# ensure that it's moved back / removed again
|
2015-02-26 09:46:04 +01:00
|
|
|
if [[ -n $(backing "$IMG") ]]; then
|
2015-02-18 11:22:43 +01:00
|
|
|
echo "==> Image has backing image, copying image and rebasing ..."
|
|
|
|
|
trap "rm -rf $TMP_DIR" EXIT
|
2015-02-26 09:46:04 +01:00
|
|
|
cp "$IMG" "$TMP_IMG"
|
|
|
|
|
rebase "$TMP_IMG"
|
2015-02-18 11:22:43 +01:00
|
|
|
else
|
2015-02-26 09:46:04 +01:00
|
|
|
trap 'mv "$TMP_IMG" "$IMG"; rm -rf "$TMP_DIR"' EXIT
|
|
|
|
|
mv "$IMG" "$TMP_IMG"
|
2015-02-18 11:22:43 +01:00
|
|
|
fi
|
|
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
cd "$TMP_DIR"
|
2015-02-18 11:22:43 +01:00
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
IMG_SIZE=$(qemu-img info "$TMP_IMG" | grep 'virtual size' | awk '{print $3;}' | tr -d 'G')
|
2015-02-18 11:22:43 +01:00
|
|
|
|
|
|
|
|
cat > metadata.json <<EOF
|
|
|
|
|
{
|
|
|
|
|
"provider": "libvirt",
|
|
|
|
|
"format": "qcow2",
|
|
|
|
|
"virtual_size": ${IMG_SIZE}
|
|
|
|
|
}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
cat > Vagrantfile <<EOF
|
|
|
|
|
Vagrant.configure("2") do |config|
|
|
|
|
|
|
|
|
|
|
config.vm.provider :libvirt do |libvirt|
|
|
|
|
|
|
|
|
|
|
libvirt.driver = "kvm"
|
|
|
|
|
libvirt.host = ""
|
|
|
|
|
libvirt.connect_via_ssh = false
|
|
|
|
|
libvirt.storage_pool_name = "default"
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
echo "==> Creating box, tarring and gzipping"
|
|
|
|
|
|
2015-02-26 09:46:04 +01:00
|
|
|
tar cvzf "$BOX" --totals ./metadata.json ./Vagrantfile ./box.img
|
2015-02-18 11:22:43 +01:00
|
|
|
|
2015-02-19 08:58:49 +01:00
|
|
|
# if box is in tmpdir move it to CWD before removing tmpdir
|
2015-02-26 09:46:04 +01:00
|
|
|
if ! isabspath "$BOX"; then
|
|
|
|
|
mv "$BOX" "$CWD"
|
2015-02-19 08:58:49 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo "==> ${BOX} created"
|
2015-02-18 11:22:43 +01:00
|
|
|
echo "==> You can now add the box:"
|
|
|
|
|
echo "==> 'vagrant box add ${BOX} --name ${BOX_NAME}'"
|