File: cmdline.ml

package info (click to toggle)
libguestfs 1%3A1.40.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,660 kB
  • sloc: ansic: 460,074; ml: 63,059; sh: 14,955; java: 9,512; makefile: 9,133; cs: 6,300; haskell: 5,652; python: 3,856; perl: 3,619; erlang: 2,435; xml: 1,683; ruby: 350; pascal: 255; lex: 135; yacc: 128; cpp: 10
file content (185 lines) | stat: -rw-r--r-- 6,240 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
(* virt-sparsify
 * Copyright (C) 2011-2019 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.
 *)

(* Command line argument parsing. *)

open Printf

open Std_utils
open Tools_utils
open Common_gettext.Gettext
open Getopt.OptionName

open Utils

type cmdline = {
  indisk : string;
  format : string option;
  ignores : string list;
  zeroes : string list;
  mode : mode_t;
  ks : key_store;
}

and mode_t =
| Mode_copying of
    string * check_t * bool * string option * string option * string option
| Mode_in_place
and check_t = [`Ignore|`Continue|`Warn|`Fail]

let parse_cmdline () =
  let add xs s = List.push_front s xs in

  let check_tmpdir = ref `Warn in
  let set_check_tmpdir = function
    | "ignore" | "i" -> check_tmpdir := `Ignore
    | "continue" | "cont" | "c" -> check_tmpdir := `Continue
    | "warn" | "warning" | "w" -> check_tmpdir := `Warn
    | "fail" | "f" | "error" -> check_tmpdir := `Fail
    | str ->
      error (f_"--check-tmpdir: unknown argument ā€˜%s’") str
  in

  let compress = ref false in
  let convert = ref "" in
  let format = ref "" in
  let ignores = ref [] in
  let in_place = ref false in
  let option = ref "" in
  let tmp = ref "" in
  let zeroes = ref [] in

  let argspec = [
    [ L"check-tmpdir" ], Getopt.String ("ignore|...", set_check_tmpdir),  s_"Check there is enough space in $TMPDIR";
    [ L"compress" ], Getopt.Set compress,         s_"Compressed output format";
    [ L"convert" ], Getopt.Set_string (s_"format", convert),    s_"Format of output disk (default: same as input)";
    [ L"format" ],  Getopt.Set_string (s_"format", format),     s_"Format of input disk";
    [ L"ignore" ],  Getopt.String (s_"fs", add ignores),  s_"Ignore filesystem";
    [ L"in-place"; L"inplace" ], Getopt.Set in_place,         s_"Modify the disk image in-place";
    [ S 'o' ],        Getopt.Set_string (s_"option", option),     s_"Add qemu-img options";
    [ L"tmp" ],     Getopt.Set_string (s_"block|dir|prebuilt:file", tmp),        s_"Set temporary block device, directory or prebuilt file";
    [ L"zero" ],    Getopt.String (s_"fs", add zeroes),   s_"Zero filesystem";
  ] in
  let disks = ref [] in
  let anon_fun s = List.push_front s disks in
  let usage_msg =
    sprintf (f_"\
%s: sparsify a virtual machine disk

 virt-sparsify [--options] indisk outdisk

 virt-sparsify [--options] --in-place disk

A short summary of the options is given below.  For detailed help please
read the man page virt-sparsify(1).
")
      prog in
  let opthandle = create_standard_options argspec ~anon_fun ~key_opts:true ~machine_readable:true usage_msg in
  Getopt.parse opthandle.getopt;

  (* Dereference the rest of the args. *)
  let check_tmpdir = !check_tmpdir in
  let compress = !compress in
  let convert = match !convert with "" -> None | str -> Some str in
  let disks = List.rev !disks in
  let format = match !format with "" -> None | str -> Some str in
  let ignores = List.rev !ignores in
  let in_place = !in_place in
  let option = match !option with "" -> None | str -> Some str in
  let tmp = match !tmp with "" -> None | str -> Some str in
  let zeroes = List.rev !zeroes in

  (* No arguments and machine-readable mode?  Print out some facts
   * about what this binary supports.
   *)
  (match disks, machine_readable () with
  | [], Some { pr } ->
    pr "virt-sparsify\n";
    pr "linux-swap\n";
    pr "zero\n";
    pr "check-tmpdir\n";
    pr "in-place\n";
    pr "tmp-option\n";
    let g = open_guestfs () in
    g#add_drive "/dev/null";
    g#launch ();
    if g#feature_available [| "ntfsprogs"; "ntfs3g" |] then
      pr "ntfs\n";
    if g#feature_available [| "btrfs" |] then
      pr "btrfs\n";
    exit 0
  | _, _ -> ()
  );

  let indisk, mode =
    if not in_place then (      (* copying mode checks *)
      let indisk, outdisk =
        match disks with
        | [indisk; outdisk] -> indisk, outdisk
        | _ -> error (f_"usage: %s [--options] indisk outdisk") prog in

      (* Simple-minded check that the user isn't trying to use the
       * same disk for input and output.
       *)
      if indisk = outdisk then
        error (f_"you cannot use the same disk image for input and output");

      (* The input disk must be an absolute path, so we can store the name
       * in the overlay disk.
       *)
      let indisk = absolute_path indisk in

      (* Check the output is not a char special (RHBZ#1056290). *)
      if is_char_device outdisk then
        error (f_"output ā€˜%s’ cannot be a character device, it must be a regular file")
              outdisk;

      indisk,
      Mode_copying (outdisk, check_tmpdir, compress, convert, option, tmp)
    )
    else (                      (* --in-place checks *)
      let indisk =
        match disks with
        | [indisk] -> indisk
        | _ -> error "usage: %s --in-place [--options] indisk" prog in

      if check_tmpdir <> `Warn then
        error (f_"you cannot use --in-place and --check-tmpdir options together");

      if compress then
        error (f_"you cannot use --in-place and --compress options together");

      if convert <> None then
        error (f_"you cannot use --in-place and --convert options together");

      if option <> None then
        error (f_"you cannot use --in-place and -o options together");

      if tmp <> None then
        error (f_"you cannot use --in-place and --tmp options together");

      indisk, Mode_in_place
    ) in

  { indisk = indisk;
    format = format;
    ignores = ignores;
    zeroes = zeroes;
    mode = mode;
    ks = opthandle.ks;
  }