+
+#----------------------------------------------------------------------
+# De-activate all volume groups and switch to new dm-only LVM config.
+log deactivating volume groups
+
+vgchange -a n
+mv /etc/lvm/lvm.conf /etc/lvm/lvm.conf.old
+mv /etc/lvm/lvm.conf.new /etc/lvm/lvm.conf
+rm -f /etc/lvm/cache/.cache
+
+# Snapshot the block devices.
+for d in $devices_to_send; do
+ snap_name $d
+ log snapshotting block device /dev/$d to $dname ...
+
+ snapshot $d $dname
+
+ # The block devices are whole disks. Use kpartx to repartition them.
+ log running kpartx -a /dev/mapper/$dname ...
+ kpartx -a /dev/mapper/$dname
+done
+
+# Rescan for LVs.
+log running vgscan
+vgscan
+vgchange -a y
+
+# Mount the root filesystem on /mnt/root. If it's a physical
+# device then we want to mount (eg) /dev/mapper/snap_sda2.
+# If it's a LVM device then we can just mount the LVM partition.
+
+log mount $root_filesystem as /mnt/root
+
+snap_name $root_filesystem
+root_filesystem_dname="$dname"
+
+if [ -b /dev/mapper/$root_filesystem_dname ]; then
+ mount /dev/mapper/$root_filesystem_dname /mnt/root
+else
+ mount /dev/$root_filesystem /mnt/root
+fi
+
+#----------------------------------------------------------------------
+# Now see if we can get a network configuration.
+log network configuration $network
+
+case "$network" in
+ sh)
+ echo "Network configuration"
+ echo
+ echo "Please configure the network from this shell."
+ echo
+ echo "When finished, exit with ^D or exit"
+ echo
+ shell
+ ;;
+
+ ask)
+ # XXX Not implemented
+ echo "Sorry, we didn't implement this one yet."
+ shell
+ ;;
+
+ auto)
+ echo "Trying to auto-configure network from root filesystem ..."
+ echo
+ if ! auto_network; then
+ echo "Auto-configuration failed. Starting a shell."
+ echo
+ shell
+ fi
+esac
+
+#----------------------------------------------------------------------
+# Rewrite /mnt/root/etc/fstab
+
+log rewriting /etc/fstab
+
+cp /mnt/root/etc/fstab /mnt/root/etc/fstab.p2vsaved
+while read dev mountpoint fstype options freq passno; do
+ # If device is /dev/sd* then on the target fullvirt machine
+ # it will be /dev/hd*
+ if matches_regexp "^/dev/sd[[:alpha:]]+[[:digit:]]+$" "$dev"; then
+ dev=`echo $dev | sed 's|^/dev/sd|/dev/hd|'`
+ fi
+
+ # Print out again with all the fields nicely lined up.
+ printf "%-23s %-23s %-7s %-15s %d %d\n" \
+ "$dev" "$mountpoint" "$fstype" "$options" "$freq" "$passno"
+done < /mnt/root/etc/fstab.p2vsaved > /mnt/root/etc/fstab
+
+#----------------------------------------------------------------------
+# XXX Look for other files which might need to be changed here ...
+
+
+
+# We've now finished with /mnt/root (the real root filesystem),
+# so unmount it and synch everything.
+umount /mnt/root
+sync
+
+#----------------------------------------------------------------------
+# Send the device snapshots (underlying device + changes in ramdisk)
+# to the remote server.
+
+log sending disks
+
+# XXX Effectively this is using the hostname derived from network
+# configuration, but we might want to ask the user instead.
+# XXX How do we ensure that we won't overwrite target files? Currently
+# tries to use the current date as a uniquifier.
+
+# Names will be something like
+# p2v-oirase-200709011249-hda.img
+basename=p2v-`hostname -s|tr -cd '[0-9a-zA-Z]'`-`date +'%Y%m%d%H%M'`
+
+for dev in $devices_to_send; do
+ rdev=`echo $dev | sed 's|^sd|hd|'`
+ name="$basename-$rdev.img"
+ log sending $dev to $name
+
+ snap_name $dev
+
+ sectors=`blockdev --getsize /dev/mapper/$dname`
+
+ gigs=$(($sectors/2/1024/1024))
+ echo "Sending /dev/$dev (${gigs} GB) to remote machine"
+
+ dd if=/dev/mapper/$dname | gzip --best |
+ case "$remote_transport" in
+ ssh)
+ ssh -p "$remote_port" "$remote_host" \
+ "zcat > $remote_directory/$name"
+ ;;
+ tcp)
+ echo "p2v $name $sectors" > header
+ echo > newline
+ cat header - newline | nc "$remote_host" "$remote_port"
+ ;;
+ esac
+done
+
+
+#----------------------------------------------------------------------
+# Clean up.
+
+#for d in $devices_to_send; do
+# snap_name $d
+# kpartx -d /dev/mapper/$dname
+# drop_snapshot $dname
+#done
+
+# This file must end with a newline
+