2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 let ( +^ ) = Int64.add
24 let ( -^ ) = Int64.sub
25 let ( *^ ) = Int64.mul
26 let ( /^ ) = Int64.div
27 let ( &^ ) = Int64.logand
28 let ( ~^ ) = Int64.lognot
30 let output_spaces chan n = for i = 0 to n-1 do output_char chan ' ' done
32 let wrap ?(chan = stdout) ?(hanging = 0) str =
33 let rec _wrap col str =
34 let n = String.length str in
35 let i = try String.index str ' ' with Not_found -> n in
38 output_char chan '\n';
39 output_spaces chan hanging;
42 output_string chan (String.sub str 0 i);
45 _wrap col (String.sub str (i+1) (n-(i+1)))
52 wrap ~chan:stderr ("virt-resize: error: " ^ str);
56 "If reporting bugs, run virt-resize with the '-d' option and include the complete output.";
62 (* The reverse of device name translation, see
63 * BLOCK DEVICE NAMING in guestfs(3).
65 let canonicalize dev =
66 if String.length dev >= 8 &&
67 dev.[0] = '/' && dev.[1] = 'd' && dev.[2] = 'e' && dev.[3] = 'v' &&
68 dev.[4] = '/' && (dev.[5] = 'h' || dev.[5] = 'v') && dev.[6] = 'd' then (
69 let dev = String.copy dev in
76 let feature_available (g : Guestfs.guestfs) names =
77 try g#available names; true
78 with G.Error _ -> false
80 (* Parse the size field from --resize and --resize-force options. *)
82 let const_re = Pcre.regexp "^([.\\d]+)([bKMG])$"
83 and plus_const_re = Pcre.regexp "^\\+([.\\d]+)([bKMG])$"
84 and minus_const_re = Pcre.regexp "^-([.\\d]+)([bKMG])$"
85 and percent_re = Pcre.regexp "^([.\\d]+)%$"
86 and plus_percent_re = Pcre.regexp "^\\+([.\\d]+)%$"
87 and minus_percent_re = Pcre.regexp "^-([.\\d]+)%$"
90 let subs = ref None in
92 try subs := Some (Pcre.exec ~rex field); true
93 with Not_found -> false
96 match !subs with None -> assert false
97 | Some subs -> Pcre.get_substring subs i
99 let size_scaled f = function
100 | "b" -> Int64.of_float f
101 | "K" -> Int64.of_float (f *. 1024.)
102 | "M" -> Int64.of_float (f *. 1024. *. 1024.)
103 | "G" -> Int64.of_float (f *. 1024. *. 1024. *. 1024.)
107 if matches const_re then (
108 size_scaled (float_of_string (sub 1)) (sub 2)
110 else if matches plus_const_re then (
111 let incr = size_scaled (float_of_string (sub 1)) (sub 2) in
114 else if matches minus_const_re then (
115 let incr = size_scaled (float_of_string (sub 1)) (sub 2) in
118 else if matches percent_re then (
119 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
120 oldsize *^ percent /^ 1000L
122 else if matches plus_percent_re then (
123 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
124 oldsize +^ oldsize *^ percent /^ 1000L
126 else if matches minus_percent_re then (
127 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
128 oldsize -^ oldsize *^ percent /^ 1000L
131 error "virt-resize: %s: cannot parse size field" field
134 let sign, i = if i < 0L then "-", Int64.neg i else "", i in
137 sprintf "%s%Ld" sign i
139 let f = Int64.to_float i /. 1024. in
140 let i = i /^ 1024L in
142 sprintf "%s%.1fK" sign f
144 let f = Int64.to_float i /. 1024. in
145 let i = i /^ 1024L in
147 sprintf "%s%.1fM" sign f
149 let f = Int64.to_float i /. 1024. in
150 (*let i = i /^ 1024L in*)
151 sprintf "%s%.1fG" sign f