mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-31 04:23:40 +00:00
Feature/backup fs (#2665)
* port 040 config, WIP * update fixtures * use taiga modal for backups too * fix: update Taiga UI and refactor everything to work * chore: package-lock * fix interfaces and mocks for interfaces * better mocks * function to transform old spec to new * delete unused fns * delete unused FE config utils * fix exports from sdk * reorganize exports * functions to translate config * rename unionSelectKey and unionValueKey * new backup fs * update sdk types * change types, include fuse module * fix casing * rework setup wiz * rework UI * only fuse3 * fix arm build * misc fixes * fix duplicate server select * fix: fix throwing inside dialog --------- Co-authored-by: Matt Hill <mattnine@protonmail.com> Co-authored-by: waterplea <alexander@inkin.ru> Co-authored-by: Matt Hill <MattDHill@users.noreply.github.com>
This commit is contained in:
@@ -11,9 +11,10 @@ use super::filesystem::ecryptfs::EcryptFS;
|
||||
use super::guard::{GenericMountGuard, TmpMountGuard};
|
||||
use crate::auth::check_password;
|
||||
use crate::backup::target::BackupInfo;
|
||||
use crate::disk::mount::filesystem::backupfs::BackupFS;
|
||||
use crate::disk::mount::filesystem::ReadWrite;
|
||||
use crate::disk::mount::guard::SubPath;
|
||||
use crate::disk::util::EmbassyOsRecoveryInfo;
|
||||
use crate::disk::util::StartOsRecoveryInfo;
|
||||
use crate::util::crypto::{decrypt_slice, encrypt_slice};
|
||||
use crate::util::serde::IoFormat;
|
||||
use crate::{Error, ErrorKind, ResultExt};
|
||||
@@ -23,29 +24,27 @@ pub struct BackupMountGuard<G: GenericMountGuard> {
|
||||
backup_disk_mount_guard: Option<G>,
|
||||
encrypted_guard: Option<TmpMountGuard>,
|
||||
enc_key: String,
|
||||
pub unencrypted_metadata: EmbassyOsRecoveryInfo,
|
||||
unencrypted_metadata_path: PathBuf,
|
||||
pub unencrypted_metadata: StartOsRecoveryInfo,
|
||||
pub metadata: BackupInfo,
|
||||
}
|
||||
impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
fn backup_disk_path(&self) -> &Path {
|
||||
if let Some(guard) = &self.backup_disk_mount_guard {
|
||||
guard.path()
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn mount(backup_disk_mount_guard: G, password: &str) -> Result<Self, Error> {
|
||||
pub async fn mount(
|
||||
backup_disk_mount_guard: G,
|
||||
server_id: &str,
|
||||
password: &str,
|
||||
) -> Result<Self, Error> {
|
||||
let backup_disk_path = backup_disk_mount_guard.path();
|
||||
let unencrypted_metadata_path =
|
||||
backup_disk_path.join("EmbassyBackups/unencrypted-metadata.cbor");
|
||||
let mut unencrypted_metadata: EmbassyOsRecoveryInfo =
|
||||
let backup_dir = backup_disk_path.join("StartOSBackups").join(server_id);
|
||||
let unencrypted_metadata_path = backup_dir.join("unencrypted-metadata.json");
|
||||
let crypt_path = backup_dir.join("crypt");
|
||||
let mut unencrypted_metadata: StartOsRecoveryInfo =
|
||||
if tokio::fs::metadata(&unencrypted_metadata_path)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
IoFormat::Cbor.from_slice(
|
||||
IoFormat::Json.from_slice(
|
||||
&tokio::fs::read(&unencrypted_metadata_path)
|
||||
.await
|
||||
.with_ctx(|_| {
|
||||
@@ -56,6 +55,9 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
})?,
|
||||
)?
|
||||
} else {
|
||||
if tokio::fs::metadata(&crypt_path).await.is_ok() {
|
||||
tokio::fs::remove_dir_all(&crypt_path).await?;
|
||||
}
|
||||
Default::default()
|
||||
};
|
||||
let enc_key = if let (Some(hash), Some(wrapped_key)) = (
|
||||
@@ -96,7 +98,6 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
));
|
||||
}
|
||||
|
||||
let crypt_path = backup_disk_path.join("EmbassyBackups/crypt");
|
||||
if tokio::fs::metadata(&crypt_path).await.is_err() {
|
||||
tokio::fs::create_dir_all(&crypt_path).await.with_ctx(|_| {
|
||||
(
|
||||
@@ -106,11 +107,11 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
})?;
|
||||
}
|
||||
let encrypted_guard =
|
||||
TmpMountGuard::mount(&EcryptFS::new(&crypt_path, &enc_key), ReadWrite).await?;
|
||||
TmpMountGuard::mount(&BackupFS::new(&crypt_path, &enc_key), ReadWrite).await?;
|
||||
|
||||
let metadata_path = encrypted_guard.path().join("metadata.cbor");
|
||||
let metadata_path = encrypted_guard.path().join("metadata.json");
|
||||
let metadata: BackupInfo = if tokio::fs::metadata(&metadata_path).await.is_ok() {
|
||||
IoFormat::Cbor.from_slice(&tokio::fs::read(&metadata_path).await.with_ctx(|_| {
|
||||
IoFormat::Json.from_slice(&tokio::fs::read(&metadata_path).await.with_ctx(|_| {
|
||||
(
|
||||
crate::ErrorKind::Filesystem,
|
||||
metadata_path.display().to_string(),
|
||||
@@ -124,6 +125,7 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
backup_disk_mount_guard: Some(backup_disk_mount_guard),
|
||||
encrypted_guard: Some(encrypted_guard),
|
||||
enc_key,
|
||||
unencrypted_metadata_path,
|
||||
unencrypted_metadata,
|
||||
metadata,
|
||||
})
|
||||
@@ -152,20 +154,17 @@ impl<G: GenericMountGuard> BackupMountGuard<G> {
|
||||
|
||||
#[instrument(skip_all)]
|
||||
pub async fn save(&self) -> Result<(), Error> {
|
||||
let metadata_path = self.path().join("metadata.cbor");
|
||||
let backup_disk_path = self.backup_disk_path();
|
||||
let metadata_path = self.path().join("metadata.json");
|
||||
let mut file = AtomicFile::new(&metadata_path, None::<PathBuf>)
|
||||
.await
|
||||
.with_kind(ErrorKind::Filesystem)?;
|
||||
file.write_all(&IoFormat::Cbor.to_vec(&self.metadata)?)
|
||||
file.write_all(&IoFormat::Json.to_vec(&self.metadata)?)
|
||||
.await?;
|
||||
file.save().await.with_kind(ErrorKind::Filesystem)?;
|
||||
let unencrypted_metadata_path =
|
||||
backup_disk_path.join("EmbassyBackups/unencrypted-metadata.cbor");
|
||||
let mut file = AtomicFile::new(&unencrypted_metadata_path, None::<PathBuf>)
|
||||
let mut file = AtomicFile::new(&self.unencrypted_metadata_path, None::<PathBuf>)
|
||||
.await
|
||||
.with_kind(ErrorKind::Filesystem)?;
|
||||
file.write_all(&IoFormat::Cbor.to_vec(&self.unencrypted_metadata)?)
|
||||
file.write_all(&IoFormat::Json.to_vec(&self.unencrypted_metadata)?)
|
||||
.await?;
|
||||
file.save().await.with_kind(ErrorKind::Filesystem)?;
|
||||
Ok(())
|
||||
|
||||
55
core/startos/src/disk/mount/filesystem/backupfs.rs
Normal file
55
core/startos/src/disk/mount/filesystem/backupfs.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
use std::fmt::{self, Display};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::path::Path;
|
||||
|
||||
use digest::generic_array::GenericArray;
|
||||
use digest::{Digest, OutputSizeUser};
|
||||
use sha2::Sha256;
|
||||
|
||||
use super::FileSystem;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct BackupFS<DataDir: AsRef<Path>, Password: fmt::Display> {
|
||||
data_dir: DataDir,
|
||||
password: Password,
|
||||
}
|
||||
impl<DataDir: AsRef<Path>, Password: fmt::Display> BackupFS<DataDir, Password> {
|
||||
pub fn new(data_dir: DataDir, password: Password) -> Self {
|
||||
BackupFS { data_dir, password }
|
||||
}
|
||||
}
|
||||
impl<DataDir: AsRef<Path> + Send + Sync, Password: fmt::Display + Send + Sync> FileSystem
|
||||
for BackupFS<DataDir, Password>
|
||||
{
|
||||
fn mount_type(&self) -> Option<impl AsRef<str>> {
|
||||
Some("backup-fs")
|
||||
}
|
||||
fn mount_options(&self) -> impl IntoIterator<Item = impl Display> {
|
||||
[
|
||||
format!("password={}", self.password),
|
||||
format!("file-size-padding=0.05"),
|
||||
]
|
||||
}
|
||||
async fn source(&self) -> Result<Option<impl AsRef<Path>>, Error> {
|
||||
Ok(Some(&self.data_dir))
|
||||
}
|
||||
async fn source_hash(
|
||||
&self,
|
||||
) -> Result<GenericArray<u8, <Sha256 as OutputSizeUser>::OutputSize>, Error> {
|
||||
let mut sha = Sha256::new();
|
||||
sha.update("BackupFS");
|
||||
sha.update(
|
||||
tokio::fs::canonicalize(self.data_dir.as_ref())
|
||||
.await
|
||||
.with_ctx(|_| {
|
||||
(
|
||||
crate::ErrorKind::Filesystem,
|
||||
self.data_dir.as_ref().display().to_string(),
|
||||
)
|
||||
})?
|
||||
.as_os_str()
|
||||
.as_bytes(),
|
||||
);
|
||||
Ok(sha.finalize())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::ffi::OsStr;
|
||||
use std::fmt::{Display, Write};
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use digest::generic_array::GenericArray;
|
||||
use digest::OutputSizeUser;
|
||||
@@ -11,6 +12,7 @@ use tokio::process::Command;
|
||||
use crate::prelude::*;
|
||||
use crate::util::Invoke;
|
||||
|
||||
pub mod backupfs;
|
||||
pub mod bind;
|
||||
pub mod block_dev;
|
||||
pub mod cifs;
|
||||
@@ -71,6 +73,7 @@ pub(self) async fn default_mount_impl(
|
||||
fs.pre_mount().await?;
|
||||
tokio::fs::create_dir_all(mountpoint.as_ref()).await?;
|
||||
Command::from(default_mount_command(fs, mountpoint, mount_type).await?)
|
||||
.capture(false)
|
||||
.invoke(ErrorKind::Filesystem)
|
||||
.await?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user