| 1 | #!/bin/sh
|
|---|
| 2 | #
|
|---|
| 3 | # VirtualBox 4.0.6 fails to create snapshots properly if the root (i.e.
|
|---|
| 4 | # non-differencing) disk image is attached to some other VM. See
|
|---|
| 5 | #
|
|---|
| 6 | # http://www.virtualbox.org/ticket/8765
|
|---|
| 7 | #
|
|---|
| 8 | # This script heads off the problem before it can occur, by moving the hard
|
|---|
| 9 | # disk media registry entries that VirtualBox 4 keeps in per-machine .vbox
|
|---|
| 10 | # files back to the central VirtualBox.xml registry as used by earlier
|
|---|
| 11 | # versions. It can't fix a media registry that VB4 has already broken -
|
|---|
| 12 | # this is prevention, not cure. Use it after creating a VM whose hard disk
|
|---|
| 13 | # will be shared with a second VM, before creating any snapshots of the
|
|---|
| 14 | # second VM.
|
|---|
| 15 | #
|
|---|
| 16 | # VirtualBox must not be running when you use this, as it may overwrite some
|
|---|
| 17 | # or all changes and leave your VM definitions in an inconsistent state.
|
|---|
| 18 | #
|
|---|
| 19 | # The script relies on the xmlstarlet package for manipulating XML files. Check
|
|---|
| 20 | # your distro's repository or see http://xmlstar.sourceforge.net/overview.php
|
|---|
| 21 | #
|
|---|
| 22 | # Stephen Thomas <flabdablet@gmail.com> 25-Apr-2011
|
|---|
| 23 | #
|
|---|
| 24 | # This is free software. Do whatever you want with it except hold me
|
|---|
| 25 | # accountable for the grief it will undoubtedly cause you.
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | globals=~/.VirtualBox/VirtualBox.xml
|
|---|
| 30 |
|
|---|
| 31 | optback=--backup=numbered
|
|---|
| 32 | #optback=
|
|---|
| 33 |
|
|---|
| 34 | tmp=/tmp/fixvbox-$$
|
|---|
| 35 | rm -rf $tmp
|
|---|
| 36 | mkdir $tmp
|
|---|
| 37 | alias xml=xmlstarlet
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 | # Extract a list of VM definition files from the global registry's
|
|---|
| 42 | # MachineEntry tags.
|
|---|
| 43 |
|
|---|
| 44 | xml pyx "$globals" | sed -n "
|
|---|
| 45 | /^(MachineEntry$/,/^)MachineEntry$/{
|
|---|
| 46 | /^Asrc /s///p
|
|---|
| 47 | }
|
|---|
| 48 | " |
|
|---|
| 49 |
|
|---|
| 50 | # Pipe that through a loop that empties the VM definition file's HardDisks
|
|---|
| 51 | # tags after saving their contents (augmented with absolute pathnames if
|
|---|
| 52 | # necessary) to a temporary file.
|
|---|
| 53 |
|
|---|
| 54 | while read -r path
|
|---|
| 55 | do
|
|---|
| 56 | if test -e "$path"
|
|---|
| 57 | then
|
|---|
| 58 | dir="${path%/*}"
|
|---|
| 59 | file="${path#$dir/}"
|
|---|
| 60 | name="${file%.*}"
|
|---|
| 61 |
|
|---|
| 62 | xml pyx "$path" |
|
|---|
| 63 | sed "
|
|---|
| 64 | /^(HardDisks$/,/^)HardDisks$/{
|
|---|
| 65 | /^[()]HardDisks$/b
|
|---|
| 66 | /^Alocation [^/]/s! ! $dir/!
|
|---|
| 67 | w $tmp/LocalHardDisks.$name.pyx
|
|---|
| 68 | d
|
|---|
| 69 | }
|
|---|
| 70 | " |
|
|---|
| 71 | xml depyx >"$path.new"
|
|---|
| 72 | mv $optback "$path.new" "$path"
|
|---|
| 73 | fi
|
|---|
| 74 | done
|
|---|
| 75 |
|
|---|
| 76 | # Extract any existing HardDisks content from the central registry as well.
|
|---|
| 77 |
|
|---|
| 78 | xml pyx "$globals" |
|
|---|
| 79 | sed -n "
|
|---|
| 80 | /^(HardDisks$/,/^)HardDisks$/{
|
|---|
| 81 | /^[()]HardDisks$/!w $tmp/GlobalHardDisks.pyx
|
|---|
| 82 | }
|
|---|
| 83 | "
|
|---|
| 84 |
|
|---|
| 85 | # Consolidate all hard disk entries.
|
|---|
| 86 |
|
|---|
| 87 | cat $tmp/*.pyx >$tmp/AllHardDisks.pyx
|
|---|
| 88 |
|
|---|
| 89 | # Replace the central registry's HardDisks tags with the consolidated content.
|
|---|
| 90 |
|
|---|
| 91 | xml pyx "$globals" |
|
|---|
| 92 | sed "
|
|---|
| 93 | /^(HardDisks$/,/^)HardDisks$/{
|
|---|
| 94 | /^(HardDisks$/r $tmp/AllHardDisks.pyx
|
|---|
| 95 | /^[()]HardDisks$/!d
|
|---|
| 96 | }
|
|---|
| 97 | " |
|
|---|
| 98 | xml depyx >"$globals.new"
|
|---|
| 99 | mv $optback "$globals.new" "$globals"
|
|---|
| 100 |
|
|---|
| 101 | # If the safety net is turned off, clean up the temp files.
|
|---|
| 102 |
|
|---|
| 103 | test -z "$optback" && rm -r $tmp
|
|---|