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>
11 export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin
13 # The defaults here make a generic virt-p2v.sh script, but if you want
14 # to build a partially-/fully-automatic P2V solution, then you can set
15 # these variables to something, and the script won't ask the user for
18 # Use 'greeting=no' to suppress greeting and final verification screens.
24 # can be 'ssh' or 'tcp'
25 override_remote_transport=
27 # eg. override_remote_directory=/var/lib/xen/images
28 # (only if override_remote_transport is 'ssh')
29 override_remote_directory=
31 # list of local physical devices to examine, separated by spaces,
33 # this is usually a list of whole disk devices (eg. "sda")
34 override_physical_devices=""
36 # list of filesystems to send (eg. "sda1 sda2 VolGroup00/LogVol00")
37 override_filesystems_to_send=""
39 # list of root filesystems to examine (eg. "sda3" or
40 # "VolGroup00/LogVol00")
41 override_root_filesystems=""
43 # network configuration
44 # - if empty, ask user
45 # - if contains a filesystem name (eg. "VolGroup00/LogVol00") try to
46 # auto-configure network from that
47 # (XXX needs to contain more ways to override in future)
50 #----------------------------------------------------------------------
54 echo "$@" >> /tmp/virt-p2v.log
57 #----------------------------------------------------------------------
60 # 'matches_regexp regexp string' returns true if string matches regexp.
61 # It uses egrep for extended regexps.
62 function matches_regexp {
64 echo "$@" | grep -Esq "$re"
67 # 'string_contains needle haystack' returns true if needle in haystack.
68 function string_contains {
69 echo "$2" | grep -Fsq "$1"
72 # 'word_in_list word ...'. '...' (list of parameters) is a list of
73 # words. Returns true if 'word' is in the list.
74 function word_in_list {
75 local word="$1"; shift
78 [ "$w" = "$word" ] && return 0
83 #----------------------------------------------------------------------
86 # Use the function read_line instead of the shell built-in read.
87 # It reads from the console.
89 read "$*" </dev/console
92 # Launch a bash subshell connected to the console.
94 PS1='\u@\h:\w\$ ' bash </dev/console >/dev/console 2>&1
97 #----------------------------------------------------------------------
98 # Device mapper snapshotting.
100 # Create a device-mapper snapshot of a device with ramdisk overlay.
103 # creates a snapshot of /dev/sda1 called /dev/mapper/snap
107 # Get size of the device in sectors.
108 local sectors=`blockdev --getsize /dev/$dev`
110 dmsetup create ${name}_org \
111 --table="0 $sectors snapshot-origin /dev/$dev"
112 if [ $? -ne 0 ]; then exit 1; fi
113 dmsetup create $name \
114 --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram1 n 64"
115 if [ $? -ne 0 ]; then exit 1; fi
118 # Drop an existing snapshot created by snapshot function.
121 # drops a snapshot called /dev/mapper/snap
122 function drop_snapshot {
123 dmsetup remove /dev/mapper/$name
124 dmsetup remove /dev/mapper/${name}_org
127 #----------------------------------------------------------------------
128 # Searching for devices, partitions and LVs.
130 # Convert "/dev/foo" into "foo". Returns $device.
131 function strip_slash_dev {
132 device=$(echo "$1" | sed 's|^/dev/||')
135 # The 'lvs' utility returns devices like '/dev/sda2(0)'. I've no
136 # idea what the number in parentheses is, but remove /dev/ and the
138 function device_of_lvs_device {
140 device=$(echo "$device" | sed 's|(.*)$||')
143 # All functions in this section assume that physical block devices
144 # (things corresponding to hard disks in the system) are called
145 # hd[a-z]+ or sd[a-z]+
147 # Get list of physical block devices. Sets variable $devices
148 # to something like "sda sdb".
149 function search_devices {
150 devices=$(cd /sys/block && /bin/ls -d [hs]d*)
153 # Get list of partitions from a physical block device. Sets
154 # variable $partitions to something like "sda1 sda2 sda3".
155 # See also: search_parts
156 function get_partitions {
157 partitions=$(cd /sys/block/"$1" && /bin/ls -d "$1"*)
160 # Given a partition (eg. "sda1" or "VolGroup00/LogVol00") return
161 # the physical block device which contains it. In the case where
162 # we are given an LV, this is supposed to do the right thing and
163 # go back through the layers until it gets to the physical block
164 # device. Returns $device set to something like "sda".
165 function block_device_of_part {
166 local part="$1" vg_name lv_name pv
168 if matches_regexp '^[hs]d[a-z]+[0-9]*$' "$part"; then
169 device=$(echo "$part" | sed 's|[0-9]*$||')
173 # Not a partition name, so it's a LV name. Ask lvs to
175 lvs --noheadings -o vg_name,lv_name,devices > lvs
176 while read vg_name lv_name pv; do
177 if [ "$vg_name/$lv_name" = "$part" \
178 -o "mapper/$vg_name-$lv_name" = "$part" ]; then
179 device_of_lvs_device "$pv" ;# sets $device to (eg.) "sda1"
180 block_device_of_part "$device"
185 # Help ... block device not found.
186 log block_device_of_part: block device cannot be resolved: $part
190 # search_parts $devices examines the list of devices and looks for
191 # partitions or LVs on just those devices. Sets variable $parts to
192 # something like "sda1 VolGroup00/LogVol00".
193 function search_parts {
194 local vg_name lv_name pv pvs="" device partition partitions
198 lvs --noheadings -o vg_name,lv_name,devices > lvs
199 while read vg_name lv_name pv; do
200 # Get just the partition name (eg. "sda2").
201 device_of_lvs_device "$pv"; pv="$device"
202 # Get just the block device name (eg. "sda").
203 block_device_of_part "$pv"
205 log search_parts: pv $pv device $device lv $vg_name/$lv_name
207 # A device in our list of devices to consider?
208 if word_in_list $device "$@"; then
209 log search_parts: adding $vg_name/$lv_name
210 parts="$parts $vg_name/$lv_name"
215 log search_parts: after lvs, parts $parts pvs $pvs
217 # Look for non-LVM partitions, but ignore any which are PVs
218 # as identified above.
219 for device in "$@"; do
220 get_partitions "$device"
221 for partition in $partitions; do
222 if ! word_in_list $partition $pvs; then
223 log search_parts: adding $partition
224 parts="$parts $partition"
229 # $parts is returned.
232 #----------------------------------------------------------------------
233 # Network configuration functions.
235 # `auto_network $fs' mounts /dev/$fs read-only on /mnt/root and then
236 # tries to configure the network from it. Returns true or false.
237 function auto_network {
238 if ! mount -o ro /dev/$1 /mnt; then return 1; fi
240 # Make sure this file exists, otherwise Fedora gives a warning.
241 touch /etc/resolv.conf
245 mv network network.saved
246 mv networking networking.saved
247 mv network-scripts network-scripts.saved
249 # Originally I symlinked these, but that causes dhclient to
250 # keep open /mnt (as its cwd is in network-scripts subdir).
251 # So now we will copy them recursively instead.
252 cp -r /mnt/etc/sysconfig/network .
253 cp -r /mnt/etc/sysconfig/networking .
254 cp -r /mnt/etc/sysconfig/network-scripts .
255 # This also means we can umount /mnt early.
258 /etc/init.d/network start
261 rm -rf network networking network-scripts
262 mv network.saved network
263 mv networking.saved networking
264 mv network-scripts.saved network-scripts
268 ping -c 3 $remote_host
270 if [ "$greeting" != "no" ]; then
271 echo "Did automatic network configuration work?"
272 echo "(Hint: if not sure, there is a shell on console [ALT] [F2])"
275 read_line line </dev/console
276 if [ "$line" = "y" -o "$line" = "yes" ]; then return 0; fi
280 # In non-interactive mode, return the status of /etc/init.d/network.
285 #----------------------------------------------------------------------
286 # General script setup.
288 # We can safely write files into /tmp without modifying anything.
291 #----------------------------------------------------------------------
292 # Dialog with the user.
295 log virt-p2v starting up at `date`
297 if [ "$greeting" != "no" ]; then
300 --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
303 # Get configuration from the user.
305 # To make the [Back] button work, we make this into a looping state
306 # machine. Each state asks a question and jumps to the next state
307 # (unless [Back] is pressed, in which case it jumps back to the previous
308 # state). Finally the 'exit' state causes us to quit the loop.
311 remote_directory=/var/lib/xen/images
314 while [ "$state" != "exit" ]; do
315 log next state = $state
318 if [ -n "$override_remote_transport" ]; then
319 remote_transport="$override_remote_transport"
322 case "$remote_transport" in
323 tcp) ssh_on=off; tcp_on=on;;
324 *) ssh_on=on; tcp_on=off;;
328 --radiolist "Connection type" 10 50 2 \
329 "ssh" "SSH (secure shell - recommended)" $ssh_on \
330 "tcp" "TCP socket" $tcp_on \
332 remote_transport=`cat line`
337 if [ -n "$override_remote_host" ]; then
338 remote_host="$override_remote_host"
342 --extra-button --extra-label "Back" --nocancel \
343 --inputbox "Remote host" 10 50 "$remote_host" \
345 if [ $? -eq 3 ]; then state=transport
347 remote_host=`cat line`
348 if [ -n "$remote_host" ]; then state=port; fi
349 # else stay in same state and demand a hostname!
354 if [ -n "$override_remote_port" ]; then
355 remote_port="$override_remote_port"
359 --extra-button --extra-label "Back" --nocancel \
360 --inputbox "Remote port" 10 50 "$remote_port" \
362 if [ $? -eq 3 ]; then state=hostname
364 remote_port=`cat line`
370 if [ "$remote_transport" = "tcp" ]; then
372 elif [ -n "$override_remote_directory" ]; then
373 remote_directory="$override_remote_directory"
377 --extra-button --extra-label "Back" --nocancel \
378 --inputbox "Remote directory containing guest images" \
380 "$remote_directory" \
382 if [ $? -eq 3 ]; then state=port
384 remote_directory=`cat line`
390 # Block devices configuration.
392 if [ -n "$override_physical_devices" ]; then
393 physical_devices="$override_physical_devices"
396 # Returns the list of physical devices in $devices
399 log devices returned by search_devices: $devices
402 for d in $devices; do
403 if word_in_list $d $physical_devices; then
408 gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024))
409 deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat"
413 --extra-button --extra-label "Back" --nocancel \
415 --checklist "Pick local disks to examine" 15 50 8 \
418 if [ $? -eq 3 ]; then state=directory
420 physical_devices=`cat line`
426 if [ -n "$override_filesystems_to_send" ]; then
427 filesystems_to_send="$override_filesystems_to_send"
430 # Returns the list of possible partitions / LVs in $parts
431 search_parts $physical_devices
433 log partitions returned by search_parts: $parts
437 if word_in_list $p $filesystems_to_send; then
442 gigs=$(($(blockdev --getsize /dev/$p)/2/1024/1024))
443 partslist="$partslist $p /dev/$p(${gigs}GB) $stat"
447 --extra-button --extra-label "Back" --nocancel \
449 --checklist "Pick filesystems to send" 15 70 8 \
452 if [ $? -eq 3 ]; then state=devices
454 filesystems_to_send=`cat line`
460 if [ -n "$override_root_filesystems" ]; then
461 root_filesystems="$override_root_filesystems"
465 for r in $filesystems_to_send; do
466 if word_in_list $r $root_filesystems; then
471 partslist="$partslist $r /dev/$r $stat"
475 --extra-button --extra-label "Back" --nocancel \
477 --checklist "Pick partition(s) containing a root (/) filesystem" 10 70 5 \
480 if [ $? -eq 3 ]; then state=filesystems
482 root_filesystems=`cat line`
488 # Network configuration.
490 if [ -n "$override_network" ]; then
491 network="$override_network"
495 for r in $root_filesystems; do
496 if [ "$r" = "$network" ]; then
501 partslist="$partslist $r Auto-configure $stat"
505 --extra-button --extra-label "Back" --nocancel \
506 --radiolist "Network configuration" 10 70 5 \
508 "ask" "Manual configuration" off \
509 "sh" "Configure from the shell" off \
511 if [ $? -eq 3 ]; then state=roots
519 # Verify configuration.
521 if [ "$greeting" = "no" ]; then
526 --yesno "Transport: $remote_transport\nRemote host: $remote_host\nRemote port: $remote_port\nRemote directory (ssh only): $remote_directory\nFilesystems to send: $filesystems_to_send\nRoot filesystem(s): $root_filesystems\nNetwork configuration: $network\n\nProceed with these settings?" \
528 if [ $? -eq 1 ]; then
536 echo "Invalid state: $state"
544 # Now see if we can get a network configuration.
545 log network configuration $network
549 echo "Network configuration"
551 echo "Please configure the network from this shell."
553 echo "You can modify any file under /etc (especially /etc/sysconfig)."
554 echo "No changes are made to the system disks unless you mount them"
555 echo "and explicitly modify them."
557 echo "When finished, exit with ^D or exit"
563 # XXX Not implemented
564 echo "Sorry, we didn't implement this one yet."
569 echo "Trying to auto-configure network from /dev/$network ..."
571 if ! auto_network "$network"; then
572 echo "Auto-configuration failed. Starting a shell."
578 # Mount the root filesystem(s) using device-mapper snapshots.
579 # The snapshots are called snap0, snap1, etc. with the number
580 # corresponding to its index in $root_filesystems array.
593 # This file must end with a newline