public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v2 proxmox-backup 01/15] tools: add disks utility functions
Date: Mon, 30 Aug 2021 13:14:51 +0200	[thread overview]
Message-ID: <20210830111505.38694-2-h.laimer@proxmox.com> (raw)
In-Reply-To: <20210830111505.38694-1-h.laimer@proxmox.com>

adds:
 - get_fs_uuid_by_disk_path: taken out of the already existing
    get_fs_uuid function
 - get_mount_point_by_uuid: returns the mount-point of a disk by a given
    uuid, fails if the disk is not mounted
 - get_fs_uuid_by_path: finds the uuid of the disk that the given path
    is on, can fail if there is no uuid for the disk
 - mount_by_uuid: mounts a disk identified by uuid to a given path
 - unmount_by_mount_point: basically linux 'umount -l', justification
    for the lazy option in code
---
 src/tools/disks/mod.rs | 53 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/src/tools/disks/mod.rs b/src/tools/disks/mod.rs
index af7f7fe1..ec4d8742 100644
--- a/src/tools/disks/mod.rs
+++ b/src/tools/disks/mod.rs
@@ -1009,6 +1009,22 @@ pub fn get_fs_uuid(disk: &Disk) -> Result<String, Error> {
         None => bail!("disk {:?} has no node in /dev", disk.syspath()),
     };
 
+    get_fs_uuid_by_disk_path(&disk_path)
+}
+
+/// Get the FS UUID of the disk that the given path is on.
+pub fn get_fs_uuid_by_path(path: &Path) -> Result<String, Error> {
+    let mut command = std::process::Command::new("df");
+    command.args(&["--output=source"]);
+    command.arg(path);
+
+    let output = crate::tools::run_command(command, None)?;
+
+    get_fs_uuid_by_disk_path(Path::new(output.lines().skip(1).next().unwrap()))
+}
+
+/// Get the FS UUID of a disk with a given disk path.
+pub fn get_fs_uuid_by_disk_path(disk_path: &Path) -> Result<String, Error> {
     let mut command = std::process::Command::new("blkid");
     command.args(&["-o", "export"]);
     command.arg(disk_path);
@@ -1023,3 +1039,40 @@ pub fn get_fs_uuid(disk: &Disk) -> Result<String, Error> {
 
     bail!("get_fs_uuid failed - missing UUID");
 }
+
+/// Get mount point by UUID
+pub fn get_mount_point_by_uuid(uuid: &str) -> Result<String, Error> {
+    let mut command = std::process::Command::new("findmnt");
+    command.args(&["-rn", "-oTARGET", "-S"]);
+    command.arg(&format!("UUID={}", uuid));
+
+    let output = crate::tools::run_command(command, None)?;
+
+    if !output.is_empty() {
+        return Ok(String::from(output.trim()));
+    }
+
+    bail!("get_mount_point_by_uuid failed - device with uuid: {} is not mounted", uuid);
+}
+
+/// Mount a disk by its UUID and the mount point.
+pub fn mount_by_uuid(uuid: &str, mount_point: &Path) -> Result<(), Error> {
+    let mut command = std::process::Command::new("mount");
+    command.args(&[&format!("UUID={}", uuid)]);
+    command.arg(mount_point);
+
+    crate::tools::run_command(command, None)?;
+    Ok(())
+}
+
+/// Unmount a disk by its mount point.
+pub fn unmount_by_mount_point(mount_point: &Path) -> Result<(), Error> {
+    let mut command = std::process::Command::new("umount");
+    // Datastores are only unmounted after a check for running jobs, '-l' needed due to some weird
+    // bug where some tokio-threads keep the .lock-file open (sometimes)
+    command.args(&["-l"]);
+    command.arg(mount_point);
+
+    crate::tools::run_command(command, None)?;
+    Ok(())
+}
-- 
2.30.2





  reply	other threads:[~2021-08-30 11:15 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-30 11:14 [pbs-devel] [PATCH v2 proxmox-backup 00/15] (partially)close #3156: Add support for removable datastores Hannes Laimer
2021-08-30 11:14 ` Hannes Laimer [this message]
2021-09-01 14:48   ` [pbs-devel] [PATCH v2 proxmox-backup 01/15] tools: add disks utility functions Dominik Csapak
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 02/15] config: add uuid+mountpoint to DataStoreConfig Hannes Laimer
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 03/15] api2: add support for removable datastore creation Hannes Laimer
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 04/15] backup: add check_if_available function to ds Hannes Laimer
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 05/15] api2: add 'is_available' to DataStoreConfig Hannes Laimer
2021-09-01 14:48   ` Dominik Csapak
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 06/15] api2: add 'removable' to DataStoreListItem Hannes Laimer
2021-09-01 14:48   ` Dominik Csapak
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 07/15] api2: add (un)mount endpoint for removable ds's Hannes Laimer
2021-09-01 14:48   ` Dominik Csapak
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 08/15] pbs: add mount-removable command to commandSocket Hannes Laimer
2021-08-30 11:14 ` [pbs-devel] [PATCH v2 proxmox-backup 09/15] pbs-manager: add 'send-command' command Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 10/15] debian: add udev rule for removable datastores Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 11/15] ui: show usb icon for removable datastore in list Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 12/15] ui: add 'removable' checkbox in datastore creation Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 13/15] ui: display row as disabled in ds statistics Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 14/15] ui: show backing device UUID and mount-point in option tab Hannes Laimer
2021-08-30 11:15 ` [pbs-devel] [PATCH v2 proxmox-backup 15/15] ui: add (un)mount button to summary Hannes Laimer
2021-09-01 14:48   ` Dominik Csapak
2021-09-01 14:48 ` [pbs-devel] [PATCH v2 proxmox-backup 00/15] (partially)close #3156: Add support for removable datastores Dominik Csapak
2021-09-02  6:09   ` Thomas Lamprecht
2021-09-02  6:18     ` Dominik Csapak
2021-09-02  6:28       ` Thomas Lamprecht
2021-09-03  9:27   ` Hannes Laimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210830111505.38694-2-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal