/usr/share/doc/vagrant-libvirt/examples/create_box.sh is in vagrant-libvirt 0.0.43-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | #!/usr/bin/env bash
#set -xu
error() {
local msg="${1}"
echo "==> ERROR: ${msg}"
exit 1
}
usage() {
echo "Usage: ${0} IMAGE [BOX] [Vagrantfile.add]"
echo
echo "Package a qcow2 image into a vagrant-libvirt reusable box"
}
# Print the image's backing file
backing(){
local img=${1}
qemu-img info "$img" | grep 'backing file:' | cut -d ':' -f2
}
# Rebase the image
rebase(){
local img=${1}
qemu-img rebase -p -b "" "$img"
[[ "$?" -ne 0 ]] && error "Error during rebase"
}
# Is absolute path
isabspath(){
local path=${1}
[[ "$path" =~ ^/.* ]]
}
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 1
fi
IMG=$(readlink -e "$1")
[[ "$?" -ne 0 ]] && error "'$1': No such image"
IMG_DIR=$(dirname "$IMG")
IMG_BASENAME=$(basename "$IMG")
BOX=${2:-}
# If no box name is supplied infer one from image name
if [[ -z "$BOX" ]]; then
BOX_NAME=${IMG_BASENAME%.*}
BOX=$BOX_NAME.box
else
BOX_NAME=$(basename "${BOX%.*}")
fi
[[ -f "$BOX" ]] && error "'$BOX': Already exists"
CWD=$(pwd)
TMP_DIR="$CWD/_tmp_package"
TMP_IMG="$TMP_DIR/box.img"
mkdir -p "$TMP_DIR"
[[ ! -r "$IMG" ]] && error "'$IMG': Permission denied"
if [ -n "$3" ] && [ -r "$3" ]; then
VAGRANTFILE_ADD="$(cat $3)"
fi
# We move / copy (when the image has master) the image to the tempdir
# ensure that it's moved back / removed again
if [[ -n $(backing "$IMG") ]]; then
echo "==> Image has backing image, copying image and rebasing ..."
trap "rm -rf $TMP_DIR" EXIT
cp "$IMG" "$TMP_IMG"
rebase "$TMP_IMG"
else
if fuser -s "$IMG"; then
error "Image '$IMG_BASENAME' is used by another process"
fi
# move the image to get a speed-up and use less space on disk
trap 'mv "$TMP_IMG" "$IMG"; rm -rf "$TMP_DIR"' EXIT
mv "$IMG" "$TMP_IMG"
fi
cd "$TMP_DIR"
#Using the awk int function here to truncate the virtual image size to an
#integer since the fog-libvirt library does not seem to properly handle
#floating point.
IMG_SIZE=$(qemu-img info --output=json "$TMP_IMG" | awk '/virtual-size/{s=int($2)/(1024^3); print (s == int(s)) ? s : int(s)+1 }')
echo "{$IMG_SIZE}"
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
${VAGRANTFILE_ADD:-}
end
EOF
echo "==> Creating box, tarring and gzipping"
tar cvzf "$BOX" -S --totals ./metadata.json ./Vagrantfile ./box.img
# if box is in tmpdir move it to CWD before removing tmpdir
if ! isabspath "$BOX"; then
mv "$BOX" "$CWD"
fi
echo "==> ${BOX} created"
echo "==> You can now add the box:"
echo "==> 'vagrant box add ${BOX} --name ${BOX_NAME}'"
|