2022-01-02 21:38:21 +00:00
|
|
|
# Copyright 2022 Oliver Smith
|
2020-02-20 20:07:28 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2017-05-26 20:08:45 +00:00
|
|
|
import os
|
|
|
|
import pmb.helpers.run
|
|
|
|
|
|
|
|
|
|
|
|
def ismount(folder):
|
|
|
|
"""
|
2021-09-26 15:50:11 +00:00
|
|
|
Ismount() implementation that works for mount --bind.
|
2017-05-26 20:08:45 +00:00
|
|
|
Workaround for: https://bugs.python.org/issue29707
|
|
|
|
"""
|
2017-08-15 14:08:48 +00:00
|
|
|
folder = os.path.realpath(os.path.realpath(folder))
|
2017-05-26 20:08:45 +00:00
|
|
|
with open("/proc/mounts", "r") as handle:
|
|
|
|
for line in handle:
|
|
|
|
words = line.split()
|
|
|
|
if len(words) >= 2 and words[1] == folder:
|
|
|
|
return True
|
2017-06-04 00:57:37 +00:00
|
|
|
if words[0] == folder:
|
|
|
|
return True
|
2017-05-26 20:08:45 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-02-19 22:04:01 +00:00
|
|
|
def bind(args, source, destination, create_folders=True, umount=False):
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
|
|
|
Mount --bind a folder and create necessary directory structure.
|
2018-02-19 22:04:01 +00:00
|
|
|
:param umount: when destination is already a mount point, umount it first.
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2018-02-19 22:04:01 +00:00
|
|
|
# Check/umount destination
|
2017-05-26 20:08:45 +00:00
|
|
|
if ismount(destination):
|
2018-02-19 22:04:01 +00:00
|
|
|
if umount:
|
|
|
|
umount_all(args, destination)
|
|
|
|
else:
|
|
|
|
return
|
2017-05-26 20:08:45 +00:00
|
|
|
|
|
|
|
# Check/create folders
|
|
|
|
for path in [source, destination]:
|
|
|
|
if os.path.exists(path):
|
|
|
|
continue
|
|
|
|
if create_folders:
|
|
|
|
pmb.helpers.run.root(args, ["mkdir", "-p", path])
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Mount failed, folder does not exist: " +
|
|
|
|
path)
|
|
|
|
|
|
|
|
# Actually mount the folder
|
|
|
|
pmb.helpers.run.root(args, ["mount", "--bind", source, destination])
|
|
|
|
|
2021-09-26 15:50:11 +00:00
|
|
|
# Verify that it has worked
|
2017-05-26 20:08:45 +00:00
|
|
|
if not ismount(destination):
|
|
|
|
raise RuntimeError("Mount failed: " + source + " -> " + destination)
|
|
|
|
|
|
|
|
|
2020-01-20 12:31:13 +00:00
|
|
|
def bind_file(args, source, destination, create_folders=False):
|
2017-07-27 18:14:02 +00:00
|
|
|
"""
|
2020-01-20 12:27:40 +00:00
|
|
|
Mount a file with the --bind option, and create the destination file,
|
|
|
|
if necessary.
|
2017-07-27 18:14:02 +00:00
|
|
|
"""
|
2017-05-26 20:08:45 +00:00
|
|
|
# Skip existing mountpoint
|
|
|
|
if ismount(destination):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Create empty file
|
|
|
|
if not os.path.exists(destination):
|
2020-01-20 12:31:13 +00:00
|
|
|
if create_folders:
|
|
|
|
dir = os.path.dirname(destination)
|
|
|
|
if not os.path.isdir(dir):
|
|
|
|
pmb.helpers.run.root(args, ["mkdir", "-p", dir])
|
|
|
|
|
2017-05-26 20:08:45 +00:00
|
|
|
pmb.helpers.run.root(args, ["touch", destination])
|
|
|
|
|
|
|
|
# Mount
|
|
|
|
pmb.helpers.run.root(args, ["mount", "--bind", source,
|
|
|
|
destination])
|
|
|
|
|
|
|
|
|
2017-07-27 18:14:02 +00:00
|
|
|
def umount_all_list(prefix, source="/proc/mounts"):
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2017-07-27 18:14:02 +00:00
|
|
|
Parses `/proc/mounts` for all folders beginning with a prefix.
|
|
|
|
:source: can be changed for testcases
|
2021-09-26 15:50:11 +00:00
|
|
|
:returns: a list of folders that need to be umounted
|
2017-05-26 20:08:45 +00:00
|
|
|
"""
|
2017-07-27 18:14:02 +00:00
|
|
|
ret = []
|
2017-08-15 14:08:48 +00:00
|
|
|
prefix = os.path.realpath(prefix)
|
2017-07-27 18:14:02 +00:00
|
|
|
with open(source, "r") as handle:
|
2017-05-26 20:08:45 +00:00
|
|
|
for line in handle:
|
|
|
|
words = line.split()
|
2017-07-27 18:14:02 +00:00
|
|
|
if len(words) < 2:
|
|
|
|
raise RuntimeError("Failed to parse line in " + source + ": " +
|
|
|
|
line)
|
2017-12-11 19:56:46 +00:00
|
|
|
mountpoint = words[1]
|
|
|
|
if mountpoint.startswith(prefix):
|
|
|
|
# Remove "\040(deleted)" suffix (#545)
|
|
|
|
deleted_str = r"\040(deleted)"
|
|
|
|
if mountpoint.endswith(deleted_str):
|
|
|
|
mountpoint = mountpoint[:-len(deleted_str)]
|
|
|
|
ret.append(mountpoint)
|
2017-07-27 18:14:02 +00:00
|
|
|
ret.sort(reverse=True)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def umount_all(args, folder):
|
|
|
|
"""
|
2021-09-26 15:50:11 +00:00
|
|
|
Umount all folders that are mounted inside a given folder.
|
2017-07-27 18:14:02 +00:00
|
|
|
"""
|
|
|
|
for mountpoint in umount_all_list(folder):
|
|
|
|
pmb.helpers.run.root(args, ["umount", mountpoint])
|
|
|
|
if ismount(mountpoint):
|
|
|
|
raise RuntimeError("Failed to umount: " + mountpoint)
|