X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=virt-p2v.sh;h=ab62404aff1602db4a73efeb4e2cdcad4a03d3f9;hb=73f18e249d542d8c2d406565b0690d7c47194100;hp=44184c703b5c17616b128e125e7fa86d0b8e555f;hpb=cc7a490c49e75e0c0b29098f212ed3c0f3f73612;p=virt-p2v.git diff --git a/virt-p2v.sh b/virt-p2v.sh index 44184c7..ab62404 100755 --- a/virt-p2v.sh +++ b/virt-p2v.sh @@ -3,9 +3,10 @@ # virt-p2v.sh is a shell script which performs a physical to # virtual conversion of local disks. # -# By Richard W.M. Jones -# # Copyright (C) 2007 Red Hat Inc. +# Written by Richard W.M. Jones +# +# $Id$ export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin @@ -14,7 +15,7 @@ export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin # these variables to something, and the script won't ask the user for # input. -# greeting=no +# Use 'greeting=no' to suppress greeting and final verification screens. greeting= override_remote_host= @@ -27,30 +28,93 @@ override_remote_transport= # (only if override_remote_transport is 'ssh') override_remote_directory= -# list of devices to send, separated by spaces, if empty ask user +# list of local physical devices to send, separated by spaces, +# if empty ask user. +# this is usually a list of whole disk devices (eg. "sda") override_devices_to_send="" +# the root filesystem containing /etc/fstab (eg. "sda3" or +# "VolGroup00/LogVol00") +override_root_filesystem="" + +# network configuration +# - if empty, ask user +# - "auto" means try to autoconfigure from root filesystem +# (XXX needs to contain more ways to override in future) +override_network="" + +#---------------------------------------------------------------------- +# Logging. + +function log { + echo "$@" >> /tmp/virt-p2v.log +} + +#---------------------------------------------------------------------- +# Helper functions. + +# 'matches_regexp regexp string' returns true if string matches regexp. +# It uses egrep for extended regexps. +function matches_regexp { + local re="$1"; shift + echo "$@" | grep -Esq "$re" +} + +# 'string_contains needle haystack' returns true if needle in haystack. +function string_contains { + echo "$2" | grep -Fsq "$1" +} + +# 'word_in_list word ...'. '...' (list of parameters) is a list of +# words. Returns true if 'word' is in the list. +function word_in_list { + local word="$1"; shift + local w + for w in "$@"; do + [ "$w" = "$word" ] && return 0 + done + return 1 +} + +#---------------------------------------------------------------------- +# I/O functions + +# Use the function read_line instead of the shell built-in read. +# It reads from the console. +function read_line { + read "$*" /dev/console 2>&1 +} + #---------------------------------------------------------------------- # Device mapper snapshotting. +next_ramdisk=1 + # Create a device-mapper snapshot of a device with ramdisk overlay. # Example: -# snapshot /dev/sda1 snap +# snapshot sda1 snap # creates a snapshot of /dev/sda1 called /dev/mapper/snap - - -# XXX Error checking. function snapshot { - dev=$1 - name=$2 + local dev=$1 name=$2 + + # Next free ramdisk (/dev/ram$i) + local i=$next_ramdisk + next_ramdisk=$(($next_ramdisk+1)) # Get size of the device in sectors. - sectors=`blockdev --getsize $dev` + local sectors=`blockdev --getsize /dev/$dev` dmsetup create ${name}_org \ - --table="0 $sectors snapshot-origin $dev" + --table="0 $sectors snapshot-origin /dev/$dev" + if [ $? -ne 0 ]; then exit 1; fi dmsetup create $name \ - --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram1 n 64" + --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram$i n 64" + if [ $? -ne 0 ]; then exit 1; fi } # Drop an existing snapshot created by snapshot function. @@ -58,10 +122,165 @@ function snapshot { # drop_snapshot snap # drops a snapshot called /dev/mapper/snap function drop_snapshot { + local name=$1 + dmsetup remove /dev/mapper/$name dmsetup remove /dev/mapper/${name}_org } +#---------------------------------------------------------------------- +# Searching for devices, partitions and LVs. + +# Convert "/dev/foo" into "foo". Returns $device. +function strip_slash_dev { + device=$(echo "$1" | sed 's|^/dev/||') +} + +# The 'lvs' utility returns devices like '/dev/sda2(0)'. I've no +# idea what the number in parentheses is, but remove /dev/ and the +# strange number. +function device_of_lvs_device { + strip_slash_dev "$1" + device=$(echo "$device" | sed 's|(.*)$||') +} + +# All functions in this section assume that physical block devices +# (things corresponding to hard disks in the system) are called +# hd[a-z]+ or sd[a-z]+ + +# 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*) +} + +# Get list of partitions from a physical block device. Sets +# variable $partitions to something like "sda1 sda2 sda3". +# See also: search_parts +function get_partitions { + partitions=$(cd /sys/block/"$1" && /bin/ls -d "$1"*) +} + +# Given a partition (eg. "sda1" or "VolGroup00/LogVol00") return +# the physical block device which contains it. In the case where +# we are given an LV, this is supposed to do the right thing and +# go back through the layers until it gets to the physical block +# device. Returns $device set to something like "sda". +function block_device_of_part { + local part="$1" vg_name lv_name pv + + if matches_regexp '^[hs]d[a-z]+[0-9]*$' "$part"; then + device=$(echo "$part" | sed 's|[0-9]*$||') + return 0 + fi + + # Not a partition name, so it's a LV name. Ask lvs to + # do the hard work. + lvs --noheadings -o vg_name,lv_name,devices > lvs + while read vg_name lv_name pv; do + if [ "$vg_name/$lv_name" = "$part" \ + -o "mapper/$vg_name-$lv_name" = "$part" ]; then + device_of_lvs_device "$pv" ;# sets $device to (eg.) "sda1" + block_device_of_part "$device" + return 0 + fi + done < lvs + + # Help ... block device not found. + log block_device_of_part: block device cannot be resolved: $part + device="$part" +} + +# search_parts $devices examines the list of devices and looks for +# partitions or LVs on just those devices. Sets variable $parts to +# something like "sda1 VolGroup00/LogVol00". +function search_parts { + local vg_name lv_name pv pvs="" device partition partitions + + parts="" + + lvs --noheadings -o vg_name,lv_name,devices > lvs + while read vg_name lv_name pv; do + # Get just the partition name (eg. "sda2"). + device_of_lvs_device "$pv"; pv="$device" + # Get just the block device name (eg. "sda"). + block_device_of_part "$pv" + + log search_parts: pv $pv device $device lv $vg_name/$lv_name + + # A device in our list of devices to consider? + if word_in_list $device "$@"; then + log search_parts: adding $vg_name/$lv_name + parts="$parts $vg_name/$lv_name" + pvs="$pvs $pv" + fi + done < lvs + + log search_parts: after lvs, parts $parts pvs $pvs + + # Look for non-LVM partitions, but ignore any which are PVs + # as identified above. + for device in "$@"; do + get_partitions "$device" + for partition in $partitions; do + if ! word_in_list $partition $pvs; then + log search_parts: adding $partition + parts="$parts $partition" + fi + done + done + + # $parts is returned. +} + +#---------------------------------------------------------------------- +# Network configuration functions. + +# `auto_network $fs' tries to configure the network from the +# root filesystem. Returns true or false. +function auto_network { + # Make sure this file exists, otherwise Fedora gives a warning. + touch /etc/resolv.conf + + pushd /etc/sysconfig + + mv network network.saved + mv networking networking.saved + mv network-scripts network-scripts.saved + + # Originally I symlinked these, but that causes dhclient to + # keep open /mnt/root (as its cwd is in network-scripts subdir). + # So now we will copy them recursively instead. + cp -r /mnt/root/etc/sysconfig/network . + cp -r /mnt/root/etc/sysconfig/networking . + cp -r /mnt/root/etc/sysconfig/network-scripts . + + /etc/init.d/network start + local status=$? + + rm -rf network networking network-scripts + mv network.saved network + mv networking.saved networking + mv network-scripts.saved network-scripts + + popd + + ping -c 3 $remote_host + + if [ "$greeting" != "no" ]; then + echo "Did automatic network configuration work?" + echo "(Hint: if not sure, there is a shell on console [ALT] [F2])" + echo -n " (y/n) " + local line + read_line line line + remote_transport=`cat line` + state=hostname + fi + ;; hostname) if [ -n "$override_remote_host" ]; then remote_host="$override_remote_host" + state=port else dialog \ - --nocancel \ + --extra-button --extra-label "Back" --nocancel \ --inputbox "Remote host" 10 50 "$remote_host" \ 2> line - remote_host=`cat line` - state=port + if [ $? -eq 3 ]; then state=transport + else + remote_host=`cat line` + if [ -n "$remote_host" ]; then state=port; fi + # else stay in same state and demand a hostname! + fi fi ;; port) if [ -n "$override_remote_port" ]; then remote_port="$override_remote_port" + state=directory else dialog \ --extra-button --extra-label "Back" --nocancel \ @@ -111,18 +362,225 @@ while [ "$state" != "exit" ]; do if [ $? -eq 3 ]; then state=hostname else remote_port=`cat line` - state=exit + state=directory + fi + fi + ;; + directory) + if [ "$remote_transport" = "tcp" ]; then + state=devices + elif [ -n "$override_remote_directory" ]; then + remote_directory="$override_remote_directory" + state=devices + else + dialog \ + --extra-button --extra-label "Back" --nocancel \ + --inputbox "Remote directory containing guest images" \ + 10 50 \ + "$remote_directory" \ + 2> line + if [ $? -eq 3 ]; then state=port + else + remote_directory=`cat line` + state=devices fi fi ;; + # Block devices configuration. + devices) + if [ -n "$override_devices_to_send" ]; then + devices_to_send="$override_devices_to_send" + state=root + else + # Returns the list of physical devices in $devices + search_devices + + log devices returned by search_devices: $devices + + deviceslist="" + for d in $devices; do + if word_in_list $d $devices_to_send; then + stat=on + else + stat=off + fi + gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024)) + deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat" + done + + dialog \ + --extra-button --extra-label "Back" --nocancel \ + --single-quoted \ + --checklist "Pick disks to send" 15 50 8 \ + $deviceslist \ + 2> line + if [ $? -eq 3 ]; then state=directory + else + devices_to_send=`cat line` + state=root + fi + fi + ;; + + # Root filesystem. + root) + if [ -n "$override_root_filesystem" ]; then + root_filesystem="$override_root_filesystem" + state=network + else + # Returns the list of possible partitions / LVs in $parts + search_parts $devices_to_send + + log partitions returned by search_parts: $parts + + partslist="" + for r in $parts; do + if word_in_list $r $root_filesystem; then + stat=on + else + stat=off + fi + partslist="$partslist $r /dev/$r $stat" + done + + dialog \ + --extra-button --extra-label "Back" --nocancel \ + --single-quoted \ + --radiolist "Pick partition containing the root (/) filesystem" 10 70 5 \ + $partslist \ + 2> line + if [ $? -eq 3 ]; then state=devices + else + root_filesystem=`cat line` + state=network + fi + fi + ;; + + # Network configuration. + network) + if [ -n "$override_network" ]; then + network="$override_network" + state=verify + else + dialog \ + --extra-button --extra-label "Back" --nocancel \ + --radiolist "Network configuration" 10 70 5 \ + "auto" "Auto-configure from root filesystem" on \ + "ask" "Manual configuration" off \ + "sh" "Configure from the shell" off \ + 2> line + if [ $? -eq 3 ]; then state=root + else + network=`cat line` + state=verify + fi + fi + ;; + + # Verify configuration. + verify) + if [ "$greeting" = "no" ]; then + state=exit + else + dialog \ + --title "Summary" \ + --yesno "Transport: $remote_transport\nRemote host: $remote_host\nRemote port: $remote_port\nRemote directory (ssh only): $remote_directory\nDisks to send: $devices_to_send\nRoot filesystem: $root_filesystem\nNetwork configuration: $network\n\nProceed with these settings?" \ + 18 70 + if [ $? -eq 1 ]; then + state=transport + else + state=exit + fi + fi + ;; *) echo "Invalid state: $state" - state=hostname + state=transport ;; esac done clear -echo remote_host $remote_host -echo remote_port $remote_port \ No newline at end of file + +# 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 + log snapshotting block device /dev/$d ... + + snapshot $d snap_$d + + # 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 +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 + +if [ -f /dev/mapper/snap_$root_filesystem ]; then + mount /dev/mapper/snap_$root_filesystem /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 /dev/$network ..." + echo + if ! auto_network "$network"; then + echo "Auto-configuration failed. Starting a shell." + echo + shell + fi +esac + + + + + + + +#---------------------------------------------------------------------- +# Clean up. + +umount /mnt/root +drop_snapshot snap + +# This file must end with a newline +