More
[virt-p2v.git] / virt-p2v.sh
1 #!/bin/bash
2 #
3 # virt-p2v.sh is a shell script which performs a physical to
4 # virtual conversion of local disks.
5 #
6 # Copyright (C) 2007 Red Hat Inc.
7 # Written by Richard W.M. Jones <rjones@redhat.com>
8 #
9 # $Id$
10
11 export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin
12
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
16 # input.
17
18 # Use 'greeting=no' to suppress greeting and final verification screens.
19 greeting=
20
21 override_remote_host=
22 override_remote_port=
23
24 # can be 'ssh' or 'tcp'
25 override_remote_transport=
26
27 # eg. override_remote_directory=/var/lib/xen/images
28 # (only if override_remote_transport is 'ssh')
29 override_remote_directory=
30
31 # list of local physical devices to examine, separated by spaces,
32 # if empty ask user.
33 # this is usually a list of whole disk devices (eg. "sda")
34 override_physical_devices=""
35
36 # list of filesystems to send (eg. "sda1 sda2 VolGroup00/LogVol00")
37 override_filesystems_to_send=""
38
39 # list of root filesystems to examine (eg. "sda3" or
40 # "VolGroup00/LogVol00")
41 override_root_filesystems=""
42
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)
48 override_network=""
49
50 #----------------------------------------------------------------------
51 # Logging.
52
53 function log {
54     echo "$@" >> /tmp/virt-p2v.log
55 }
56
57 #----------------------------------------------------------------------
58 # Helper functions.
59
60 # 'matches_regexp regexp string' returns true if string matches regexp.
61 # It uses egrep for extended regexps.
62 function matches_regexp {
63     local re="$1"; shift
64     echo "$@" | grep -Esq "$re"
65 }
66
67 # 'string_contains needle haystack' returns true if needle in haystack.
68 function string_contains {
69     echo "$2" | grep -Fsq "$1"
70 }
71
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
76     local w
77     for w in "$@"; do
78         [ "$w" = "$word" ] && return 0
79     done
80     return 1
81 }
82
83 #----------------------------------------------------------------------
84 # I/O functions
85
86 # Use the function read_line instead of the shell built-in read.
87 # It reads from the console.
88 function read_line {
89     read "$*" </dev/console
90 }
91
92 # Launch a bash subshell connected to the console.
93 function shell {
94     PS1='\u@\h:\w\$ ' bash </dev/console >/dev/console 2>&1
95 }
96
97 #----------------------------------------------------------------------
98 # Device mapper snapshotting.
99
100 # Create a device-mapper snapshot of a device with ramdisk overlay.
101 # Example:
102 #   snapshot sda1 snap
103 # creates a snapshot of /dev/sda1 called /dev/mapper/snap
104 function snapshot {
105     local dev=$1 name=$2
106
107     # Get size of the device in sectors.
108     local sectors=`blockdev --getsize /dev/$dev`
109
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
116 }
117
118 # Drop an existing snapshot created by snapshot function.
119 # Example:
120 #   drop_snapshot snap
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
125 }
126
127 #----------------------------------------------------------------------
128 # Searching for devices, partitions and LVs.
129
130 # Convert "/dev/foo" into "foo".  Returns $device.
131 function strip_slash_dev {
132     device=$(echo "$1" | sed 's|^/dev/||')
133 }
134
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
137 # strange number.
138 function device_of_lvs_device {
139     strip_slash_dev "$1"
140     device=$(echo "$device" | sed 's|(.*)$||')
141 }
142
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]+
146
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*)
151 }
152
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"*)
158 }
159
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
167
168     if matches_regexp '^[hs]d[a-z]+[0-9]*$' "$part"; then
169         device=$(echo "$part" | sed 's|[0-9]*$||')
170         return 0
171     fi
172
173     # Not a partition name, so it's a LV name.  Ask lvs to
174     # do the hard work.
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"
181             return 0
182         fi
183     done < lvs
184
185     # Help ... block device not found.
186     log block_device_of_part: block device cannot be resolved: $part
187     device="$part"
188 }
189
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
195
196     parts=""
197
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"
204
205         log search_parts: pv $pv device $device lv $vg_name/$lv_name
206
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"
211             pvs="$pvs $pv"
212         fi
213     done < lvs
214
215     log search_parts: after lvs, parts $parts pvs $pvs
216
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"
225             fi
226         done
227     done
228
229     # $parts is returned.
230 }
231
232 #----------------------------------------------------------------------
233 # Network configuration functions.
234
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
239
240     # Make sure this file exists, otherwise Fedora gives a warning.
241     touch /etc/resolv.conf
242
243     pushd /etc/sysconfig
244
245     mv network network.saved
246     mv networking networking.saved
247     mv network-scripts network-scripts.saved
248
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.
256     umount /mnt
257
258     /etc/init.d/network start
259     local status=$?
260
261     rm -rf network networking network-scripts
262     mv network.saved network
263     mv networking.saved networking
264     mv network-scripts.saved network-scripts
265
266     popd
267
268     ping -c 3 $remote_host
269
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])"
273         echo -n "    (y/n) "
274         local line
275         read_line line </dev/console
276         if [ "$line" = "y" -o "$line" = "yes" ]; then return 0; fi
277         return 1
278     fi
279
280     # In non-interactive mode, return the status of /etc/init.d/network.
281     return $status
282 }
283
284
285 #----------------------------------------------------------------------
286 # General script setup.
287
288 # We can safely write files into /tmp without modifying anything.
289 cd /tmp
290
291 #----------------------------------------------------------------------
292 # Dialog with the user.
293
294 log
295 log virt-p2v starting up at `date`
296
297 if [ "$greeting" != "no" ]; then
298     dialog \
299         --title "virt-p2v" \
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
301 fi
302
303 # Get configuration from the user.
304
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.
309
310 remote_port=22
311 remote_directory=/var/lib/xen/images
312 state=transport
313
314 while [ "$state" != "exit" ]; do
315     log next state = $state
316     case "$state" in
317         transport)
318             if [ -n "$override_remote_transport" ]; then
319                 remote_transport="$override_remote_transport"
320                 state=hostname
321             else
322                 case "$remote_transport" in
323                     tcp) ssh_on=off; tcp_on=on;;
324                     *) ssh_on=on; tcp_on=off;;
325                 esac
326                 dialog \
327                     --nocancel \
328                     --radiolist "Connection type" 10 50 2 \
329                     "ssh" "SSH (secure shell - recommended)" $ssh_on \
330                     "tcp" "TCP socket" $tcp_on \
331                     2> line
332                 remote_transport=`cat line`
333                 state=hostname
334             fi
335             ;;
336         hostname)
337             if [ -n "$override_remote_host" ]; then
338                 remote_host="$override_remote_host"
339                 state=port
340             else
341                 dialog \
342                     --extra-button --extra-label "Back" --nocancel \
343                     --inputbox "Remote host" 10 50 "$remote_host" \
344                     2> line
345                 if [ $? -eq 3 ]; then state=transport
346                 else
347                     remote_host=`cat line`
348                     if [ -n "$remote_host" ]; then state=port; fi
349                     # else stay in same state and demand a hostname!
350                 fi
351             fi
352             ;;
353         port)
354             if [ -n "$override_remote_port" ]; then
355                 remote_port="$override_remote_port"
356                 state=directory
357             else
358                 dialog \
359                     --extra-button --extra-label "Back" --nocancel \
360                     --inputbox "Remote port" 10 50 "$remote_port" \
361                     2> line
362                 if [ $? -eq 3 ]; then state=hostname
363                 else
364                     remote_port=`cat line`
365                     state=directory
366                 fi
367             fi
368             ;;
369         directory)
370             if [ "$remote_transport" = "tcp" ]; then
371                 state=devices
372             elif [ -n "$override_remote_directory" ]; then
373                 remote_directory="$override_remote_directory"
374                 state=devices
375             else
376                 dialog \
377                     --extra-button --extra-label "Back" --nocancel \
378                     --inputbox "Remote directory containing guest images" \
379                     10 50 \
380                     "$remote_directory" \
381                     2> line
382                 if [ $? -eq 3 ]; then state=port
383                 else
384                     remote_directory=`cat line`
385                     state=devices
386                 fi
387             fi
388             ;;
389
390         # Block devices configuration.
391         devices)
392             if [ -n "$override_physical_devices" ]; then
393                 physical_devices="$override_physical_devices"
394                 state=filesystems
395             else
396                 # Returns the list of physical devices in $devices
397                 search_devices
398
399                 log devices returned by search_devices: $devices
400
401                 deviceslist=""
402                 for d in $devices; do
403                     if word_in_list $d $physical_devices; then
404                         stat=on
405                     else
406                         stat=off
407                     fi
408                     gigs=$(($(blockdev --getsize /dev/$d)/2/1024/1024))
409                     deviceslist="$deviceslist $d /dev/$d(${gigs}GB) $stat"
410                 done
411
412                 dialog \
413                     --extra-button --extra-label "Back" --nocancel \
414                     --single-quoted \
415                     --checklist "Pick local disks to examine" 15 50 8 \
416                     $deviceslist \
417                     2> line
418                 if [ $? -eq 3 ]; then state=directory
419                 else
420                     physical_devices=`cat line`
421                     state=filesystems
422                 fi
423             fi
424             ;;
425         filesystems)
426             if [ -n "$override_filesystems_to_send" ]; then
427                 filesystems_to_send="$override_filesystems_to_send"
428                 state=roots
429             else
430                 # Returns the list of possible partitions / LVs in $parts
431                 search_parts $physical_devices
432
433                 log partitions returned by search_parts: $parts
434
435                 partslist=""
436                 for p in $parts; do
437                     if word_in_list $p $filesystems_to_send; then
438                         stat=on
439                     else
440                         stat=off
441                     fi
442                     gigs=$(($(blockdev --getsize /dev/$p)/2/1024/1024))
443                     partslist="$partslist $p /dev/$p(${gigs}GB) $stat"
444                 done
445
446                 dialog \
447                     --extra-button --extra-label "Back" --nocancel \
448                     --single-quoted \
449                     --checklist "Pick filesystems to send" 15 70 8 \
450                     $partslist \
451                     2> line
452                 if [ $? -eq 3 ]; then state=devices
453                 else
454                     filesystems_to_send=`cat line`
455                     state=roots
456                 fi
457             fi
458             ;;
459         roots)
460             if [ -n "$override_root_filesystems" ]; then
461                 root_filesystems="$override_root_filesystems"
462                 state=network
463             else
464                 partslist=""
465                 for r in $filesystems_to_send; do
466                     if word_in_list $r $root_filesystems; then
467                         stat=on
468                     else
469                         stat=off
470                     fi
471                     partslist="$partslist $r /dev/$r $stat"
472                 done
473
474                 dialog \
475                     --extra-button --extra-label "Back" --nocancel \
476                     --single-quoted \
477                     --checklist "Pick partition(s) containing a root (/) filesystem" 10 70 5 \
478                     $partslist \
479                     2> line
480                 if [ $? -eq 3 ]; then state=filesystems
481                 else
482                     root_filesystems=`cat line`
483                     state=network
484                 fi
485             fi
486             ;;
487
488         # Network configuration.
489         network)
490             if [ -n "$override_network" ]; then
491                 network="$override_network"
492                 state=verify
493             else
494                 partslist=""
495                 for r in $root_filesystems; do
496                     if [ "$r" = "$network" ]; then
497                         stat=on
498                     else
499                         stat=off
500                     fi
501                     partslist="$partslist $r Auto-configure $stat"
502                 done
503
504                 dialog \
505                     --extra-button --extra-label "Back" --nocancel \
506                     --radiolist "Network configuration" 10 70 5 \
507                     $partslist \
508                     "ask" "Manual configuration" off \
509                     "sh" "Configure from the shell" off \
510                     2> line
511                 if [ $? -eq 3 ]; then state=roots
512                 else
513                     network=`cat line`
514                     state=verify
515                 fi
516             fi
517             ;;
518
519         # Verify configuration.
520         verify)
521             if [ "$greeting" = "no" ]; then
522                 state=exit
523             else
524                 dialog \
525                     --title "Summary" \
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?" \
527                     23 70
528                 if [ $? -eq 1 ]; then
529                     state=transport
530                 else
531                     state=exit
532                 fi
533             fi
534             ;;
535         *)
536             echo "Invalid state: $state"
537             state=transport
538             ;;
539     esac
540 done
541
542 clear
543
544 # Now see if we can get a network configuration.
545 log network configuration $network
546
547 case "$network" in
548     sh)
549         echo "Network configuration"
550         echo
551         echo "Please configure the network from this shell."
552         echo
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."
556         echo
557         echo "When finished, exit with ^D or exit"
558         echo
559         shell
560         ;;
561
562     ask)
563         # XXX Not implemented
564         echo "Sorry, we didn't implement this one yet."
565         shell
566         ;;
567
568     *)
569         echo "Trying to auto-configure network from /dev/$network ..."
570         echo
571         if ! auto_network "$network"; then
572             echo "Auto-configuration failed.  Starting a shell."
573             echo
574             shell
575         fi
576 esac
577
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.
581
582
583
584
585
586
587
588
589
590
591
592
593 # This file must end with a newline
594