3 # virt-p2v.sh is a shell script which performs a physical to
4 # virtual conversion of local disks.
6 # Copyright (C) 2007 Red Hat Inc.
7 # Written by Richard W.M. Jones <rjones@redhat.com>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 # Because we're running from a start-up script, we don't have much
26 # of a login environment, so set one up.
27 export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin
31 # The defaults here make a generic virt-p2v.sh script, but if you want
32 # to build a partially-/fully-automatic P2V solution, then you can set
33 # these variables to something, and the script won't ask the user for
36 # Use 'greeting=no' to suppress greeting and final verification screens.
42 # can be 'ssh' or 'tcp'
43 override_remote_transport=
45 # eg. override_remote_directory=/var/lib/xen/images
46 # (only if override_remote_transport is 'ssh')
47 override_remote_directory=
49 # list of local physical devices to send, separated by spaces,
51 # this is usually a list of whole disk devices (eg. "sda")
52 override_devices_to_send=""
54 # the root filesystem containing /etc/fstab (eg. "sda3" or
55 # "VolGroup00/LogVol00")
56 override_root_filesystem=""
58 # network configuration
59 # - if empty, ask user
60 # - "auto" means try to autoconfigure from root filesystem
61 # (XXX needs to contain more ways to override in future)
64 #----------------------------------------------------------------------
65 # General script setup and logging.
67 exec 2>> /tmp/virt-p2v.log
74 log virt-p2v starting up at `date`
76 # The first and only parameter must be the tty. Connect
77 # stdin/stdout to this device.
79 log connecting to /dev/$1
80 exec </dev/$1 >/dev/$1
83 # We can safely write files into /tmp without modifying anything.
86 #----------------------------------------------------------------------
89 # 'matches_regexp regexp string' returns true if string matches regexp.
90 # It uses egrep for extended regexps.
91 function matches_regexp {
93 echo "$@" | grep -Esq "$re"
96 # 'string_contains needle haystack' returns true if needle in haystack.
97 function string_contains {
98 echo "$2" | grep -Fsq "$1"
101 # 'word_in_list word ...'. '...' (list of parameters) is a list of
102 # words. Returns true if 'word' is in the list.
103 function word_in_list {
104 local word="$1"; shift
107 [ "$w" = "$word" ] && return 0
112 #----------------------------------------------------------------------
115 # Use the function read_line instead of the shell built-in read.
116 # It reads from the console.
121 # Launch a bash subshell connected to the console.
123 PS1='\u@\h:\w\$ ' bash
126 #----------------------------------------------------------------------
127 # Device mapper snapshotting.
131 # Create a device-mapper snapshot of a device with ramdisk overlay.
134 # creates a snapshot of /dev/sda1 called /dev/mapper/snap
138 # Next free ramdisk (/dev/ram$i)
139 local i=$next_ramdisk
140 next_ramdisk=$(($next_ramdisk+1))
142 # Get size of the device in sectors.
143 local sectors=`blockdev --getsize /dev/$dev`
145 dmsetup create ${name}_org \
146 --table="0 $sectors snapshot-origin /dev/$dev"
147 if [ $? -ne 0 ]; then exit 1; fi
148 dmsetup create $name \
149 --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram$i n 64"
150 if [ $? -ne 0 ]; then exit 1; fi
153 # Drop an existing snapshot created by snapshot function.
156 # drops a snapshot called /dev/mapper/snap
157 function drop_snapshot {
160 dmsetup remove /dev/mapper/$name
161 dmsetup remove /dev/mapper/${name}_org
164 #----------------------------------------------------------------------
165 # Searching for devices, partitions and LVs.
167 # Convert "/dev/foo" into "foo". Returns $device.
168 function strip_slash_dev {
169 device=$(echo "$1" | sed 's|^/dev/||')
172 # The 'lvs' utility returns devices like '/dev/sda2(0)'. I've no
173 # idea what the number in parentheses is, but remove /dev/ and the
175 function device_of_lvs_device {
177 device=$(echo "$device" | sed 's|(.*)$||')
180 # All functions in this section assume that physical block devices
181 # (things corresponding to hard disks in the system) are called
182 # hd[a-z]+ or sd[a-z]+
184 # Get list of physical block devices. Sets variable $devices
185 # to something like "sda sdb".
186 function search_devices {
187 devices1=$(cd /sys/block && /bin/ls -d [hs]d*)
188 log search_devices: devices1: $devices1
189 # Ignore devices which fail 'blockdev --getsize' - probably
190 # removable devices or other strange ones.
192 for d in $devices1; do
193 if blockdev --getsize /dev/$d > /dev/null; then
194 devices="$devices${devices:+ }$d"
197 log search_devices: devices: $devices
200 # Get list of partitions from a physical block device. Sets
201 # variable $partitions to something like "sda1 sda2 sda3".
202 # See also: search_parts
203 function get_partitions {
204 partitions=$(cd /sys/block/"$1" && /bin/ls -d "$1"*)
207 # Given a partition (eg. "sda1" or "VolGroup00/LogVol00") return
208 # the physical block device which contains it. In the case where
209 # we are given an LV, this is supposed to do the right thing and
210 # go back through the layers until it gets to the physical block
211 # device. Returns $device set to something like "sda".
212 function block_device_of_part {
213 local part="$1" vg_name lv_name pv
215 if matches_regexp '^[hs]d[a-z]+[0-9]*$' "$part"; then
216 device=$(echo "$part" | sed 's|[0-9]*$||')
220 # Not a partition name, so it's a LV name. Ask lvs to
222 lvs --noheadings -o vg_name,lv_name,devices > lvs
223 while read vg_name lv_name pv; do
224 if [ "$vg_name/$lv_name" = "$part" \
225 -o "mapper/$vg_name-$lv_name" = "$part" ]; then
226 device_of_lvs_device "$pv" ;# sets $device to (eg.) "sda1"
227 block_device_of_part "$device"
232 # Help ... block device not found.
233 log block_device_of_part: block device cannot be resolved: $part
237 # search_parts $devices examines the list of devices and looks for
238 # partitions or LVs on just those devices. Sets variable $parts to
239 # something like "sda1 VolGroup00/LogVol00".
240 function search_parts {
241 local vg_name lv_name pv pvs="" device partition partitions
245 lvs --noheadings -o vg_name,lv_name,devices > lvs
246 while read vg_name lv_name pv; do
247 # Get just the partition name (eg. "sda2").
248 device_of_lvs_device "$pv"; pv="$device"
249 # Get just the block device name (eg. "sda").
250 block_device_of_part "$pv"
252 log search_parts: pv $pv device $device lv $vg_name/$lv_name
254 # A device in our list of devices to consider?
255 if word_in_list $device "$@"; then
256 log search_parts: adding $vg_name/$lv_name
257 parts="$parts $vg_name/$lv_name"
262 log search_parts: after lvs, parts $parts pvs $pvs
264 # Look for non-LVM partitions, but ignore any which are PVs
265 # as identified above.
266 for device in "$@"; do
267 get_partitions "$device"
268 for partition in $partitions; do
269 if ! word_in_list $partition $pvs; then
270 log search_parts: adding $partition
271 parts="$parts $partition"
276 # $parts is returned.
279 # This generates a snapshot device name from a device name.
280 # eg. sda -> snap_sda
283 dname=`echo -n snap_"$1" | tr -cs '[:alnum:]' _`
286 #----------------------------------------------------------------------
287 # Network configuration functions.
289 # `auto_network' tries to configure the network from the
290 # root filesystem. Returns true or false.
291 function auto_network {
292 # Make sure this file exists, otherwise Fedora gives a warning.
293 touch /etc/resolv.conf
297 mv network network.saved
298 mv networking networking.saved
299 mv network-scripts network-scripts.saved
301 # Originally I symlinked these, but that causes dhclient to
302 # keep open /mnt/root (as its cwd is in network-scripts subdir).
303 # So now we will copy them recursively instead.
304 cp -r /mnt/root/etc/sysconfig/network .
305 cp -r /mnt/root/etc/sysconfig/networking .
306 cp -r /mnt/root/etc/sysconfig/network-scripts .
308 /etc/init.d/network start
311 rm -rf network networking network-scripts
312 mv network.saved network
313 mv networking.saved networking
314 mv network-scripts.saved network-scripts
318 ping -c 3 $remote_host
320 if [ "$greeting" != "no" ]; then
321 echo "Did automatic network configuration work?"
322 echo "(Hint: if not sure, there is a shell on console [ALT] [F2])"
326 if [ "$line" = "y" -o "$line" = "yes" ]; then return 0; fi
330 # In non-interactive mode, return the status of /etc/init.d/network.
334 #----------------------------------------------------------------------
335 # Dialog with the user.
337 if [ "$greeting" != "no" ]; then
340 --msgbox "\nWelcome to virt-p2v, a live CD for migrating a physical machine to a virtualized host.\n\nTo continue press the Return key.\n\nTo get a shell you can use [ALT] [F2] and log in as root with no password." 17 50
343 # Get configuration from the user.
345 # To make the [Back] button work, we make this into a looping state
346 # machine. Each state asks a question and jumps to the next state
347 # (unless [Back] is pressed, in which case it jumps back to the previous
348 # state). Finally the 'exit' state causes us to quit the loop.
351 remote_directory=/var/lib/xen/images
354 while [ "$state" != "exit" ]; do
355 log next state = $state
358 if [ -n "$override_remote_transport" ]; then
359 remote_transport="$override_remote_transport"
362 case "$remote_transport" in
363 tcp) ssh_on=off; tcp_on=on;;
364 *) ssh_on=on; tcp_on=off;;
368 --radiolist "Connection type" 10 50 2 \
369 "ssh" "SSH (secure shell - recommended)" $ssh_on \
370 "tcp" "TCP socket" $tcp_on \
372 remote_transport=`cat line`
377 if [ -n "$override_remote_host" ]; then
378 remote_host="$override_remote_host"
382 --extra-button --extra-label "Back" --nocancel \
383 --inputbox "Remote host" 10 50 "$remote_host" \
385 if [ $? -eq 3 ]; then state=transport
387 remote_host=`cat line`
388 if [ -n "$remote_host" ]; then state=port; fi
389 # else stay in same state and demand a hostname!
394 if [ -n "$override_remote_port" ]; then
395 remote_port="$override_remote_port"
399 --extra-button --extra-label "Back" --nocancel \
400 --inputbox "Remote port" 10 50 "$remote_port" \
402 if [ $? -eq 3 ]; then state=hostname
404 remote_port=`cat line`
410 if [ "$remote_transport" = "tcp" ]; then
412 elif [ -n "$override_remote_directory" ]; then
413 remote_directory="$override_remote_directory"
417 --extra-button --extra-label "Back" --nocancel \
418 --inputbox "Remote directory containing guest images" \
420 "$remote_directory" \
422 if [ $? -eq 3 ]; then state=port
424 remote_directory=`cat line`
430 # Block devices configuration.
432 if [ -n "$override_devices_to_send" ]; then
433 devices_to_send="$override_devices_to_send"
436 # Returns the list of physical devices in $devices
439 log devices returned by search_devices: $devices
442 for d in $devices; do
443 if word_in_list $d $devices_to_send; then
448 log running blockdev --getsize /dev/$d
449 gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024))
450 log /dev/$d has size $gigs
451 deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat"
453 log deviceslist="$deviceslist"
456 --extra-button --extra-label "Back" --nocancel \
458 --checklist "Pick disks to send" 15 50 8 \
461 if [ $? -eq 3 ]; then state=directory
463 devices_to_send=`cat line`
471 if [ -n "$override_root_filesystem" ]; then
472 root_filesystem="$override_root_filesystem"
475 # Returns the list of possible partitions / LVs in $parts
476 search_parts $devices_to_send
478 log partitions returned by search_parts: $parts
482 if word_in_list $r $root_filesystem; then
487 partslist="$partslist $r /dev/$r $stat"
491 --extra-button --extra-label "Back" --nocancel \
493 --radiolist "Pick partition containing the root (/) filesystem" 10 70 5 \
496 if [ $? -eq 3 ]; then state=devices
498 root_filesystem=`cat line`
504 # Network configuration.
506 if [ -n "$override_network" ]; then
507 network="$override_network"
511 --extra-button --extra-label "Back" --nocancel \
512 --radiolist "Network configuration" 10 70 5 \
513 "auto" "Auto-configure from root filesystem" on \
514 "ask" "Manual configuration" off \
515 "sh" "Configure from the shell" off \
517 if [ $? -eq 3 ]; then state=root
525 # Verify configuration.
527 if [ "$greeting" = "no" ]; then
532 --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?" \
534 if [ $? -eq 1 ]; then
542 echo "Invalid state: $state"
550 #----------------------------------------------------------------------
551 # De-activate all volume groups and switch to new dm-only LVM config.
552 log deactivating volume groups
555 mv /etc/lvm/lvm.conf /etc/lvm/lvm.conf.old
556 mv /etc/lvm/lvm.conf.new /etc/lvm/lvm.conf
557 rm -f /etc/lvm/cache/.cache
559 # Snapshot the block devices.
560 for d in $devices_to_send; do
562 log snapshotting block device /dev/$d to $dname ...
566 # The block devices are whole disks. Use kpartx to repartition them.
567 log running kpartx -a /dev/mapper/$dname ...
568 kpartx -a /dev/mapper/$dname
576 # Mount the root filesystem on /mnt/root. If it's a physical
577 # device then we want to mount (eg) /dev/mapper/snap_sda2.
578 # If it's a LVM device then we can just mount the LVM partition.
580 log mount $root_filesystem as /mnt/root
582 snap_name $root_filesystem
583 root_filesystem_dname="$dname"
585 if [ -b /dev/mapper/$root_filesystem_dname ]; then
586 mount /dev/mapper/$root_filesystem_dname /mnt/root
588 mount /dev/$root_filesystem /mnt/root
591 #----------------------------------------------------------------------
592 # Now see if we can get a network configuration.
593 log network configuration $network
597 echo "Network configuration"
599 echo "Please configure the network from this shell."
601 echo "When finished, exit with ^D or exit"
607 # XXX Not implemented
608 echo "Sorry, we didn't implement this one yet."
613 echo "Trying to auto-configure network from root filesystem ..."
615 if ! auto_network; then
616 echo "Auto-configuration failed. Starting a shell."
622 #----------------------------------------------------------------------
623 # Rewrite /mnt/root/etc/fstab
625 log rewriting /etc/fstab
627 cp /mnt/root/etc/fstab /mnt/root/etc/fstab.p2vsaved
628 while read dev mountpoint fstype options freq passno; do
629 # If device is /dev/sd* then on the target fullvirt machine
630 # it will be /dev/sd*
631 if matches_regexp "^/dev/sd[[:alpha:]]+[[:digit:]]+$" "$dev"; then
632 dev=`echo $dev | sed 's|^/dev/sd|/dev/hd|'`
635 # Print out again with all the fields nicely lined up.
636 printf "%-23s %-23s %-7s %-15s %d %d\n" \
637 "$dev" "$mountpoint" "$fstype" "$options" "$freq" "$passno"
638 done < /mnt/root/etc/fstab.p2vsaved > /mnt/root/etc/fstab
640 #----------------------------------------------------------------------
641 # XXX Look for other files which might need to be changed here ...
645 # We've now finished with /mnt/root (the real root filesystem),
646 # so unmount it and synch everything.
650 #----------------------------------------------------------------------
651 # Send the device snapshots (underlying device + changes in ramdisk)
652 # to the remote server.
656 # XXX Effectively this is using the hostname derived from network
657 # configuration, but we might want to ask the user instead.
658 # XXX How do we ensure that we won't overwrite target files? Currently
659 # tries to use the current date as a uniquifier.
661 # Names will be something like
662 # p2v-oirase-200709011249-hda.img
663 basename=p2v-`hostname -s|tr -cd '[0-9a-zA-Z]'`-`date +'%Y%m%d%H%M'`
665 for dev in $devices_to_send; do
666 rdev=`echo $dev | sed 's|^sd|hd|'`
667 name="$basename-$rdev.img"
668 log sending $dev to $name
672 sectors=`blockdev --getsize /dev/mapper/$dname`
674 gigs=$(($sectors/2/1024/1024))
675 echo "Sending /dev/$dev (${gigs} GB) to remote machine"
677 dd if=/dev/mapper/$dname | gzip --best |
678 case "$remote_transport" in
680 ssh -p "$remote_port" "$remote_host" \
681 "zcat > $remote_directory/$name"
684 echo "p2v $name $sectors" > header
686 cat header - newline | nc "$remote_host" "$remote_port"
692 #----------------------------------------------------------------------
695 #for d in $devices_to_send; do
697 # kpartx -d /dev/mapper/$dname
698 # drop_snapshot $dname
701 # This file must end with a newline