]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/mount.py
1 # Copyright 2021 Oliver Smith
2 # SPDX-License-Identifier: GPL-3.0-or-later
4 import tools
.helpers
.run
5 from tools
.helpers
.version
import versiontuple
, kernel_version
10 Ismount() implementation, that works for mount --bind.
11 Workaround for: https://bugs.python.org/issue29707
13 folder
= os
.path
.realpath(os
.path
.realpath(folder
))
14 with open("/proc/mounts", "r") as handle
:
17 if len(words
) >= 2 and words
[1] == folder
:
19 if words
[0] == folder
:
24 def bind(args
, source
, destination
, create_folders
=True, umount
=False):
26 Mount --bind a folder and create necessary directory structure.
27 :param umount: when destination is already a mount point, umount it first.
29 # Check/umount destination
30 if ismount(destination
):
32 umount_all(args
, destination
)
36 # Check/create folders
37 for path
in [source
, destination
]:
38 if os
.path
.exists(path
):
41 tools
.helpers
.run
.user(args
, ["mkdir", "-p", path
])
43 raise RuntimeError("Mount failed, folder does not exist: " +
46 # Actually mount the folder
47 tools
.helpers
.run
.user(args
, ["mount", "-o", "bind", source
, destination
])
49 # Verify, that it has worked
50 if not ismount(destination
):
51 raise RuntimeError("Mount failed: " + source
+ " -> " + destination
)
54 def bind_file(args
, source
, destination
, create_folders
=False):
56 Mount a file with the --bind option, and create the destination file,
59 # Skip existing mountpoint
60 if ismount(destination
):
64 if not os
.path
.exists(destination
):
66 dir = os
.path
.dirname(destination
)
67 if not os
.path
.isdir(dir):
68 tools
.helpers
.run
.user(args
, ["mkdir", "-p", dir])
70 tools
.helpers
.run
.user(args
, ["touch", destination
])
73 tools
.helpers
.run
.user(args
, ["mount", "-o", "bind", source
,
77 def umount_all_list(prefix
, source
="/proc/mounts"):
79 Parses `/proc/mounts` for all folders beginning with a prefix.
80 :source: can be changed for testcases
81 :returns: a list of folders, that need to be umounted
84 prefix
= os
.path
.realpath(prefix
)
85 with open(source
, "r") as handle
:
89 raise RuntimeError("Failed to parse line in " + source
+ ": " +
92 if mountpoint
.startswith(prefix
):
93 # Remove "\040(deleted)" suffix (#545)
94 deleted_str
= r
"\040(deleted)"
95 if mountpoint
.endswith(deleted_str
):
96 mountpoint
= mountpoint
[:-len(deleted_str
)]
97 ret
.append(mountpoint
)
98 ret
.sort(reverse
=True)
102 def umount_all(args
, folder
):
104 Umount all folders, that are mounted inside a given folder.
106 all_list
= umount_all_list(folder
)
107 for mountpoint
in all_list
:
108 tools
.helpers
.run
.user(args
, ["umount", mountpoint
])
109 for mountpoint
in all_list
:
110 if ismount(mountpoint
):
111 raise RuntimeError("Failed to umount: " + mountpoint
)
113 def mount(args
, source
, destination
, create_folders
=True, umount
=False,
114 readonly
=True, mount_type
=None, options
=None, force
=True):
116 Mount and create necessary directory structure.
117 :param umount: when destination is already a mount point, umount it first.
118 :param force: attempt mounting even if the mount point already exists.
120 # Check/umount destination
121 if ismount(destination
):
123 umount_all(args
, destination
)
128 # Check/create folders
129 if not os
.path
.exists(destination
):
131 tools
.helpers
.run
.user(args
, ["mkdir", "-p", destination
])
133 raise RuntimeError("Mount failed, folder does not exist: " +
139 extra_args
.extend(["-t", mount_type
])
141 opt_args
.append("ro")
143 opt_args
.extend(options
)
145 extra_args
.extend(["-o", ",".join(opt_args
)])
147 # Actually mount the folder
148 tools
.helpers
.run
.user(args
, ["mount", *extra_args
, source
, destination
])
150 # Verify, that it has worked
151 if not ismount(destination
):
152 raise RuntimeError("Mount failed: " + source
+ " -> " + destination
)
154 def mount_overlay(args
, lower_dirs
, destination
, upper_dir
=None, work_dir
=None,
155 create_folders
=True, readonly
=True):
160 options
= ["lowerdir=" + (":".join(lower_dirs
))]
163 dirs
.append(upper_dir
)
164 dirs
.append(work_dir
)
165 options
.append("upperdir=" + upper_dir
)
166 options
.append("workdir=" + work_dir
)
168 if kernel_version() >= versiontuple("4.17"):
169 options
.append("xino=off")
171 for dir_path
in dirs
:
172 if not os
.path
.exists(dir_path
):
174 tools
.helpers
.run
.user(args
, ["mkdir", "-p", dir_path
])
176 raise RuntimeError("Mount failed, folder does not exist: " +
179 mount(args
, "overlay", destination
, mount_type
="overlay", options
=options
,
180 readonly
=readonly
, create_folders
=create_folders
, force
=True)