/usr/lib/python3/dist-packages/mockbuild/mounts.py is in mock 1.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | # -*- coding: utf-8 -*-
# vim: noai:ts=4:sw=4:expandtab
import grp
import os
import os.path
from . import exception
from . import util
from .trace_decorator import traceLog
class MountPoint(object):
'''base class for mounts'''
@traceLog()
def __init__(self, mountsource, mountpath):
self.mountpath = mountpath
self.mountsource = mountsource
@traceLog()
def ismounted(self):
with open('/proc/mounts') as f:
if self.mountpath in [x.split()[1] for x in f]:
return True
return False
class FileSystemMountPoint(MountPoint):
'''class for managing filesystem mounts in the chroot'''
@traceLog()
def __init__(self, path, filetype=None, device=None, options=None):
if not path:
raise RuntimeError("no path specified for mountpoint")
if not filetype:
raise RuntimeError("no filetype specified for mountpoint")
if filetype in ('pts', 'proc', 'sys', 'sysfs', 'tmpfs', 'devpts'):
device = filetype
if not device:
raise RuntimeError("no device file specified for mountpoint")
MountPoint.__init__(self, mountsource=device, mountpath=path)
self.device = device
self.path = path
self.filetype = filetype
self.options = options
self.mounted = self.ismounted()
@traceLog()
def mount(self):
if self.mounted:
return
cmd = ['/bin/mount', '-n', '-t', self.filetype]
if self.options:
cmd += ['-o', self.options]
cmd += [self.device, self.path]
util.do(cmd)
self.mounted = True
return True
@traceLog()
# pylint: disable=unused-argument
def umount(self, force=False, nowarn=False):
if not self.mounted:
return
cmd = ['/bin/umount', '-n', '-l', self.path]
try:
util.do(cmd)
except exception.Error:
return False
self.mounted = False
return True
class BindMountPoint(MountPoint):
'''class for managing bind-mounts in the chroot'''
@traceLog()
def __init__(self, srcpath, bindpath):
MountPoint.__init__(self, mountsource=srcpath, mountpath=bindpath)
self.srcpath = srcpath
self.bindpath = bindpath
self.mounted = self.ismounted()
@traceLog()
def mount(self):
if not self.mounted:
cmd = ['/bin/mount', '-n', '--bind', self.srcpath, self.bindpath]
util.do(cmd)
self.mounted = True
return True
@traceLog()
def umount(self):
if self.mounted:
cmd = ['/bin/umount', '-n', self.bindpath]
try:
util.do(cmd)
except exception.Error:
return False
self.mounted = False
return True
class Mounts(object):
'''class to manage all mountpoints'''
@traceLog()
def __init__(self, rootObj):
self.rootObj = rootObj
self.mounts = []
if not util.USE_NSPAWN:
self.mounts = [
FileSystemMountPoint(filetype='proc',
device='mock_chroot_proc',
path=rootObj.make_chroot_path('/proc')),
FileSystemMountPoint(filetype='sysfs',
device='mock_chroot_sys',
path=rootObj.make_chroot_path('/sys')),
]
if rootObj.config['internal_dev_setup']:
self.mounts.append(FileSystemMountPoint(filetype='tmpfs',
device='mock_chroot_shmfs',
path=rootObj.make_chroot_path('/dev/shm')))
opts = 'gid=%d,mode=0620,ptmxmode=0666' % grp.getgrnam('tty').gr_gid
if util.cmpKernelVer(os.uname()[2], '2.6.29') >= 0:
opts += ',newinstance'
self.mounts.append(FileSystemMountPoint(filetype='devpts',
device='mock_chroot_devpts',
path=rootObj.make_chroot_path('/dev/pts'), options=opts))
@traceLog()
def add(self, mount):
self.mounts.append(mount)
@traceLog()
def mountall(self):
for m in self.mounts:
m.mount()
@traceLog()
# pylint: disable=unused-argument
def umountall(self, force=False, nowarn=False):
for m in reversed(self.mounts):
m.umount()
@traceLog()
def get_mountpoints(self):
return [m.mountpath for m in self.mounts]
|