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 #----------------------------------------------------------------------
68 echo "$@" >> /tmp/virt-p2v.log
71 #----------------------------------------------------------------------
74 # 'matches_regexp regexp string' returns true if string matches regexp.
75 # It uses egrep for extended regexps.
76 function matches_regexp {
78 echo "$@" | grep -Esq "$re"
81 # 'string_contains needle haystack' returns true if needle in haystack.
82 function string_contains {
83 echo "$2" | grep -Fsq "$1"
86 # 'word_in_list word ...'. '...' (list of parameters) is a list of
87 # words. Returns true if 'word' is in the list.
88 function word_in_list {
89 local word="$1"; shift
92 [ "$w" = "$word" ] && return 0
97 #----------------------------------------------------------------------
100 # Use the function read_line instead of the shell built-in read.
101 # It reads from the console.
106 # Launch a bash subshell connected to the console.
108 PS1='\u@\h:\w\$ ' bash
111 #----------------------------------------------------------------------
112 # Device mapper snapshotting.
116 # Create a device-mapper snapshot of a device with ramdisk overlay.
119 # creates a snapshot of /dev/sda1 called /dev/mapper/snap
123 # Next free ramdisk (/dev/ram$i)
124 local i=$next_ramdisk
125 next_ramdisk=$(($next_ramdisk+1))
127 # Get size of the device in sectors.
128 local sectors=`blockdev --getsize /dev/$dev`
130 dmsetup create ${name}_org \
131 --table="0 $sectors snapshot-origin /dev/$dev"
132 if [ $? -ne 0 ]; then exit 1; fi
133 dmsetup create $name \
134 --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram$i n 64"
135 if [ $? -ne 0 ]; then exit 1; fi
138 # Drop an existing snapshot created by snapshot function.
141 # drops a snapshot called /dev/mapper/snap
142 function drop_snapshot {
145 dmsetup remove /dev/mapper/$name
146 dmsetup remove /dev/mapper/${name}_org
149 #----------------------------------------------------------------------
150 # Searching for devices, partitions and LVs.
152 # Convert "/dev/foo" into "foo". Returns $device.
153 function strip_slash_dev {
154 device=$(echo "$1" | sed 's|^/dev/||')
157 # The 'lvs' utility returns devices like '/dev/sda2(0)'. I've no
158 # idea what the number in parentheses is, but remove /dev/ and the
160 function device_of_lvs_device {
162 device=$(echo "$device" | sed 's|(.*)$||')
165 # All functions in this section assume that physical block devices
166 # (things corresponding to hard disks in the system) are called
167 # hd[a-z]+ or sd[a-z]+
169 # Get list of physical block devices. Sets variable $devices
170 # to something like "sda sdb".
171 function search_devices {
172 devices=$(cd /sys/block && /bin/ls -d [hs]d*)
175 # Get list of partitions from a physical block device. Sets
176 # variable $partitions to something like "sda1 sda2 sda3".
177 # See also: search_parts
178 function get_partitions {
179 partitions=$(cd /sys/block/"$1" && /bin/ls -d "$1"*)
182 # Given a partition (eg. "sda1" or "VolGroup00/LogVol00") return
183 # the physical block device which contains it. In the case where
184 # we are given an LV, this is supposed to do the right thing and
185 # go back through the layers until it gets to the physical block
186 # device. Returns $device set to something like "sda".
187 function block_device_of_part {
188 local part="$1" vg_name lv_name pv
190 if matches_regexp '^[hs]d[a-z]+[0-9]*$' "$part"; then
191 device=$(echo "$part" | sed 's|[0-9]*$||')
195 # Not a partition name, so it's a LV name. Ask lvs to
197 lvs --noheadings -o vg_name,lv_name,devices > lvs
198 while read vg_name lv_name pv; do
199 if [ "$vg_name/$lv_name" = "$part" \
200 -o "mapper/$vg_name-$lv_name" = "$part" ]; then
201 device_of_lvs_device "$pv" ;# sets $device to (eg.) "sda1"
202 block_device_of_part "$device"
207 # Help ... block device not found.
208 log block_device_of_part: block device cannot be resolved: $part
212 # search_parts $devices examines the list of devices and looks for
213 # partitions or LVs on just those devices. Sets variable $parts to
214 # something like "sda1 VolGroup00/LogVol00".
215 function search_parts {
216 local vg_name lv_name pv pvs="" device partition partitions
220 lvs --noheadings -o vg_name,lv_name,devices > lvs
221 while read vg_name lv_name pv; do
222 # Get just the partition name (eg. "sda2").
223 device_of_lvs_device "$pv"; pv="$device"
224 # Get just the block device name (eg. "sda").
225 block_device_of_part "$pv"
227 log search_parts: pv $pv device $device lv $vg_name/$lv_name
229 # A device in our list of devices to consider?
230 if word_in_list $device "$@"; then
231 log search_parts: adding $vg_name/$lv_name
232 parts="$parts $vg_name/$lv_name"
237 log search_parts: after lvs, parts $parts pvs $pvs
239 # Look for non-LVM partitions, but ignore any which are PVs
240 # as identified above.
241 for device in "$@"; do
242 get_partitions "$device"
243 for partition in $partitions; do
244 if ! word_in_list $partition $pvs; then
245 log search_parts: adding $partition
246 parts="$parts $partition"
251 # $parts is returned.
254 #----------------------------------------------------------------------
255 # Network configuration functions.
257 # `auto_network' tries to configure the network from the
258 # root filesystem. Returns true or false.
259 function auto_network {
260 # Make sure this file exists, otherwise Fedora gives a warning.
261 touch /etc/resolv.conf
265 mv network network.saved
266 mv networking networking.saved
267 mv network-scripts network-scripts.saved
269 # Originally I symlinked these, but that causes dhclient to
270 # keep open /mnt/root (as its cwd is in network-scripts subdir).
271 # So now we will copy them recursively instead.
272 cp -r /mnt/root/etc/sysconfig/network .
273 cp -r /mnt/root/etc/sysconfig/networking .
274 cp -r /mnt/root/etc/sysconfig/network-scripts .
276 /etc/init.d/network start
279 rm -rf network networking network-scripts
280 mv network.saved network
281 mv networking.saved networking
282 mv network-scripts.saved network-scripts
286 ping -c 3 $remote_host
288 if [ "$greeting" != "no" ]; then
289 echo "Did automatic network configuration work?"
290 echo "(Hint: if not sure, there is a shell on console [ALT] [F2])"
294 if [ "$line" = "y" -o "$line" = "yes" ]; then return 0; fi
298 # In non-interactive mode, return the status of /etc/init.d/network.
303 #----------------------------------------------------------------------
304 # General script setup.
307 log virt-p2v starting up at `date`
309 # The first and only parameter must be the tty. Connect
310 # stdin/stdout/stderr to this device.
312 log connecting to /dev/$1
313 exec </dev/$1 &>/dev/$1
316 # We can safely write files into /tmp without modifying anything.
319 #----------------------------------------------------------------------
320 # Dialog with the user.
322 if [ "$greeting" != "no" ]; then
325 --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
328 # Get configuration from the user.
330 # To make the [Back] button work, we make this into a looping state
331 # machine. Each state asks a question and jumps to the next state
332 # (unless [Back] is pressed, in which case it jumps back to the previous
333 # state). Finally the 'exit' state causes us to quit the loop.
336 remote_directory=/var/lib/xen/images
339 while [ "$state" != "exit" ]; do
340 log next state = $state
343 if [ -n "$override_remote_transport" ]; then
344 remote_transport="$override_remote_transport"
347 case "$remote_transport" in
348 tcp) ssh_on=off; tcp_on=on;;
349 *) ssh_on=on; tcp_on=off;;
353 --radiolist "Connection type" 10 50 2 \
354 "ssh" "SSH (secure shell - recommended)" $ssh_on \
355 "tcp" "TCP socket" $tcp_on \
357 remote_transport=`cat line`
362 if [ -n "$override_remote_host" ]; then
363 remote_host="$override_remote_host"
367 --extra-button --extra-label "Back" --nocancel \
368 --inputbox "Remote host" 10 50 "$remote_host" \
370 if [ $? -eq 3 ]; then state=transport
372 remote_host=`cat line`
373 if [ -n "$remote_host" ]; then state=port; fi
374 # else stay in same state and demand a hostname!
379 if [ -n "$override_remote_port" ]; then
380 remote_port="$override_remote_port"
384 --extra-button --extra-label "Back" --nocancel \
385 --inputbox "Remote port" 10 50 "$remote_port" \
387 if [ $? -eq 3 ]; then state=hostname
389 remote_port=`cat line`
395 if [ "$remote_transport" = "tcp" ]; then
397 elif [ -n "$override_remote_directory" ]; then
398 remote_directory="$override_remote_directory"
402 --extra-button --extra-label "Back" --nocancel \
403 --inputbox "Remote directory containing guest images" \
405 "$remote_directory" \
407 if [ $? -eq 3 ]; then state=port
409 remote_directory=`cat line`
415 # Block devices configuration.
417 if [ -n "$override_devices_to_send" ]; then
418 devices_to_send="$override_devices_to_send"
421 # Returns the list of physical devices in $devices
424 log devices returned by search_devices: $devices
427 for d in $devices; do
428 if word_in_list $d $devices_to_send; then
433 gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024))
434 deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat"
438 --extra-button --extra-label "Back" --nocancel \
440 --checklist "Pick disks to send" 15 50 8 \
443 if [ $? -eq 3 ]; then state=directory
445 devices_to_send=`cat line`
453 if [ -n "$override_root_filesystem" ]; then
454 root_filesystem="$override_root_filesystem"
457 # Returns the list of possible partitions / LVs in $parts
458 search_parts $devices_to_send
460 log partitions returned by search_parts: $parts
464 if word_in_list $r $root_filesystem; then
469 partslist="$partslist $r /dev/$r $stat"
473 --extra-button --extra-label "Back" --nocancel \
475 --radiolist "Pick partition containing the root (/) filesystem" 10 70 5 \
478 if [ $? -eq 3 ]; then state=devices
480 root_filesystem=`cat line`
486 # Network configuration.
488 if [ -n "$override_network" ]; then
489 network="$override_network"
493 --extra-button --extra-label "Back" --nocancel \
494 --radiolist "Network configuration" 10 70 5 \
495 "auto" "Auto-configure from root filesystem" on \
496 "ask" "Manual configuration" off \
497 "sh" "Configure from the shell" off \
499 if [ $? -eq 3 ]; then state=root
507 # Verify configuration.
509 if [ "$greeting" = "no" ]; then
514 --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?" \
516 if [ $? -eq 1 ]; then
524 echo "Invalid state: $state"
532 #----------------------------------------------------------------------
533 # De-activate all volume groups and switch to new dm-only LVM config.
534 log deactivating volume groups
537 mv /etc/lvm/lvm.conf /etc/lvm/lvm.conf.old
538 mv /etc/lvm/lvm.conf.new /etc/lvm/lvm.conf
539 rm -f /etc/lvm/cache/.cache
541 # Snapshot the block devices.
542 for d in $devices_to_send; do
543 log snapshotting block device /dev/$d ...
547 # The block devices are whole disks. Use kpartx to repartition them.
548 log running kpartx -a /dev/mapper/snap_$d ...
549 kpartx -a /dev/mapper/snap_$d
557 # Mount the root filesystem on /mnt/root. If it's a physical
558 # device then we want to mount (eg) /dev/mapper/snap_sda2.
559 # If it's a LVM device then we can just mount the LVM partition.
561 log mount $root_filesystem as /mnt/root
563 if [ -f /dev/mapper/snap_$root_filesystem ]; then
564 mount /dev/mapper/snap_$root_filesystem /mnt/root
566 mount /dev/$root_filesystem /mnt/root
569 #----------------------------------------------------------------------
570 # Now see if we can get a network configuration.
571 log network configuration $network
575 echo "Network configuration"
577 echo "Please configure the network from this shell."
579 echo "When finished, exit with ^D or exit"
585 # XXX Not implemented
586 echo "Sorry, we didn't implement this one yet."
591 echo "Trying to auto-configure network from root filesystem ..."
593 if ! auto_network; then
594 echo "Auto-configuration failed. Starting a shell."
600 #----------------------------------------------------------------------
601 # Rewrite /mnt/root/etc/fstab
603 log rewriting /etc/fstab
605 cp /mnt/root/etc/fstab /mnt/root/etc/fstab.p2vsaved
606 while read dev mountpoint fstype options freq passno; do
607 # If device is /dev/sd* then on the target fullvirt machine
608 # it will be /dev/sd*
609 if matches_regexp "^/dev/sd[[:alpha:]]+[[:digit:]]+$" "$dev"; then
610 dev=`echo $dev | sed 's|^/dev/sd|/dev/hd|'`
613 # Print out again with all the fields nicely lined up.
614 printf "%-23s %-23s %-7s %-15s %d %d\n" \
615 "$dev" "$mountpoint" "$fstype" "$options" "$freq" "$passno"
616 done < /mnt/root/etc/fstab.p2vsaved > /mnt/root/etc/fstab
618 #----------------------------------------------------------------------
619 # XXX Look for other files which might need to be changed here ...
623 # We've now finished with /mnt/root (the real root filesystem),
624 # so unmount it and synch everything.
628 #----------------------------------------------------------------------
629 # Send the device snapshots (underlying device + changes in ramdisk)
630 # to the remote server.
634 # XXX Effectively this is using the hostname derived from network
635 # configuration, but we might want to ask the user instead.
636 # XXX How do we ensure that we won't overwrite target files? Currently
637 # tries to use the current date as a uniquifier.
639 # Names will be something like
640 # p2v-oirase-200709011249-hda.img
641 basename=p2v-`hostname -s|tr -cd '[0-9a-zA-Z]'`-`date +'%Y%m%d%H%M'`
643 for dev in $devices_to_send; do
644 rdev=`echo $dev | sed 's|^sd|hd|'`
645 name="$basename-$rdev.img"
646 log sending $dev to $name
648 sectors=`blockdev --getsize /dev/mapper/snap_$dev`
650 gigs=$(($sectors/2/1024/1024))
651 echo "Sending /dev/$dev (${gigs} GB) to remote machine"
653 dd if=/dev/mapper/snap_$dev | gzip --best |
654 case "$remote_transport" in
656 ssh -p "$remote_port" "$remote_host" \
657 "zcat > $remote_directory/$name"
660 echo "p2v $name $sectors" > header
662 cat header - newline | nc "$remote_host" "$remote_port"
668 #----------------------------------------------------------------------
671 #for d in $devices_to_send; do
672 # kpartx -d /dev/mapper/snap_$d
673 # drop_snapshot snap_$d
676 # This file must end with a newline