X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=virt-p2v.sh;h=e8550684df4e350e1988e259dda4c0f219b40be6;hb=09eaf293724cc1ed6100f5a8a97cecf8cc007956;hp=0007e54bec6e1d39d1235e0f575bc0640eebcaec;hpb=733cfd03fb692fc76a8ea87018640cd2e9806409;p=virt-p2v.git diff --git a/virt-p2v.sh b/virt-p2v.sh index 0007e54..e855068 100755 --- a/virt-p2v.sh +++ b/virt-p2v.sh @@ -62,12 +62,27 @@ override_root_filesystem="" override_network="" #---------------------------------------------------------------------- -# Logging. +# General script setup and logging. + +exec 2>> /tmp/virt-p2v.log function log { - echo "$@" >> /tmp/virt-p2v.log + echo "$@" 1>&2 } +log +log virt-p2v starting up at `date` + +# The first and only parameter must be the tty. Connect +# stdin/stdout to this device. +if [ -n "$1" ]; then + log connecting to /dev/$1 + exec /dev/$1 +fi + +# We can safely write files into /tmp without modifying anything. +cd /tmp + #---------------------------------------------------------------------- # Helper functions. @@ -169,7 +184,17 @@ function device_of_lvs_device { # Get list of physical block devices. Sets variable $devices # to something like "sda sdb". function search_devices { - devices=$(cd /sys/block && /bin/ls -d [hs]d*) + devices1=$(cd /sys/block && /bin/ls -d [hs]d*) + log search_devices: devices1: $devices1 + # Ignore devices which fail 'blockdev --getsize' - probably + # removable devices or other strange ones. + devices="" + for d in $devices1; do + if blockdev --getsize /dev/$d > /dev/null; then + devices="$devices${devices:+ }$d" + fi + done + log search_devices: devices: $devices } # Get list of partitions from a physical block device. Sets @@ -251,6 +276,13 @@ function search_parts { # $parts is returned. } +# This generates a snapshot device name from a device name. +# eg. sda -> snap_sda +# Sets $dname. +function snap_name { + dname=`echo -n snap_"$1" | tr -cs '[:alnum:]' _` +} + #---------------------------------------------------------------------- # Network configuration functions. @@ -299,23 +331,6 @@ function auto_network { return $status } - -#---------------------------------------------------------------------- -# General script setup. - -log -log virt-p2v starting up at `date` - -# The first and only parameter must be the tty. Connect -# stdin/stdout/stderr to this device. -if [ -n "$1" ]; then - log connecting to /dev/$1 - exec /dev/$1 -fi - -# We can safely write files into /tmp without modifying anything. -cd /tmp - #---------------------------------------------------------------------- # Dialog with the user. @@ -430,9 +445,12 @@ while [ "$state" != "exit" ]; do else stat=off fi + log running blockdev --getsize /dev/$d gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024)) + log /dev/$d has size $gigs deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat" done + log deviceslist="$deviceslist" dialog \ --extra-button --extra-label "Back" --nocancel \ @@ -540,13 +558,14 @@ rm -f /etc/lvm/cache/.cache # Snapshot the block devices. for d in $devices_to_send; do - log snapshotting block device /dev/$d ... + snap_name $d + log snapshotting block device /dev/$d to $dname ... - snapshot $d snap_$d + snapshot $d $dname # The block devices are whole disks. Use kpartx to repartition them. - log running kpartx -a /dev/mapper/snap_$d ... - kpartx -a /dev/mapper/snap_$d + log running kpartx -a /dev/mapper/$dname ... + kpartx -a /dev/mapper/$dname done # Rescan for LVs. @@ -560,8 +579,11 @@ vgchange -a y log mount $root_filesystem as /mnt/root -if [ -f /dev/mapper/snap_$root_filesystem ]; then - mount /dev/mapper/snap_$root_filesystem /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 @@ -645,12 +667,14 @@ for dev in $devices_to_send; do name="$basename-$rdev.img" log sending $dev to $name - sectors=`blockdev --getsize /dev/mapper/snap_$dev` + 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/snap_$dev | gzip --best | + dd if=/dev/mapper/$dname | gzip --best | case "$remote_transport" in ssh) ssh -p "$remote_port" "$remote_host" \ @@ -669,8 +693,9 @@ done # Clean up. #for d in $devices_to_send; do -# kpartx -d /dev/mapper/snap_$d -# drop_snapshot snap_$d +# snap_name $d +# kpartx -d /dev/mapper/$dname +# drop_snapshot $dname #done # This file must end with a newline