def mount_devpts(newroot, target, **mnt_opts): """Mounts directory on devpts. """ while target.startswith('/'): target = target[1:] mount.mount(source='devpts', target=os.path.join(newroot, target), fs_type='devpts', mnt_flags=(mount.MS_NOSUID, mount.MS_NOEXEC), **mnt_opts) os.chmod(os.path.join(newroot, target, 'ptmx'), 0o666)
def mount_bind(newroot, target, source=None, recursive=True, read_only=True): """Bind mounts `source` to `newroot/target` so that `source` is accessed when reaching `newroot/target`. If a directory, the source will be mounted using --rbind. """ # Ensure root directory exists if not os.path.exists(newroot): raise exc.ContainerSetupError('Path %r does not exist' % newroot) if source is None: source = target target = fs.norm_safe(target) source = fs.norm_safe(source) # Make sure target directory exists. if not os.path.exists(source): raise exc.ContainerSetupError('Source path %r does not exist' % source) mnt_flags = [mount.MS_BIND] # Use --rbind for directories and --bind for files. if recursive and os.path.isdir(source): mnt_flags.append(mount.MS_REC) # Strip leading /, ensure that mount is relative path. while target.startswith('/'): target = target[1:] # Create mount directory, make sure it does not exists. target_fp = os.path.join(newroot, target) if os.path.isdir(source): fs.mkdir_safe(target_fp) else: fs.mkfile_safe(target_fp) res = mount.mount(source=source, target=target_fp, fs_type=None, mnt_flags=mnt_flags) if res == 0 and read_only: res = mount.mount(source=None, target=target_fp, fs_type=None, mnt_flags=(mount.MS_BIND, mount.MS_RDONLY, mount.MS_REMOUNT)) return res
def mount_move(target, source): """Move a mount from one to a point to another. """ return mount.mount(source=source, target=target, fs_type=None, mnt_flags=[mount.MS_MOVE])
def mount_tmpfs(newroot, target, nodev=True, noexec=True, nosuid=True, relatime=True, **mnt_opts): """Mounts directory on tmpfs. """ while target.startswith('/'): target = target[1:] mnt_flags = [ (nodev, mount.MS_NODEV), (noexec, mount.MS_NOEXEC), (nosuid, mount.MS_NOSUID), (relatime, mount.MS_RELATIME), ] return mount.mount( source='tmpfs', target=os.path.join(newroot, target), fs_type='tmpfs', mnt_flags=[mnt_flag for flag, mnt_flag in mnt_flags if flag], **mnt_opts)
def mount_proc(newroot, target='proc'): """Mounts /proc directory on newroot. """ while target.startswith('/'): target = target[1:] return mount.mount(source='proc', target=os.path.join(newroot, target), fs_type='proc')
def mount_mqueue(newroot, target, **mnt_opts): """Mounts directory on mqueue. """ while target.startswith('/'): target = target[1:] return mount.mount(source='mqueue', target=os.path.join(newroot, target), fs_type='mqueue', mnt_flags=(mount.MS_NOSUID, mount.MS_NODEV, mount.MS_NOEXEC), **mnt_opts)
def mount_filesystem(block_dev, target_dir, fs_type='ext4'): """Mount filesystem on target directory. :param block_dev: Block device to mount """ return mount.mount(source=block_dev, target=target_dir, fs_type=fs_type, mnt_flags=( mount.MS_NOATIME, mount.MS_NODIRATIME, ))
def mount_sysfs(newroot, target='sys', read_only=True): """Mounts /sysfs directory on newroot. """ while target.startswith('/'): target = target[1:] mnt_flags = [] if read_only: mnt_flags.append(mount.MS_RDONLY) return mount.mount(source='sysfs', target=os.path.join(newroot, target), fs_type='sysfs', mnt_flags=mnt_flags)
def mount_tmpfs(newroot, target, **mnt_opts): """Mounts directory on tmpfs. """ while target.startswith('/'): target = target[1:] mnt_flags = [ mount.MS_NODEV, mount.MS_NOEXEC, mount.MS_NOSUID, mount.MS_RELATIME, ] return mount.mount(source='tmpfs', target=os.path.join(newroot, target), fs_type='tmpfs', mnt_flags=mnt_flags, **mnt_opts)
def mount_tmpfs(newroot, target, size=None): """Mounts directory on tmpfs. """ while target.startswith('/'): target = target[1:] mnt_opts = {} if size is not None: mnt_opts['size'] = size return mount.mount(source='tmpfs', target=os.path.join(newroot, target), fs_type='tmpfs', mnt_flags=( mount.MS_NOATIME, mount.MS_NODIRATIME, ), mnt_opts=mnt_opts)
def mount_procfs(newroot, target='/proc'): """Mounts procfs on directory. """ while target.startswith('/'): target = target[1:] mnt_flags = [ mount.MS_NODEV, mount.MS_NOEXEC, mount.MS_NOSUID, mount.MS_RELATIME, ] return mount.mount( source='proc', target=os.path.join(newroot, target), fs_type='proc', mnt_flags=mnt_flags, )