+(* Shell-safe quoting function. In fact there's one in stdlib so use it. *)
+let quote = Filename.quote
+
+(* Run a shell command and check it returns 0. *)
+let sh cmd =
+ eprintf "sh: %s\n%!" cmd;
+ if Sys.command cmd <> 0 then fail_dialog (sprintf "Command failed:\n\n%s" cmd)
+
+let shfailok cmd =
+ eprintf "shfailok: %s\n%!" cmd;
+ ignore (Sys.command cmd)
+
+let shwithstatus cmd =
+ eprintf "shwithstatus: %s\n%!" cmd;
+ Sys.command cmd
+
+(* Same as `cmd` in shell. Any error message will be in the logfile. *)
+let shget cmd =
+ eprintf "shget: %s\n%!" cmd;
+ let chan = open_process_in cmd in
+ let lines = input_all_lines chan in
+ match close_process_in chan with
+ | WEXITED 0 -> Some lines (* command succeeded *)
+ | WEXITED _ -> None (* command failed *)
+ | WSIGNALED i -> failwith (sprintf "shget: command killed by signal %d" i)
+ | WSTOPPED i -> failwith (sprintf "shget: command stopped by signal %d" i)
+
+(* Start an interactive shell. *)
+let shell () =
+ shfailok "PS1='\\u@\\h:\\w\\$ ' bash"
+
+(* Some true if is dir/file, Some false if not, None if not found. *)
+let is_dir path =
+ try Some ((stat path).st_kind = S_DIR)
+ with Unix_error (ENOENT, "stat", _) -> None
+let is_file path =
+ try Some ((stat path).st_kind = S_REG)
+ with Unix_error (ENOENT, "stat", _) -> None
+
+(* Useful regular expression. *)
+let whitespace = Pcre.regexp "[ \t]+"
+
+(* Generate a predictable safe name containing only letters, numbers
+ * and underscores. If passed a string with no letters or numbers,
+ * generates "_1", "_2", etc.
+ *)
+let safe_name =
+ let next_anon =
+ let i = ref 0 in
+ fun () -> incr i; "_" ^ string_of_int !i
+ in
+ fun name ->
+ let is_safe = function 'a'..'z'|'A'..'Z'|'0'..'9' -> true | _ -> false in
+ let name = String.copy name in
+ let have_safe = ref false in
+ for i = 0 to String.length name - 1 do
+ if not (is_safe name.[i]) then name.[i] <- '_' else have_safe := true
+ done;
+ if !have_safe then name else next_anon ()
+
+type block_device = string * int64 (* "hda" & size in bytes *)
+
+(* Parse the output of 'lvs' to get list of LV names, sizes,
+ * corresponding PVs, etc. Returns a list of (lvname, PVs, lvsize).
+ *)
+let get_lvs =
+ let devname = Pcre.regexp "^/dev/(.+)\\(.+\\)$" in
+
+ function () ->
+ match
+ shget "lvs --noheadings -o vg_name,lv_name,devices,lv_size"
+ with
+ | None -> []
+ | Some lines ->
+ let lines = List.map (Pcre.split ~rex:whitespace) lines in
+ List.map (
+ function
+ | [vg; lv; pvs; lvsize]
+ | [_; vg; lv; pvs; lvsize] ->
+ let pvs = String.nsplit pvs "," in
+ let pvs = List.filter_map (
+ fun pv ->
+ try
+ let subs = Pcre.exec ~rex:devname pv in
+ Some (Pcre.get_substring subs 1)
+ with
+ Not_found ->
+ eprintf "lvs: unexpected device name: %s\n%!" pv;
+ None
+ ) pvs in
+ LV (vg, lv), pvs, lvsize
+ | line ->
+ failwith ("lvs: unexpected output: " ^ String.concat "," line)
+ ) lines
+
+(* Get the partitions on a block device.
+ * eg. "sda" -> [Part ("sda","1"); Part ("sda", "2")]
+ *)
+let get_partitions dev =
+ let rex = Pcre.regexp ("^" ^ dev ^ "(.+)$") in
+ let devdir = "/sys/block/" ^ dev in
+ let parts = Sys.readdir devdir in
+ let parts = Array.to_list parts in
+ let parts = List.filter (
+ fun name -> Some true = is_dir (devdir ^ "/" ^ name)
+ ) parts in
+ let parts = List.filter_map (
+ fun part ->
+ try
+ let subs = Pcre.exec ~rex part in
+ Some (Part (dev, Pcre.get_substring subs 1))
+ with
+ Not_found -> None
+ ) parts in
+ parts
+
+(* Generate snapshot device name from device name. *)
+let snapshot_name dev =
+ "snap" ^ (safe_name dev)
+
+(* Perform a device-mapper snapshot with ramdisk overlay. *)
+let snapshot =
+ let next_free_ram_disk =
+ let i = ref 0 in
+ fun () -> incr i; "/dev/ram" ^ string_of_int !i
+ in
+ fun origin_dev snapshot_dev ->
+ let ramdisk = next_free_ram_disk () in
+ let sectors =
+ let cmd = "blockdev --getsz " ^ quote ("/dev/" ^ origin_dev) in
+ let lines = shget cmd in
+ match lines with
+ | Some (sectors::_) -> Int64.of_string sectors
+ | Some [] | None ->
+ fail_dialog (sprintf "Snapshot failed - unable to read the size in sectors of block device %s" origin_dev) in
+
+ (* Create the snapshot origin device. Called, eg. snap_sda1_org *)
+ sh (sprintf "dmsetup create %s_org --table='0 %Ld snapshot-origin /dev/%s'"
+ snapshot_dev sectors origin_dev);
+ (* Create the snapshot. *)
+ sh (sprintf "dmsetup create %s --table='0 %Ld snapshot /dev/mapper/%s_org %s n 64'"
+ snapshot_dev sectors snapshot_dev ramdisk)
+
+(* Try to perform automatic network configuration, assuming a Fedora or RHEL-
+ * like root filesystem mounted on /mnt/root.
+ *)
+let auto_network state =
+ (* Fedora gives an error if this file doesn't exist. *)
+ sh "touch /etc/resolv.conf";
+
+ chdir "/etc/sysconfig";
+
+ sh "mv network network.saved";
+ sh "mv networking networking.saved";
+ sh "mv network-scripts network-scripts.saved";
+
+ (* Originally I symlinked these, but that causes dhclient to
+ * keep open /mnt/root (as its cwd is in network-scripts subdir).
+ * So now we will copy them recursively instead.
+ *)
+ sh "cp -r /mnt/root/etc/sysconfig/network .";
+ sh "cp -r /mnt/root/etc/sysconfig/networking .";
+ sh "cp -r /mnt/root/etc/sysconfig/network-scripts .";
+
+ let status = shwithstatus "/etc/init.d/network start" in
+
+ sh "rm -rf network networking network-scripts";
+ sh "mv network.saved network";
+ sh "mv networking.saved networking";
+ sh "mv network-scripts.saved network-scripts";
+
+ chdir "/tmp";
+
+ (* Try to ping the remote host to see if this worked. *)
+ shfailok ("ping -c 3 " ^ Option.map_default quote "" state.remote_host);
+
+ if state.greeting then (
+ printf "\n\nDid automatic network configuration work?\n";
+ printf "Hint: If not sure, there is a shell on console [ALT] [F2]\n";
+ printf " (y/n) %!";
+ let line = read_line () in
+ String.length line > 0 && (line.[0] = 'y' || line.[0] = 'Y')
+ )
+ else
+ (* Non-interactive: return the status of /etc/init.d/network start. *)
+ status = 0
+
+(* Map local device names to remote devices names. At the moment we
+ * just change sd* to hd* (as device names appear under fullvirt). In
+ * future, lots of complex possibilities.
+ *)
+let remote_of_origin_dev =
+ let devsd = Pcre.regexp "^sd([[:alpha:]]+[[:digit:]]*)$" in
+ let devsd_subst = Pcre.subst "hd$1" in
+ fun dev ->
+ Pcre.replace ~rex:devsd ~itempl:devsd_subst dev
+
+(* Rewrite /mnt/root/etc/fstab. *)
+let rewrite_fstab state devices_to_send =
+ let filename = "/mnt/root/etc/fstab" in
+ if is_file filename = Some true then (
+ sh ("cp " ^ quote filename ^ " " ^ quote (filename ^ ".p2vsaved"));
+
+ let chan = open_in filename in
+ let lines = input_all_lines chan in
+ close_in chan;
+ let lines = List.map (Pcre.split ~rex:whitespace) lines in
+ let lines = List.map (
+ function
+ | dev :: rest when String.starts_with dev "/dev/" ->
+ let dev = String.sub dev 5 (String.length dev - 5) in
+ let dev = remote_of_origin_dev dev in
+ let dev = "/dev/" ^ dev in
+ dev :: rest
+ | line -> line
+ ) lines in
+
+ let chan = open_out filename in
+ List.iter (
+ function
+ | [dev; mountpoint; fstype; options; freq; passno] ->
+ fprintf chan "%-23s %-23s %-7s %-15s %s %s\n"
+ dev mountpoint fstype options freq passno
+ | line ->
+ output_string chan (String.concat " " line)
+ ) lines;
+ close_out chan
+ )
+