]> glassweightruler.freedombox.rocks Git - waydroid.git/blobdiff - tools/helpers/mount.py
helpers/mount: Add an option to mount even if mount point already exists
[waydroid.git] / tools / helpers / mount.py
index 4d772067fa76bcd152c6ec7053ef52ac8fecea0c..2660952696a156b2b0e266be58feb6098c12baf5 100644 (file)
@@ -37,13 +37,13 @@ def bind(args, source, destination, create_folders=True, umount=False):
         if os.path.exists(path):
             continue
         if create_folders:
         if os.path.exists(path):
             continue
         if create_folders:
-            tools.helpers.run.root(args, ["mkdir", "-p", path])
+            tools.helpers.run.user(args, ["mkdir", "-p", path])
         else:
             raise RuntimeError("Mount failed, folder does not exist: " +
                                path)
 
     # Actually mount the folder
         else:
             raise RuntimeError("Mount failed, folder does not exist: " +
                                path)
 
     # Actually mount the folder
-    tools.helpers.run.root(args, ["mount", "-o", "bind", source, destination])
+    tools.helpers.run.user(args, ["mount", "-o", "bind", source, destination])
 
     # Verify, that it has worked
     if not ismount(destination):
 
     # Verify, that it has worked
     if not ismount(destination):
@@ -64,12 +64,12 @@ def bind_file(args, source, destination, create_folders=False):
         if create_folders:
             dir = os.path.dirname(destination)
             if not os.path.isdir(dir):
         if create_folders:
             dir = os.path.dirname(destination)
             if not os.path.isdir(dir):
-                tools.helpers.run.root(args, ["mkdir", "-p", dir])
+                tools.helpers.run.user(args, ["mkdir", "-p", dir])
 
 
-        tools.helpers.run.root(args, ["touch", destination])
+        tools.helpers.run.user(args, ["touch", destination])
 
     # Mount
 
     # Mount
-    tools.helpers.run.root(args, ["mount", "-o", "bind", source,
+    tools.helpers.run.user(args, ["mount", "-o", "bind", source,
                                 destination])
 
 
                                 destination])
 
 
@@ -102,35 +102,49 @@ def umount_all(args, folder):
     """
     Umount all folders, that are mounted inside a given folder.
     """
     """
     Umount all folders, that are mounted inside a given folder.
     """
-    for mountpoint in umount_all_list(folder):
-        tools.helpers.run.root(args, ["umount", mountpoint])
+    all_list = umount_all_list(folder)
+    for mountpoint in all_list:
+        tools.helpers.run.user(args, ["umount", mountpoint])
+    for mountpoint in all_list:
         if ismount(mountpoint):
             raise RuntimeError("Failed to umount: " + mountpoint)
 
         if ismount(mountpoint):
             raise RuntimeError("Failed to umount: " + mountpoint)
 
-def mount(args, source, destination, create_folders=True, umount=False, readonly=True):
+def mount(args, source, destination, create_folders=True, umount=False,
+          readonly=True, mount_type=None, options=None, force=True):
     """
     Mount and create necessary directory structure.
     :param umount: when destination is already a mount point, umount it first.
     """
     Mount and create necessary directory structure.
     :param umount: when destination is already a mount point, umount it first.
+    :param force: attempt mounting even if the mount point already exists.
     """
     # Check/umount destination
     if ismount(destination):
         if umount:
             umount_all(args, destination)
         else:
     """
     # Check/umount destination
     if ismount(destination):
         if umount:
             umount_all(args, destination)
         else:
-            return
+            if not force:
+                return
 
     # Check/create folders
     if not os.path.exists(destination):
         if create_folders:
 
     # Check/create folders
     if not os.path.exists(destination):
         if create_folders:
-            tools.helpers.run.root(args, ["mkdir", "-p", destination])
+            tools.helpers.run.user(args, ["mkdir", "-p", destination])
         else:
             raise RuntimeError("Mount failed, folder does not exist: " +
                             destination)
 
         else:
             raise RuntimeError("Mount failed, folder does not exist: " +
                             destination)
 
-    # Actually mount the folder
-    tools.helpers.run.root(args, ["mount", source, destination])
+    extra_args = []
+    opt_args = []
+    if mount_type:
+        extra_args.extend(["-t", mount_type])
     if readonly:
     if readonly:
-        tools.helpers.run.root(args, ["mount", "-o", "remount,ro", source, destination])
+        opt_args.append("ro")
+    if options:
+        opt_args.extend(options)
+    if opt_args:
+        extra_args.extend(["-o", ",".join(opt_args)])
+
+    # Actually mount the folder
+    tools.helpers.run.user(args, ["mount", *extra_args, source, destination])
 
     # Verify, that it has worked
     if not ismount(destination):
 
     # Verify, that it has worked
     if not ismount(destination):