Add section "Notes on disk format" to the manual.
[virt-bmap.git] / virt-bmap.in
1 #!/bin/bash -
2 # virt-bmap
3 # Copyright (C) 2014 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # 'virt-bmap' is a shell script wrapper that runs nbdkit with the
20 # right options.  Read virt-bmap(1) before trying to use this program.
21 #
22 # If you want to run this without installing then use the './run'
23 # script in the build directory, eg:
24 #
25 #   ./run virt-bmap -o bmap disk.img
26
27 program=virt-bmap
28 version="@PACKAGE_VERSION@"
29
30 output=bmap
31 format=raw
32
33 TEMP=`getopt \
34         -o f:o:V \
35         --long help,format:,output:,version \
36         -n $program -- "$@"`
37 if [ $? != 0 ]; then
38     echo "$program: problem parsing the command line arguments"
39     exit 1
40 fi
41 eval set -- "$TEMP"
42
43 usage ()
44 {
45     echo "Usage:"
46     echo "  $program [-o bmap] [--format raw|qcow2|...] disk.img"
47     echo
48     echo "Read $program(1) man page for more information."
49     exit $1
50 }
51
52 while true; do
53     case "$1" in
54         -f|--format)
55             format="$2"
56             shift 2;;
57         -o|--output)
58             output="$2"
59             shift 2;;
60         -V|--version)
61             echo "$program $version"
62             exit 0;;
63         --help)
64             usage 0;;
65         --)
66             shift
67             break;;
68         *)
69             echo "internal error ($1)"
70             exit 1;;
71     esac
72 done
73
74 # At the moment we only support a single disk image argument.
75 # Supporting multiple disk images requires adding support for export
76 # names to nbdkit.
77 if [ $# -ne 1 ]; then
78     echo "$program: Missing disk image argument.  See $program(1)."
79     exit 1
80 fi
81
82 # Turn the disk image arguments into disk=...
83 declare -a disks
84 for arg in "$@"; do
85     disks+=("disk=$arg")
86 done
87
88 # Deal with stupid autotools libdir-not-expandable crap.
89 prefix="@prefix@"
90 exec_prefix="@exec_prefix@"
91 libdir="@libdir@"
92
93 # Get the examiner plugin.
94 if [ -z "$VIRTBMAP_PLUGIN_DIR" ]; then
95     VIRTBMAP_PLUGIN_DIR="$libdir"
96 fi
97
98 # Create the socket.
99 tmpdir="$(mktemp -d)"
100 cleanup ()
101 {
102     rm -rf "$tmpdir" ||:
103 }
104 trap cleanup EXIT ERR
105 socket="$tmpdir/socket"
106
107 # Run nbdkit with plugin and arguments.
108 nbdkit -r -f -U "$socket" \
109        "$VIRTBMAP_PLUGIN_DIR/virtbmapexaminer.so" \
110        output="$output" \
111        format="$format" \
112        socket="$socket" \
113        "${disks[@]}"