def _remove_thin_device(name): """ Destroys a thin device via subprocess call. """ r = util.subp(['dmsetup', 'remove', '--retry', name]) if r.return_code != 0: raise MountError('Could not remove thin device:\n' + r.stderr)
def _mount_overlay(self, identifier, options): """ OverlayFS mount backend. """ if os.geteuid() != 0: raise MountError('Insufficient privileges to mount device.') if self.live: raise MountError('The OverlayFS backend does not support live ' 'mounts.') elif 'rw' in options: raise MountError('The OverlayFS backend does not support ' 'writeable mounts.') cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) ld, ud, wd = '', '', '' try: ld = cinfo['GraphDriver']['Data']['lowerDir'] ud = cinfo['GraphDriver']['Data']['upperDir'] wd = cinfo['GraphDriver']['Data']['workDir'] except: ld, ud, wd = DockerMount._no_gd_api_overlay(cid) options += ['ro', 'lowerdir=' + ld, 'upperdir=' + ud, 'workdir=' + wd] optstring = ','.join(options) cmd = ['mount', '-t', 'overlay', '-o', optstring, 'overlay', self.mountpoint] status = util.subp(cmd) if status.return_code != 0: self._cleanup_container(cinfo) raise MountError('Failed to mount OverlayFS device.\n' + status.stderr.decode(sys.getdefaultencoding()))
def unmount_path(path): """ Unmounts the directory specified by path. """ r = util.subp(['umount', path]) if r.return_code != 0: raise ValueError(r.stderr)
def _get_fs(thin_pathname): """ Returns the file system type (xfs, ext4) of a given device """ cmd = ['lsblk', '-o', 'FSTYPE', '-n', thin_pathname] fs_return = util.subp(cmd) return fs_return.stdout.strip()
def get_dev_at_mountpoint(mntpoint): """ Retrieves the device mounted at mntpoint, or raises MountError if none. """ results = util.subp(['findmnt', '-o', 'SOURCE', mntpoint]) if results.return_code != 0: raise MountError('No device mounted at ' + mntpoint) return results.stdout.replace('SOURCE\n', '').strip().split('\n')[-1]
def _activate_thin_device(name, dm_id, size, pool): """ Provisions an LVM device-mapper thin device reflecting, DM device id 'dm_id' in the docker pool. """ table = '0 {0} thin /dev/mapper/{1} {2}'.format(int(size) / 512, pool, dm_id) cmd = ['dmsetup', 'create', name, '--table', table] r = util.subp(cmd) if r.return_code != 0: raise MountError('Failed to create thin device: ' + r.stderr)
def _is_device_active(device): """ Checks dmsetup to see if a device is already active """ cmd = ['dmsetup', 'info', device] dmsetup_info = util.subp(cmd) for dm_line in dmsetup_info.stdout.split("\n"): line = dm_line.split(':') if ('State' in line[0].strip()) and ('ACTIVE' in line[1].strip()): return True return False
def _activate_thin_device(name, dm_id, size, pool): """ Provisions an LVM device-mapper thin device reflecting, DM device id 'dm_id' in the docker pool. """ table = '0 {0} thin /dev/mapper/{1} {2}'.format( int(size) / 512, pool, dm_id) cmd = ['dmsetup', 'create', name, '--table', table] r = util.subp(cmd) if r.return_code != 0: raise MountError('Failed to create thin device: ' + r.stderr)
def mount_path(source, target, optstring='', bind=False): """ Subprocess call to mount dev at path. """ cmd = ['mount'] if bind: cmd.append('--bind') if optstring: cmd.append('-o') cmd.append(optstring) cmd.append(source) cmd.append(target) r = util.subp(cmd) if r.return_code != 0: raise MountError('Could not mount docker container:\n' + ' '.join(cmd) + '\n' + r.stderr)
def _get_overlay_mount_cid(self): """ Returns the cid of the container mounted at mountpoint. """ cmd = ['findmnt', '-o', 'OPTIONS', '-n', self.mountpoint] r = util.subp(cmd) if r.return_code != 0: raise MountError('No devices mounted at that location.') optstring = r.stdout.strip().split('\n')[-1] upperdir = [o.replace('upperdir=', '') for o in optstring.split(',') if o.startswith('upperdir=')][0] cdir = upperdir.rsplit('/', 1)[0] if not cdir.startswith('/var/lib/docker/overlay/'): raise MountError('The device mounted at that location is not a ' 'docker container.') return cdir.replace('/var/lib/docker/overlay/', '')
def _get_overlay_mount_cid(self): """ Returns the cid of the container mounted at mountpoint. """ cmd = ['findmnt', '-o', 'OPTIONS', '-n', self.mountpoint] r = util.subp(cmd) if r.return_code != 0: raise MountError('No devices mounted at that location.') optstring = r.stdout.strip().split('\n')[-1] upperdir = [ o.replace('upperdir=', '') for o in optstring.split(',') if o.startswith('upperdir=') ][0] cdir = upperdir.rsplit('/', 1)[0] if not cdir.startswith('/var/lib/docker/overlay/'): raise MountError('The device mounted at that location is not a ' 'docker container.') return cdir.replace('/var/lib/docker/overlay/', '')
def _mount_overlay(self, identifier, options): """ OverlayFS mount backend. """ if os.geteuid() != 0: raise MountError('Insufficient privileges to mount device.') if self.live: raise MountError('The OverlayFS backend does not support live ' 'mounts.') elif 'rw' in options: raise MountError('The OverlayFS backend does not support ' 'writeable mounts.') cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) ld, ud, wd = '', '', '' try: ld = cinfo['GraphDriver']['Data']['lowerDir'] ud = cinfo['GraphDriver']['Data']['upperDir'] wd = cinfo['GraphDriver']['Data']['workDir'] except: ld, ud, wd = DockerMount._no_gd_api_overlay(cid) options += ['ro', 'lowerdir=' + ld, 'upperdir=' + ud, 'workdir=' + wd] optstring = ','.join(options) cmd = [ 'mount', '-t', 'overlay', '-o', optstring, 'overlay', self.mountpoint ] status = util.subp(cmd) if status.return_code != 0: self._cleanup_container(cinfo) raise MountError('Failed to mount OverlayFS device.\n' + status.stderr.decode(sys.getdefaultencoding()))