-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathin_place.ml
191 lines (161 loc) · 5.86 KB
/
in_place.ml
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
186
187
188
189
190
191
(* virt-sparsify
* Copyright (C) 2011-2025 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.
*)
(* This is the virt-sparsify --in-place mode. *)
open Unix
open Printf
open Std_utils
open Tools_utils
open Common_gettext.Gettext
open Utils
open Cmdline
module G = Guestfs
let run disk format ignores zeroes ks =
(* Connect to libguestfs. *)
let g = open_guestfs () in
(* Capture ^C and clean up gracefully.
*
* To safely set a signal handler, we must call [On_exit.register]
* because that may register signal handlers which we override.
*)
On_exit.register ();
let quit = ref false in
let set_quit _ = quit := true in
Sys.set_signal Sys.sigint (Sys.Signal_handle set_quit);
Sys.set_signal Sys.sigquit (Sys.Signal_handle set_quit);
g#set_pgroup true;
(* XXX Current limitation of the API. Can remove this hunk in future. *)
let format =
match format with
| Some _ -> format
| None -> Some (g#disk_format disk) in
g#add_drive ?format ~discard:"enable" disk;
if not (quiet ()) then (
let machine_readable = machine_readable () <> None in
Progress.set_up_progress_bar ~machine_readable g
);
g#set_network (key_store_requires_network ks);
g#launch ();
(* If discard is not supported in the appliance, we must return exit
* code 3. See the man page.
*)
if not (g#feature_available [|"fstrim"|]) then
error ~exit_code:3 (f_"discard/trim is not supported");
(* Decrypt the disks. *)
inspect_decrypt g ks;
(* Discard non-ignored filesystems that we are able to mount, and
* selected swap partitions.
*)
let filesystems = g#list_filesystems () in
let filesystems = List.map fst filesystems in
let filesystems = List.sort compare filesystems in
let is_ignored fs =
let fs = g#canonical_device_name fs in
List.exists (fun fs' -> fs = g#canonical_device_name fs') ignores
in
let is_read_only_lv = is_read_only_lv g in
let tasks =
List.map (
fun fs () ->
if not (is_ignored fs) && not (is_read_only_lv fs) then (
if List.mem fs zeroes then (
message (f_"Zeroing %s") fs;
if not (g#blkdiscardzeroes fs) then
g#zero_device fs;
g#blkdiscard fs
) else (
let mounted =
try g#mount_options "discard" fs "/"; true
with _ -> false in
if mounted then (
message (f_"Trimming %s") fs;
try g#fstrim "/"
with G.Error msg as exn ->
if g#last_errno () = G.Errno.errno_ENOTSUP then (
let vfs_type = try g#vfs_type fs with _ -> "unknown" in
warning (f_"fstrim operation is not supported on %s (%s). \
Suppress this warning using '--ignore %s', \
or use copying mode instead.")
fs vfs_type fs
)
else raise exn
) else (
let is_linux_x86_swap =
(* Look for the signature for Linux swap on i386.
* Location depends on page size, so it definitely won't
* work on non-x86 architectures (eg. on PPC, page size is
* 64K). Also this avoids hibernated swap space: in those,
* the signature is moved to a different location.
*)
try g#pread_device fs 10 4086L = "SWAPSPACE2"
with _ -> false in
if is_linux_x86_swap then (
message (f_"Clearing Linux swap on %s") fs;
(* Don't use mkswap. Just preserve the header containing
* the label, UUID and swap format version (libguestfs
* mkswap may differ from guest's own).
*)
let header = g#pread_device fs 4096 0L in
g#blkdiscard fs;
if g#pwrite_device fs header 0L <> 4096 then
error (f_"pwrite: short write restoring \
swap partition header")
)
)
);
g#umount_all ()
)
) filesystems in
(* Discard unused space in volume groups. *)
let vgs = g#vgs () in
let vgs = Array.to_list vgs in
let vgs = List.sort compare vgs in
let tasks = tasks @
List.map (
fun vg () ->
if not (List.mem vg ignores) then (
let lvname = String.random8 () in
let lvdev = "/dev/" ^ vg ^ "/" ^ lvname in
let created =
try g#lvcreate_free lvname vg 100; true
with _ -> false in
if created then (
message (f_"Discard space in volgroup %s") vg;
g#blkdiscard lvdev;
g#sync ();
g#lvremove lvdev
)
)
) vgs in
(* The above calls to List.map just created a list of tasks (thunks)
* to run. Now we actually run that code, keeping an eye on the
* state of the 'quit' flag.
*)
List.iter (
fun task ->
if not !quit then task ();
) tasks;
g#shutdown ();
g#close ();
if not !quit then (
(* Finished. *)
message (f_"Sparsify in-place operation completed with no errors")
)
else (
(* User quit. *)
error (f_"quit (^C) at user request")
)