def mount_share(share, mnt_pt): if (is_mounted(mnt_pt)): return mount_root(share.pool) pool_device = ('/dev/disk/by-id/%s' % share.pool.disk_set.first().name) subvol_str = 'subvol=%s' % share.subvol_name create_tmp_dir(mnt_pt) toggle_path_rw(mnt_pt, rw=False) mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def mount_share(share, mnt_pt): if (is_mounted(mnt_pt)): return mount_root(share.pool) pool_device = ('/dev/disk/by-id/%s' % share.pool.disk_set.first().name) subvol_str = 'subvol=%s' % share.subvol_name create_tmp_dir(mnt_pt) toggle_path_rw(mnt_pt, rw=False) mnt_cmd = [MOUNT, '-t', 'btrfs', '-o', subvol_str, pool_device, mnt_pt] return run_command(mnt_cmd)
def umount_root(root_pool_mnt): if (not os.path.exists(root_pool_mnt)): return try: o, e, rc = run_command([UMOUNT, '-l', root_pool_mnt]) except CommandException as ce: if (ce.rc == 32): for l in ce.err: l = l.strip() if (re.search('not mounted$', l) is not None): return raise ce for i in range(20): if (not is_mounted(root_pool_mnt)): toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return time.sleep(2) run_command([UMOUNT, '-f', root_pool_mnt]) toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return
def umount_root(root_pool_mnt): if (not os.path.exists(root_pool_mnt)): return try: o, e, rc = run_command([UMOUNT, '-l', root_pool_mnt]) except CommandException as ce: if (ce.rc == 32): for l in ce.err: l = l.strip() if (re.search('not mounted$', l) is not None): return raise ce for i in range(20): if (not is_mounted(root_pool_mnt)): toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return time.sleep(2) run_command([UMOUNT, '-f', root_pool_mnt]) toggle_path_rw(root_pool_mnt, rw=True) run_command([RMDIR, root_pool_mnt]) return
def mount_root(pool): """ Mounts a given pool at the default mount root (usually /mnt2/) using the pool.name as the final path entry. Ie pool.name = test-pool will be mounted at /mnt2/test-pool. Any mount options held in pool.mnt_options will be added to the mount command via the -o option as will a compress = pool.compression entry. N.B. Initially the mount target is defined by /dev/disk/by-label/pool.name, if this fails then an attempt to mount by each member of /dev/disk/by-id/pool.disk_set.all() but only if there are any members. If this second method also fails then an exception is raised, currently all but the last failed mount by device name is logged. If no disk members were reported by pool.disk_set.count() a separate Exception is raised. :param pool: pool object :return: either the relevant mount point or an Exception which either indicates 'no disks in pool' or 'Unknown Reason' """ root_pool_mnt = DEFAULT_MNT_DIR + pool.name if pool.is_mounted: return root_pool_mnt # Creates a directory to act as the mount point. create_tmp_dir(root_pool_mnt) toggle_path_rw(root_pool_mnt, rw=False) mnt_device = '/dev/disk/by-label/%s' % pool.name mnt_cmd = [ MOUNT, mnt_device, root_pool_mnt, ] mnt_options = '' if (pool.mnt_options is not None): mnt_options = pool.mnt_options if (pool.compression is not None): if (re.search('compress', mnt_options) is None): mnt_options = ('%s,compress=%s' % (mnt_options, pool.compression)) # Prior to a mount by label attempt we call btrfs device scan on all # members of our pool. This call ensures btrfs has up-to-date info on # the relevant devices and avoids the potential overkill of a system wide # call such as is performed in the rockstor-bootstrap service on boot. # Disk.target_name ensures we observe any redirect roles. device_scan([dev.target_name for dev in pool.disk_set.attached()]) if (os.path.exists(mnt_device)): if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) run_command(mnt_cmd) return root_pool_mnt # If we cannot mount by-label, let's try mounting by device; one by one # until we get our first success. All devices known to our pool object # have already been scanned prior to our mount by label attempt above. if (pool.disk_set.count() < 1): raise Exception('Cannot mount Pool(%s) as it has no disks in it.' % pool.name) last_device = pool.disk_set.attached().last() logger.info('Mount by label (%s) failed.' % mnt_device) for device in pool.disk_set.attached(): mnt_device = ('/dev/disk/by-id/%s' % device.target_name) logger.info('Attempting mount by device (%s).' % mnt_device) if (os.path.exists(mnt_device)): mnt_cmd = [ MOUNT, mnt_device, root_pool_mnt, ] if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) try: run_command(mnt_cmd) return root_pool_mnt except Exception as e: if (device.name == last_device.name): # exhausted mounting using all devices in the pool raise e logger.error('Error mounting: %s. ' 'Will try using another device.' % mnt_cmd) logger.exception(e) else: logger.error('Device (%s) was not found' % mnt_device) raise Exception('Failed to mount Pool(%s) due to an unknown reason. ' 'Command used %s' % (pool.name, mnt_cmd))
def mount_root(pool): """ Mounts a given pool at the default mount root (usually /mnt2/) using the pool.name as the final path entry. Ie pool.name = test-pool will be mounted at /mnt2/test-pool. Any mount options held in pool.mnt_options will be added to the mount command via the -o option as will a compress = pool.compression entry. N.B. Initially the mount target is defined by /dev/disk/by-label/pool.name, if this fails then an attempt to mount by each member of /dev/disk/by-id/pool.disk_set.all() but only if there are any members. If this second method also fails then an exception is raised, currently all but the last failed mount by device name is logged. If no disk members were reported by pool.disk_set.count() a separate Exception is raised. :param pool: pool object :return: either the relevant mount point or an Exception which either indicates 'no disks in pool' or 'Unknown Reason' """ root_pool_mnt = DEFAULT_MNT_DIR + pool.name if (is_share_mounted(pool.name)): return root_pool_mnt # Creates a directory to act as the mount point. create_tmp_dir(root_pool_mnt) toggle_path_rw(root_pool_mnt, rw=False) mnt_device = '/dev/disk/by-label/%s' % pool.name mnt_cmd = [ MOUNT, mnt_device, root_pool_mnt, ] mnt_options = '' if (pool.mnt_options is not None): mnt_options = pool.mnt_options if (pool.compression is not None): if (re.search('compress', mnt_options) is None): mnt_options = ('%s,compress=%s' % (mnt_options, pool.compression)) if (os.path.exists(mnt_device)): if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) run_command(mnt_cmd) return root_pool_mnt # If we cannot mount by-label, let's try mounting by device; one by one # until we get our first success. if (pool.disk_set.count() < 1): raise Exception('Cannot mount Pool(%s) as it has no disks in it.' % pool.name) last_device = pool.disk_set.last() for device in pool.disk_set.all(): mnt_device = ('/dev/disk/by-id/%s' % device.name) if (os.path.exists(mnt_device)): mnt_cmd = [ MOUNT, mnt_device, root_pool_mnt, ] if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) try: run_command(mnt_cmd) return root_pool_mnt except Exception as e: if (device.name == last_device.name): # exhausted mounting using all devices in the pool raise e logger.error('Error mouting: %s. ' 'Will try using another device.' % mnt_cmd) logger.exception(e) raise Exception('Failed to mount Pool(%s) due to an unknown reason.' % pool.name)
def mount_root(pool): """ Mounts a given pool at the default mount root (usually /mnt2/) using the pool.name as the final path entry. Ie pool.name = test-pool will be mounted at /mnt2/test-pool. Any mount options held in pool.mnt_options will be added to the mount command via the -o option as will a compress = pool.compression entry. N.B. Initially the mount target is defined by /dev/disk/by-label/pool.name, if this fails then an attempt to mount by each member of /dev/disk/by-id/pool.disk_set.all() but only if there are any members. If this second method also fails then an exception is raised, currently all but the last failed mount by device name is logged. If no disk members were reported by pool.disk_set.count() a separate Exception is raised. :param pool: pool object :return: either the relevant mount point or an Exception which either indicates 'no disks in pool' or 'Unknown Reason' """ root_pool_mnt = DEFAULT_MNT_DIR + pool.name if (is_share_mounted(pool.name)): return root_pool_mnt # Creates a directory to act as the mount point. create_tmp_dir(root_pool_mnt) toggle_path_rw(root_pool_mnt, rw=False) mnt_device = '/dev/disk/by-label/%s' % pool.name mnt_cmd = [MOUNT, mnt_device, root_pool_mnt, ] mnt_options = '' if (pool.mnt_options is not None): mnt_options = pool.mnt_options if (pool.compression is not None): if (re.search('compress', mnt_options) is None): mnt_options = ('%s,compress=%s' % (mnt_options, pool.compression)) if (os.path.exists(mnt_device)): if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) run_command(mnt_cmd) return root_pool_mnt # If we cannot mount by-label, let's try mounting by device; one by one # until we get our first success. if (pool.disk_set.count() < 1): raise Exception('Cannot mount Pool(%s) as it has no disks in it.' % pool.name) last_device = pool.disk_set.last() logger.info('Mount by label failed.') for device in pool.disk_set.all(): mnt_device = ('/dev/disk/by-id/%s' % device.name) logger.info('Attempting mount by device (%s).' % mnt_device) if (os.path.exists(mnt_device)): mnt_cmd = [MOUNT, mnt_device, root_pool_mnt, ] if (len(mnt_options) > 0): mnt_cmd.extend(['-o', mnt_options]) try: run_command(mnt_cmd) return root_pool_mnt except Exception as e: if (device.name == last_device.name): # exhausted mounting using all devices in the pool raise e logger.error('Error mounting: %s. ' 'Will try using another device.' % mnt_cmd) logger.exception(e) else: logger.error('Device (%s) was not found' % mnt_device) raise Exception('Failed to mount Pool(%s) due to an unknown reason. ' 'Command used %s' % (pool.name, mnt_cmd))