Use of dialog to capture user input.
[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 # By Richard W.M. Jones <rjones@redhat.com>
7 #
8 # Copyright (C) 2007 Red Hat Inc.
9
10 export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin
11
12 # The defaults here make a generic virt-p2v.sh script, but if you want
13 # to build a partially-/fully-automatic P2V solution, then you can set
14 # these variables to something, and the script won't ask the user for
15 # input.
16
17 # greeting=no
18 greeting=
19
20 override_remote_host=
21 override_remote_port=
22
23 # can be 'ssh' or 'tcp'
24 override_remote_transport=
25
26 # eg. override_remote_directory=/var/lib/xen/images
27 # (only if override_remote_transport is 'ssh')
28 override_remote_directory=
29
30 # list of devices to send, separated by spaces, if empty ask user
31 override_devices_to_send=""
32
33 #----------------------------------------------------------------------
34 # Device mapper snapshotting.
35
36 # Create a device-mapper snapshot of a device with ramdisk overlay.
37 # Example:
38 #   snapshot /dev/sda1 snap
39 # creates a snapshot of /dev/sda1 called /dev/mapper/snap
40
41
42 # XXX Error checking.
43 function snapshot {
44     dev=$1
45     name=$2
46
47     # Get size of the device in sectors.
48     sectors=`blockdev --getsize $dev`
49
50     dmsetup create ${name}_org \
51         --table="0 $sectors snapshot-origin $dev"
52     dmsetup create $name \
53         --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram1 n 64"
54 }
55
56 # Drop an existing snapshot created by snapshot function.
57 # Example:
58 #   drop_snapshot snap
59 # drops a snapshot called /dev/mapper/snap
60 function drop_snapshot {
61     dmsetup remove /dev/mapper/$name
62     dmsetup remove /dev/mapper/${name}_org
63 }
64
65
66 #----------------------------------------------------------------------
67 # General script setup.
68
69 # We can safely write files into /tmp without modifying anything.
70 cd /tmp
71
72 #----------------------------------------------------------------------
73 # Dialog with the user.
74
75 if [ "$greeting" != "no" ]; then
76     dialog \
77         --title "virt-p2v" \
78         --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
79 fi
80
81 # Get configuration from the user.
82
83 # To make the [Back] button work, we make this into a looping state
84 # machine.  Each state asks a question and jumps to the next state
85 # (unless [Back] is pressed, in which case it jumps back to the previous
86 # state).  Finally the 'exit' state causes us to quit the loop.
87
88 state=hostname
89 while [ "$state" != "exit" ]; do
90     case "$state" in
91         hostname)
92             if [ -n "$override_remote_host" ]; then
93                 remote_host="$override_remote_host"
94             else
95                 dialog \
96                     --nocancel \
97                     --inputbox "Remote host" 10 50 "$remote_host" \
98                     2> line
99                 remote_host=`cat line`
100                 state=port
101             fi
102             ;;
103         port)
104             if [ -n "$override_remote_port" ]; then
105                 remote_port="$override_remote_port"
106             else
107                 dialog \
108                     --extra-button --extra-label "Back" --nocancel \
109                     --inputbox "Remote port" 10 50 "$remote_port" \
110                     2> line
111                 if [ $? -eq 3 ]; then state=hostname
112                 else
113                     remote_port=`cat line`
114                     state=exit
115                 fi
116             fi
117             ;;
118
119         *)
120             echo "Invalid state: $state"
121             state=hostname
122             ;;
123     esac
124 done
125
126 clear
127 echo remote_host $remote_host
128 echo remote_port $remote_port