-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathSubvolume.vala
More file actions
275 lines (215 loc) · 8.27 KB
/
Subvolume.vala
File metadata and controls
275 lines (215 loc) · 8.27 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
* Subvolume.vala
*
* Copyright 2012-2018 Tony George <teejeetech@gmail.com>
*
* 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.
*
*
*/
using TeeJee.Logging;
using TeeJee.FileSystem;
using TeeJee.JsonHelper;
using TeeJee.ProcessHelper;
using TeeJee.GtkHelper;
using TeeJee.System;
using TeeJee.Misc;
public class Subvolume : GLib.Object{
public string device_uuid;
public string name = "";
public string path = "";
public long id = -1;
public int64 total_bytes = 0;
public int64 unshared_bytes = 0;
public string mount_path = "";
//parent
public SnapshotRepo? repo;
public Subvolume(string name, string path, string parent_dev_uuid, SnapshotRepo? parent_repo){
this.name = name;
this.path = path;
this.device_uuid = parent_dev_uuid;
this.repo = parent_repo;
if (repo != null){
this.mount_path = repo.mount_paths[name];
}
}
public string total_formatted{
owned get{
return format_file_size(total_bytes);
}
}
public string unshared_formatted{
owned get{
return format_file_size(unshared_bytes);
}
}
public Device? get_device(){
return Device.get_device_by_uuid(device_uuid);
}
public bool exists_on_disk{
get {
return dir_exists(path);
}
}
public bool is_system_subvolume{
get {
return (repo == null);
}
}
public static Gee.HashMap<string, Subvolume> detect_subvolumes_for_system_by_path(
string system_path, SnapshotRepo? repo, Gtk.Window? parent_window){
var map = new Gee.HashMap<string, Subvolume>();
log_debug("Searching subvolume for system at path: %s".printf(system_path));
var fstab = FsTabEntry.read_file(path_combine(system_path, "/etc/fstab"));
var crypttab = CryptTabEntry.read_file(path_combine(system_path, "/etc/crypttab"));
foreach(var item in fstab){
if (!item.is_for_system_directory()){ continue; }
if (item.subvolume_name().length > 0){
var dev = item.resolve_device(crypttab, parent_window);
var dev_name = (dev == null) ? "" : dev.device;
var dev_uuid = (dev == null) ? "" : dev.uuid;
log_debug("Found subvolume: %s, on device: %s".printf(item.subvolume_name(), dev_name));
var subvol = new Subvolume(item.subvolume_name(), item.mount_point, dev_uuid, repo);
map.set(subvol.name, subvol);
}
}
return map;
}
public void print_info(){
log_debug("name=%s, uuid=%s, id=%ld, path=%s".printf(name, device_uuid, id, path));
}
// actions ----------------------------------
public bool remove(){
if (is_system_subvolume){
if (name == App.root_subvolume_name){
path = path_combine(App.mount_point_app + "/backup", App.root_subvolume_name);
}
else if (name == App.home_subvolume_name){
path = path_combine(App.mount_point_app + "/backup-home", App.home_subvolume_name);
}
}
string cmd = "";
string std_out, std_err, subpath;
int ret_val;
if (!dir_exists(path)){ return true; } // ok, item does not exist
log_msg("%s: %s (Id:%ld)".printf(_("Deleting subvolume"), name, id));
// always remove subvolumes recursively, this does not affect performance
string options = App.use_option_raw ? "--commit-after" : "";
if (Main.btrfs_version__can_recursive_delete) {
options += " --recursive";
}
subpath = path_combine(path, name);
if (dir_exists(subpath)) { // possible nested subvolume present, verify before deleting
int rc_show = exec_sync("btrfs subvolume show '%s'".printf(subpath), out std_out, out std_err);
if (rc_show == 0) {
// confirmed nested btrfs subvolume, delete it first
cmd = "btrfs subvolume delete %s '%s'".printf(options, subpath);
log_debug("Deleting nested subvolume in snapshot");
log_debug(cmd);
ret_val = exec_sync(cmd, out std_out, out std_err);
if (ret_val != 0){
log_error(std_err);
log_error(_("Failed to delete snapshot nested subvolume") + ": '%s'".printf(subpath));
return false;
}
} else {
// path exists but is not a btrfs subvolume, skip nested removal
log_debug("Nested path exists but is not a btrfs subvolume, skipping: %s".printf(subpath));
}
}
cmd = "btrfs subvolume delete %s '%s'".printf(options, path);
log_debug(cmd);
ret_val = exec_sync(cmd, out std_out, out std_err);
if (ret_val != 0){
log_error(std_err);
log_error(_("Failed to delete snapshot subvolume") + ": '%s'".printf(path));
return false;
}
log_msg("%s: %s (Id:%ld)\n".printf(_("Deleted subvolume"), name, id));
if (App.btrfs_qgroups_enabled) {
if ((id > 0) && (repo != null)){
log_debug("Waiting on btrfs to finish deleting...");
while (exec_sync("btrfs subvolume sync %s".printf(mount_path), out std_out, out std_err) != 0) {
log_debug("Still waiting for btrfs to finish deleting... %s".printf(std_err));
sleep(1000);
}
log_debug("Rescanning quotas...");
while (exec_sync("btrfs quota rescan %s".printf(mount_path), out std_out, out std_err) != 0) {
log_debug("Still rescanning quotas... %s".printf(std_err));
sleep(1000);
}
// Check if qgroup still exists before trying to destroy it.
// The qgroup may have been auto-destroyed when the subvolume was deleted.
string qgroup_id = "0/%ld".printf(id);
cmd = "btrfs qgroup show -f '%s'".printf(repo.mount_paths[name]);
log_debug(cmd);
ret_val = exec_sync(cmd, out std_out, out std_err);
bool qgroup_exists = (ret_val == 0) && (std_out.contains(qgroup_id));
if (!qgroup_exists) {
log_debug("Qgroup %s already removed (auto-destroyed with subvolume)".printf(qgroup_id));
} else {
log_msg("%s: %s".printf(_("Destroying qgroup"), qgroup_id));
cmd = "btrfs qgroup destroy %s '%s'".printf(qgroup_id, repo.mount_paths[name]);
log_debug(cmd);
ret_val = exec_sync(cmd, out std_out, out std_err);
if (ret_val != 0){
// Qgroup may have been destroyed between our check and destroy attempt
log_debug("Qgroup %s could not be destroyed (likely already removed): %s".printf(qgroup_id, std_err));
} else {
log_msg("%s: %s\n".printf(_("Destroyed qgroup"), qgroup_id));
}
}
log_debug("Rescanning quotas (post)...");
while (exec_sync("btrfs quota rescan %s".printf(mount_path), out std_out, out std_err) != 0) {
log_debug("Still rescanning quotas (post)... %s".printf(std_err));
sleep(1000);
}
log_debug("Final sync (post)...");
while (exec_sync("btrfs subvolume sync %s".printf(mount_path), out std_out, out std_err) != 0) {
log_debug("Still syncing (post)... %s".printf(std_err));
sleep(1000);
}
}
}
return true;
}
public bool restore(){
if (is_system_subvolume) { return false; }
// restore snapshot subvolume by creating new subvolume snapshots ----------------------
string src_path = path;
string dst_path = path_combine(mount_path, name);
if (!dir_exists(src_path)){
log_error("%s: %s".printf(_("Not Found"), src_path));
return false;
}
if (dir_exists(dst_path)){
log_error("%s: %s".printf(_("Subvolume exists at destination"), dst_path));
return false;
}
string cmd = "btrfs subvolume snapshot '%s' '%s'".printf(src_path, dst_path);
log_debug(cmd);
string std_out, std_err;
int status = exec_sync(cmd, out std_out, out std_err);
if (status != 0){
log_error(std_err);
log_error(_("btrfs returned an error") + ": %d".printf(status));
log_error(_("Failed to restore system subvolume") + ": %s".printf(name));
return false;
}
log_msg(_("Restored system subvolume") + ": %s".printf(name));
return true;
}
}