Skip to content

Commit 797d8c2

Browse files
committed
feat: add option to create read-only Btrfs snapshots
1 parent 06a6a14 commit 797d8c2

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

files/timeshift.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"parent_device_uuid" : "",
44
"do_first_run" : "true",
55
"btrfs_mode" : "false",
6+
"btrfs_readonly" : "true",
67
"include_btrfs_home" : "false",
78
"stop_cron_emails" : "true",
89
"schedule_monthly" : "false",

src/Core/Main.vala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Main : GLib.Object{
5050
public string backup_parent_uuid = "";
5151

5252
public bool btrfs_mode = true;
53+
public bool btrfs_readonly = true;
5354
public bool include_btrfs_home_for_backup = false;
5455
public bool include_btrfs_home_for_restore = false;
5556

@@ -1654,7 +1655,7 @@ public class Main : GLib.Object{
16541655
dst_path = dst_path.replace("/@home/@home", "/@home");
16551656
}
16561657

1657-
string cmd = "btrfs subvolume snapshot '%s' '%s' \n".printf(src_path, dst_path);
1658+
string cmd = "btrfs subvolume snapshot '%s' '%s' '%s' \n".printf(btrfs_readonly ? "-r" : "", src_path, dst_path);
16581659

16591660
if (LOG_COMMANDS) { log_debug(cmd); }
16601661

@@ -1683,7 +1684,7 @@ public class Main : GLib.Object{
16831684
// write control file
16841685
var snapshot = Snapshot.write_control_file(
16851686
snapshot_path, dt_created, sys_uuid, current_distro.full_name(),
1686-
initial_tags, cmd_comments, 0, true, false, repo);
1687+
initial_tags, btrfs_readonly ? "ReadOnly" : cmd_comments, 0, true, false, repo);
16871688

16881689
// write subvolume info
16891690
foreach(var subvol in sys_subvolumes.values){
@@ -3207,6 +3208,7 @@ public class Main : GLib.Object{
32073208

32083209
config.set_string_member("do_first_run", false.to_string());
32093210
config.set_string_member("btrfs_mode", btrfs_mode.to_string());
3211+
config.set_string_member("btrfs_readonly", btrfs_readonly.to_string());
32103212
config.set_string_member("include_btrfs_home_for_backup", include_btrfs_home_for_backup.to_string());
32113213
config.set_string_member("include_btrfs_home_for_restore", include_btrfs_home_for_restore.to_string());
32123214
config.set_string_member("stop_cron_emails", stop_cron_emails.to_string());
@@ -3304,6 +3306,8 @@ public class Main : GLib.Object{
33043306
bool do_first_run = json_get_bool(config, "do_first_run", false); // false as default
33053307

33063308
btrfs_mode = json_get_bool(config, "btrfs_mode", false); // false as default
3309+
3310+
btrfs_readonly = json_get_bool(config, "btrfs_readonly", true); // true as default
33073311

33083312
if (do_first_run){
33093313
set_first_run_flag();

src/Gtk/SnapshotBackendBox.vala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class SnapshotBackendBox : Gtk.Box{
3636

3737
private Gtk.RadioButton opt_rsync;
3838
private Gtk.RadioButton opt_btrfs;
39+
private Gtk.CheckButton opt_btrfs_readonly;
3940
private Gtk.Label lbl_description;
4041
private Gtk.Window parent_window;
4142

@@ -103,15 +104,34 @@ class SnapshotBackendBox : Gtk.Box{
103104
}
104105

105106
opt_btrfs.toggled.connect(()=>{
107+
add_opt_btrfs_ro(hbox);
106108
if (opt_btrfs.active){
107109
App.btrfs_mode = true;
108110
init_backend();
109111
type_changed();
110112
update_description();
111113
}
114+
this.show_all();
112115
});
113116
}
114117

118+
private void add_opt_btrfs_ro(Gtk.Box hbox){
119+
if (opt_btrfs.active){
120+
var opt = new Gtk.CheckButton.with_label(_("Create Read-only snapshots by default"));
121+
hbox.add(opt);
122+
opt_btrfs_readonly = opt;
123+
124+
opt_btrfs_readonly.toggled.connect(()=>{
125+
App.btrfs_readonly = true;
126+
});
127+
} else {
128+
if (opt_btrfs_readonly != null){
129+
hbox.remove(opt_btrfs_readonly);
130+
opt_btrfs_readonly = null;
131+
}
132+
}
133+
}
134+
115135
private bool check_for_btrfs_tools() {
116136
try {
117137
const string args[] = {"lsblk", "-o", "FSTYPE", null};
@@ -212,6 +232,7 @@ class SnapshotBackendBox : Gtk.Box{
212232
public void refresh(){
213233

214234
opt_btrfs.active = App.btrfs_mode;
235+
opt_btrfs_readonly.active = App.btrfs_readonly;
215236
type_changed();
216237
update_description();
217238
}

0 commit comments

Comments
 (0)