File: input_disk.ml

package info (click to toggle)
virt-v2v 2.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,132 kB
  • sloc: ml: 19,674; sh: 7,631; ansic: 6,897; makefile: 3,261; python: 1,114; perl: 852; xml: 114
file content (150 lines) | stat: -rw-r--r-- 4,855 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(* helper-v2v-input
 * Copyright (C) 2009-2021 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *)

open Printf
open Unix

open Std_utils
open Tools_utils
open Common_gettext.Gettext

open Types
open Utils

open Name_from_disk
open Input

module Disk = struct
  let to_string options args = String.concat " " ("-i disk" :: args)

  let query_input_options () =
    printf (f_"No input options can be used in this mode.\n")

  let rec setup dir options args =
    if options.input_options <> [] then
      error (f_"no -io (input options) are allowed here");

    if args = [] then
      error (f_"-i disk: expecting a disk image (filename) \
                on the command line");

    (* Check the input files exist and are readable. *)
    List.iter (fun disk -> access disk [R_OK]) args;

    (* What name should we use for the guest?  We try to derive it from
     * the first filename passed in.  Users can override this using the
     * `-on name' option.
     *)
    let name = name_from_disk (List.hd args) in

    let s_disks =
      List.mapi (
        fun i _ -> { s_disk_id = i; s_controller = None }
      ) args in

    (* Give the guest a simple generic network interface. *)
    let s_nic = {
      s_mac = None;
      s_nic_model = None;
      s_vnet = "default";
      s_vnet_type = Network;
    } in

    let source = {
      s_hypervisor = UnknownHV;
      s_name = name;
      s_genid = None;
      s_memory = 2048L *^ 1024L *^ 1024L; (* 2048 MB *)
      s_vcpu = 1;            (* 1 vCPU is a safe default *)
      s_cpu_vendor = None;
      s_cpu_model = None;
      s_cpu_topology = None;
      s_features = [ "acpi"; "apic"; "pae" ];
      s_firmware = UnknownFirmware; (* causes virt-v2v to autodetect *)
      s_display =
        Some { s_display_type = Window; s_keymap = None; s_password = None;
               s_listen = LNoListen; s_port = None };
      s_sound = None;
      s_disks = s_disks;
      s_removables = [];
      s_nics = [s_nic];
    } in

    let input_format = detect_local_input_format options args in

    (* Check nbdkit is installed. *)
    if not (Nbdkit.is_installed ()) then
      error (f_"nbdkit is not installed or not working.  It is required to \
                use ‘-i disk’.");

    if not (Nbdkit.probe_plugin "file") then
      error (f_"nbdkit-file-plugin is not installed or not working");
    if options.read_only && not (Nbdkit.probe_filter "cow") then
      error (f_"nbdkit-cow-filter is not installed or not working");

    List.iteri (
      fun i disk ->
        let socket = sprintf "%s/in%d" dir i in
        On_exit.unlink socket;

        match input_format with
        | "raw" ->
           let cmd = Nbdkit.create "file" in
           if options.read_only then
             Nbdkit.add_filter cmd "cow";
           Nbdkit.add_arg cmd "file" disk;
           if Nbdkit.version () >= (1, 22, 0) then
             Nbdkit.add_arg cmd "cache" "none";
           let _, pid = Nbdkit.run_unix socket cmd in

           (* --exit-with-parent should ensure nbdkit is cleaned
            * up when we exit, but it's not supported everywhere.
            *)
           On_exit.kill pid

        | format ->
           let cmd = QemuNBD.create disk in
           QemuNBD.set_snapshot cmd options.read_only;
           QemuNBD.set_format cmd (Some format);
           let _, pid = QemuNBD.run_unix socket cmd in
           On_exit.kill pid
    ) args;

    source

  (* For a list of local disks, try to detect the input format if
   * the [-if] option was not used on the command line.  If the
   * formats of the disks are different, that is an error.
   *)
  and detect_local_input_format { input_format } filenames =
    match input_format with
    | Some fmt -> fmt
    | None ->
       let formats =
         let g = open_guestfs () in
         List.map g#disk_format filenames in

       let rec get_format = function
         | [] -> error (f_"expected >= 1 disk name on the command line")
         | [x] -> x
         | x :: y :: xs when compare x y = 0 -> get_format (y :: xs)
         | _ -> error (f_"disks on the command line have mixed formats")
       in

       get_format formats
end