Skip to content

Commit cfb5ee0

Browse files
committed
config: remount rw/ro during configuration saving process
Until there's decent OverlayFS support for configuration, just remount. Without it, the whole configuration web server is basically useless.
1 parent 9c40c66 commit cfb5ee0

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/config.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ use bluer::Address;
22
use serde::de::{self, Deserializer, Error as DeError, Visitor};
33
use serde::{Deserialize, Serialize};
44
use simplelog::*;
5-
use std::fmt::{self, Display};
6-
use std::fs;
7-
use std::path::PathBuf;
8-
use std::str::FromStr;
5+
use std::{
6+
fmt::{self, Display},
7+
fs, io,
8+
path::PathBuf,
9+
process::{Command, Stdio},
10+
str::FromStr,
11+
};
912
use toml_edit::{value, DocumentMut};
1013

14+
// module name for logging engine
15+
const NAME: &str = "<i><bright-black> config: </>";
16+
1117
#[derive(
1218
clap::ValueEnum, Default, Debug, PartialEq, PartialOrd, Clone, Copy, Deserialize, Serialize,
1319
)]
@@ -170,6 +176,34 @@ impl Default for AppConfig {
170176
}
171177
}
172178

179+
/// Remount `/` as readonly (`lock = true`) or read-write (`lock = false`)
180+
fn remount_root(lock: bool) -> io::Result<()> {
181+
let mode = if lock { "remount,ro" } else { "remount,rw" };
182+
183+
let status = Command::new("mount")
184+
.args(&["-o", mode, "/"])
185+
.stdout(Stdio::null())
186+
.stderr(Stdio::null())
187+
.status()?;
188+
189+
if status.success() {
190+
info!(
191+
"{} Remount as {} successful",
192+
NAME,
193+
if lock { "read-only" } else { "read-write" }
194+
);
195+
} else {
196+
error!(
197+
"{} Remount as {} failed: {:?}",
198+
NAME,
199+
if lock { "read-only" } else { "read-write" },
200+
status
201+
);
202+
}
203+
204+
Ok(())
205+
}
206+
173207
impl AppConfig {
174208
pub fn load(config_file: PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
175209
use ::config::File;
@@ -235,6 +269,13 @@ impl AppConfig {
235269
doc["ev_battery_capacity"] = value(self.ev_battery_capacity as i64);
236270
doc["ev_factor"] = value(self.ev_factor as f64);
237271

272+
let _ = remount_root(false);
273+
info!(
274+
"{} Saving new configuration to file: {}",
275+
NAME,
276+
config_file.display()
277+
);
238278
let _ = fs::write(config_file, doc.to_string());
279+
let _ = remount_root(true);
239280
}
240281
}

0 commit comments

Comments
 (0)