Use of dialog to capture user input.
[virt-p2v.git] / virt-p2v.sh
index 16d3fd9..44184c7 100755 (executable)
@@ -1,9 +1,128 @@
 #!/bin/bash
 #
 # virt-p2v.sh is a shell script which performs a physical to
-# virtual conversion of local disks, interactively.
+# virtual conversion of local disks.
 #
 # By Richard W.M. Jones <rjones@redhat.com>
+#
+# Copyright (C) 2007 Red Hat Inc.
+
+export PATH=/usr/sbin:/sbin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin
+
+# The defaults here make a generic virt-p2v.sh script, but if you want
+# to build a partially-/fully-automatic P2V solution, then you can set
+# these variables to something, and the script won't ask the user for
+# input.
+
+# greeting=no
+greeting=
+
+override_remote_host=
+override_remote_port=
+
+# can be 'ssh' or 'tcp'
+override_remote_transport=
+
+# eg. override_remote_directory=/var/lib/xen/images
+# (only if override_remote_transport is 'ssh')
+override_remote_directory=
+
+# list of devices to send, separated by spaces, if empty ask user
+override_devices_to_send=""
+
+#----------------------------------------------------------------------
+# Device mapper snapshotting.
+
+# Create a device-mapper snapshot of a device with ramdisk overlay.
+# Example:
+#   snapshot /dev/sda1 snap
+# creates a snapshot of /dev/sda1 called /dev/mapper/snap
+
+
+# XXX Error checking.
+function snapshot {
+    dev=$1
+    name=$2
+
+    # Get size of the device in sectors.
+    sectors=`blockdev --getsize $dev`
+
+    dmsetup create ${name}_org \
+       --table="0 $sectors snapshot-origin $dev"
+    dmsetup create $name \
+       --table="0 $sectors snapshot /dev/mapper/${name}_org /dev/ram1 n 64"
+}
+
+# Drop an existing snapshot created by snapshot function.
+# Example:
+#   drop_snapshot snap
+# drops a snapshot called /dev/mapper/snap
+function drop_snapshot {
+    dmsetup remove /dev/mapper/$name
+    dmsetup remove /dev/mapper/${name}_org
+}
+
+
+#----------------------------------------------------------------------
+# General script setup.
+
+# We can safely write files into /tmp without modifying anything.
+cd /tmp
+
+#----------------------------------------------------------------------
+# Dialog with the user.
+
+if [ "$greeting" != "no" ]; then
+    dialog \
+       --title "virt-p2v" \
+       --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
+fi
+
+# Get configuration from the user.
+
+# To make the [Back] button work, we make this into a looping state
+# machine.  Each state asks a question and jumps to the next state
+# (unless [Back] is pressed, in which case it jumps back to the previous
+# state).  Finally the 'exit' state causes us to quit the loop.
 
+state=hostname
+while [ "$state" != "exit" ]; do
+    case "$state" in
+       hostname)
+           if [ -n "$override_remote_host" ]; then
+               remote_host="$override_remote_host"
+           else
+               dialog \
+                   --nocancel \
+                   --inputbox "Remote host" 10 50 "$remote_host" \
+                   2> line
+               remote_host=`cat line`
+               state=port
+           fi
+           ;;
+       port)
+           if [ -n "$override_remote_port" ]; then
+               remote_port="$override_remote_port"
+           else
+               dialog \
+                   --extra-button --extra-label "Back" --nocancel \
+                   --inputbox "Remote port" 10 50 "$remote_port" \
+                   2> line
+               if [ $? -eq 3 ]; then state=hostname
+               else
+                   remote_port=`cat line`
+                   state=exit
+               fi
+           fi
+           ;;
 
+       *)
+           echo "Invalid state: $state"
+           state=hostname
+           ;;
+    esac
+done
 
+clear
+echo remote_host $remote_host
+echo remote_port $remote_port
\ No newline at end of file