+ print " ", __"done", "\n";
+ }
+ }
+ }
+ }
+}
+
+# After copying the data over we must shut down and restart the
+# appliance in order to expand the content. The reason for this may
+# not be obvious, but it's because otherwise we'll have duplicate VGs
+# (the old VG(s) and the new VG(s)) which breaks LVM.
+#
+# The restart is only required if we're going to expand something.
+
+if ($to_be_expanded > 0) {
+ restart_appliance ();
+ expand_partitions ();
+}
+
+sub restart_appliance
+{
+ # Sync disk and exit.
+ $g->umount_all ();
+ $g->sync ();
+ undef $g;
+
+ $g = Sys::Guestfs->new ();
+ $g->set_trace (1) if $debug;
+ $g->add_drive ($outfile);
+ $g->launch ();
+
+ # Target partitions have changed from /dev/sdb to /dev/sda,
+ # so change them.
+ foreach my $part (@partitions)
+ {
+ my $target = $partitions{$part}->{target};
+ if ($target) {
+ if ($target =~ m{/dev/(.)db(.*)}) {
+ $partitions{$part}->{target} = "/dev/$1da$2";
+ } else {
+ die "internal error: unexpected partition target: $target";
+ }
+ }
+ }
+}
+
+sub expand_partitions
+{
+ foreach my $part (@partitions)
+ {
+ unless ($partitions{$part}->{ignore}) {
+ my $target = $partitions{$part}->{target};
+ if ($target) {
+ # Expand if requested.
+ if ($partitions{$part}->{will_expand_content}) {
+ if (!$quiet && !$debug) {
+ print __x("Expanding {p} using the '{meth}' method",
+ p => $part,
+ meth => $partitions{$part}->{expand_content_method});
+ }
+ expand_target_partition ($part)