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
31 assert (String.length str = 4);
32 let c0 = Char.code (String.unsafe_get str 0) in
33 let c1 = Char.code (String.unsafe_get str 1) in
34 let c2 = Char.code (String.unsafe_get str 2) in
35 let c3 = Char.code (String.unsafe_get str 3) in
37 (Int64.shift_left (Int64.of_int c1) 8) +^
38 (Int64.shift_left (Int64.of_int c2) 16) +^
39 (Int64.shift_left (Int64.of_int c3) 24)
42 let c0 = i &^ 0xffL in
43 let c1 = Int64.shift_right (i &^ 0xff00L) 8 in
44 let c2 = Int64.shift_right (i &^ 0xff0000L) 16 in
45 let c3 = Int64.shift_right (i &^ 0xff000000L) 24 in
46 let s = String.create 4 in
47 String.unsafe_set s 0 (Char.unsafe_chr (Int64.to_int c0));
48 String.unsafe_set s 1 (Char.unsafe_chr (Int64.to_int c1));
49 String.unsafe_set s 2 (Char.unsafe_chr (Int64.to_int c2));
50 String.unsafe_set s 3 (Char.unsafe_chr (Int64.to_int c3));
53 let output_spaces chan n = for i = 0 to n-1 do output_char chan ' ' done
55 let wrap ?(chan = stdout) ?(hanging = 0) str =
56 let rec _wrap col str =
57 let n = String.length str in
58 let i = try String.index str ' ' with Not_found -> n in
61 output_char chan '\n';
62 output_spaces chan hanging;
65 output_string chan (String.sub str 0 i);
68 _wrap col (String.sub str (i+1) (n-(i+1)))
75 wrap ~chan:stderr ("virt-resize: error: " ^ str);
79 "If reporting bugs, run virt-resize with the '-d' option and include the complete output.";
85 (* The reverse of device name translation, see
86 * BLOCK DEVICE NAMING in guestfs(3).
88 let canonicalize dev =
89 if String.length dev >= 8 &&
90 dev.[0] = '/' && dev.[1] = 'd' && dev.[2] = 'e' && dev.[3] = 'v' &&
91 dev.[4] = '/' && (dev.[5] = 'h' || dev.[5] = 'v') && dev.[6] = 'd' then (
92 let dev = String.copy dev in
99 let feature_available (g : Guestfs.guestfs) names =
100 try g#available names; true
101 with G.Error _ -> false
103 (* Parse the size field from --resize and --resize-force options. *)
105 let const_re = Str.regexp "^\\([.0-9]+\\)\\([bKMG]\\)$"
106 and plus_const_re = Str.regexp "^\\+\\([.0-9]+\\)\\([bKMG]\\)$"
107 and minus_const_re = Str.regexp "^-\\([.0-9]+\\)\\([bKMG]\\)$"
108 and percent_re = Str.regexp "^\\([.0-9]+\\)%$"
109 and plus_percent_re = Str.regexp "^\\+\\([.0-9]+\\)%$"
110 and minus_percent_re = Str.regexp "^-\\([.0-9]+\\)%$"
113 let matches rex = Str.string_match rex field 0 in
114 let sub i = Str.matched_group i field in
115 let size_scaled f = function
116 | "b" -> Int64.of_float f
117 | "K" -> Int64.of_float (f *. 1024.)
118 | "M" -> Int64.of_float (f *. 1024. *. 1024.)
119 | "G" -> Int64.of_float (f *. 1024. *. 1024. *. 1024.)
123 if matches const_re then (
124 size_scaled (float_of_string (sub 1)) (sub 2)
126 else if matches plus_const_re then (
127 let incr = size_scaled (float_of_string (sub 1)) (sub 2) in
130 else if matches minus_const_re then (
131 let incr = size_scaled (float_of_string (sub 1)) (sub 2) in
134 else if matches percent_re then (
135 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
136 oldsize *^ percent /^ 1000L
138 else if matches plus_percent_re then (
139 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
140 oldsize +^ oldsize *^ percent /^ 1000L
142 else if matches minus_percent_re then (
143 let percent = Int64.of_float (10. *. float_of_string (sub 1)) in
144 oldsize -^ oldsize *^ percent /^ 1000L
147 error "virt-resize: %s: cannot parse size field" field
150 let sign, i = if i < 0L then "-", Int64.neg i else "", i in
153 sprintf "%s%Ld" sign i
155 let f = Int64.to_float i /. 1024. in
156 let i = i /^ 1024L in
158 sprintf "%s%.1fK" sign f
160 let f = Int64.to_float i /. 1024. in
161 let i = i /^ 1024L in
163 sprintf "%s%.1fM" sign f
165 let f = Int64.to_float i /. 1024. in
166 (*let i = i /^ 1024L in*)
167 sprintf "%s%.1fG" sign f