def scan_directory(self, path): osd_metadata = {'cluster_name': conf.cluster} path_mounts = system.get_mounts(paths=True) for _file in os.listdir(path): file_path = os.path.join(path, _file) if os.path.islink(file_path): osd_metadata[_file] = self.scan_device(file_path) if os.path.isdir(file_path): continue # the check for binary needs to go before the file, to avoid # capturing data from binary files but still be able to capture # contents from actual files later if system.is_binary(file_path): continue if os.path.isfile(file_path): content = self.get_contents(file_path) try: osd_metadata[_file] = int(content) except ValueError: osd_metadata[_file] = content device = path_mounts.get(path) # it is possible to have more than one device, pick the first one, and # warn that it is possible that more than one device is 'data' if not device: terminal.error('Unable to detect device mounted for path: %s' % path) raise RuntimeError('Cannot activate OSD') osd_metadata['data'] = self.scan_device( device[0] if len(device) else None) return osd_metadata
def scan_directory(self, path): osd_metadata = {'cluster_name': conf.cluster} directory_files = os.listdir(path) if 'keyring' not in directory_files: raise RuntimeError( 'OSD files not found, required "keyring" file is not present at: %s' % path) for file_ in os.listdir(path): file_path = os.path.join(path, file_) file_json_key = file_ if file_.endswith('_dmcrypt'): file_json_key = file_.rstrip('_dmcrypt') logger.info(('reading file {}, stripping _dmcrypt', 'suffix').format(file_)) if os.path.islink(file_path): if os.path.exists(file_path): osd_metadata[file_json_key] = self.scan_device(file_path) else: msg = 'broken symlink found %s -> %s' % ( file_path, os.path.realpath(file_path)) terminal.warning(msg) logger.warning(msg) if os.path.isdir(file_path): continue # the check for binary needs to go before the file, to avoid # capturing data from binary files but still be able to capture # contents from actual files later try: if system.is_binary(file_path): logger.info('skipping binary file: %s' % file_path) continue except IOError: logger.exception('skipping due to IOError on file: %s' % file_path) continue if os.path.isfile(file_path): content = self.get_contents(file_path) if 'keyring' in file_path: content = parse_keyring(content) try: osd_metadata[file_json_key] = int(content) except ValueError: osd_metadata[file_json_key] = content # we must scan the paths again because this might be a temporary mount path_mounts = system.get_mounts(paths=True) device = path_mounts.get(path) # it is possible to have more than one device, pick the first one, and # warn that it is possible that more than one device is 'data' if not device: terminal.error('Unable to detect device mounted for path: %s' % path) raise RuntimeError('Cannot activate OSD') osd_metadata['data'] = self.scan_device( device[0] if len(device) else None) return osd_metadata
def test_not_mounted(self, tmpdir, monkeypatch): PROCDIR = str(tmpdir) proc_path = os.path.join(PROCDIR, 'mounts') with open(proc_path, 'w') as f: f.write('') monkeypatch.setattr(system, 'PROCDIR', PROCDIR) assert system.get_mounts() == {}
def scan(self, args): osd_metadata = {'cluster_name': conf.cluster} device_mounts = system.get_mounts(devices=True) osd_path = None logger.info('detecting if argument is a device or a directory: %s', args.osd_path) if os.path.isdir(args.osd_path): logger.info('will scan directly, path is a directory') osd_path = args.osd_path else: # assume this is a device, check if it is mounted and use that path logger.info('path is not a directory, will check if mounted') if system.device_is_mounted(args.osd_path): logger.info('argument is a device, which is mounted') mounted_osd_paths = device_mounts.get(args.osd_path) osd_path = mounted_osd_paths[0] if len(mounted_osd_paths) else None # argument is not a directory, and it is not a device that is mounted # somewhere so temporarily mount it to poke inside, otherwise, scan # directly if not osd_path: logger.info('device is not mounted, will mount it temporarily to scan') with system.tmp_mount(args.osd_path) as osd_path: osd_metadata = self.scan_directory(osd_path) else: logger.info('will scan OSD directory at path: %s', osd_path) osd_metadata = self.scan_directory(osd_path) osd_id = osd_metadata['whoami'] osd_fsid = osd_metadata['fsid'] filename = '%s-%s.json' % (osd_id, osd_fsid) json_path = os.path.join(self.etc_path, filename) if os.path.exists(json_path) and not args.stdout: if not args.force: raise RuntimeError( '--force was not used and OSD metadata file exists: %s' % json_path ) if args.stdout: print(json.dumps(osd_metadata, indent=4, sort_keys=True, ensure_ascii=False)) else: with open(json_path, 'w') as fp: json.dump(osd_metadata, fp, indent=4, sort_keys=True, ensure_ascii=False) terminal.success( 'OSD %s got scanned and metadata persisted to file: %s' % ( osd_id, json_path ) ) terminal.success( 'To take over managment of this scanned OSD, and disable ceph-disk and udev, run:' ) terminal.success(' ceph-volume simple activate %s %s' % (osd_id, osd_fsid)) if not osd_metadata.get('data'): msg = 'Unable to determine device mounted on %s' % args.osd_path logger.warning(msg) terminal.warning(msg) terminal.warning('OSD will not be able to start without this information:') terminal.warning(' "data": "/path/to/device",') logger.warning('Unable to determine device mounted on %s' % args.osd_path)
def scan_directory(self, path): osd_metadata = {'cluster_name': conf.cluster} path_mounts = system.get_mounts(paths=True) for _file in os.listdir(path): file_path = os.path.join(path, _file) if os.path.islink(file_path): osd_metadata[_file] = self.scan_device(file_path) if os.path.isdir(file_path): continue # the check for binary needs to go before the file, to avoid # capturing data from binary files but still be able to capture # contents from actual files later if system.is_binary(file_path): continue if os.path.isfile(file_path): content = self.get_contents(file_path) try: osd_metadata[_file] = int(content) except ValueError: osd_metadata[_file] = content device = path_mounts.get(path) # it is possible to have more than one device, pick the first one, and # warn that it is possible that more than one device is 'data' if not device: terminal.error('Unable to detect device mounted for path: %s' % path) raise RuntimeError('Cannot activate OSD') osd_metadata['data'] = self.scan_device(device[0] if len(device) else None) return osd_metadata
def legacy_encrypted(device): """ Detect if a device was encrypted with ceph-disk or not. In the case of encrypted devices, include the type of encryption (LUKS, or PLAIN), and infer what the lockbox partition is. This function assumes that ``device`` will be a partition. """ if os.path.isdir(device): mounts = system.get_mounts(paths=True) # yes, rebind the device variable here because a directory isn't going # to help with parsing device = mounts.get(device, [None])[0] if not device: raise RuntimeError('unable to determine the device mounted at %s' % device) metadata = { 'encrypted': False, 'type': None, 'lockbox': '', 'device': device } # check if the device is online/decrypted first active_mapper = status(device) if active_mapper: # normalize a bit to ensure same values regardless of source metadata['type'] = active_mapper['type'].lower().strip( '12') # turn LUKS1 or LUKS2 into luks metadata['encrypted'] = True if metadata['type'] in ['plain', 'luks' ] else False # The true device is now available to this function, so it gets # re-assigned here for the lockbox checks to succeed (it is not # possible to guess partitions from a device mapper device otherwise device = active_mapper.get('device', device) metadata['device'] = device else: uuid = get_part_entry_type(device) guid_match = constants.ceph_disk_guids.get(uuid, {}) encrypted_guid = guid_match.get('encrypted', False) if encrypted_guid: metadata['encrypted'] = True metadata['type'] = guid_match['encryption_type'] # Lets find the lockbox location now, to do this, we need to find out the # parent device name for the device so that we can query all of its # associated devices and *then* look for one that has the 'lockbox' label # on it. Thanks for being awesome ceph-disk disk_meta = lsblk(device, abspath=True) if not disk_meta: return metadata parent_device = disk_meta['PKNAME'] # With the parent device set, we can now look for the lockbox listing associated devices devices = device_family(parent_device) for i in devices: if 'lockbox' in i.get('PARTLABEL', ''): metadata['lockbox'] = i['NAME'] break return metadata
def test_nonexistent_devices_are_skipped(self, tmpdir, monkeypatch): PROCDIR = str(tmpdir) proc_path = os.path.join(PROCDIR, 'mounts') with open(proc_path, 'w') as f: f.write(dedent("""nfsd /proc/fs/nfsd nfsd rw,relatime 0 0 /dev/sda1 /far/lib/ceph/osd/ceph-0 xfs rw,attr2,inode64,noquota 0 0 /dev/sda2 /far/lib/ceph/osd/ceph-1 xfs rw,attr2,inode64,noquota 0 0""")) monkeypatch.setattr(system, 'PROCDIR', PROCDIR) monkeypatch.setattr(os.path, 'exists', lambda x: False if x == '/dev/sda1' else True) result = system.get_mounts() assert result.get('/dev/sda1') is None
def scan_directory(self, path): osd_metadata = {'cluster_name': conf.cluster} directory_files = os.listdir(path) if 'keyring' not in directory_files: raise RuntimeError( 'OSD files not found, required "keyring" file is not present at: %s' % path ) for _file in os.listdir(path): file_path = os.path.join(path, _file) if os.path.islink(file_path): if os.path.exists(file_path): osd_metadata[_file] = self.scan_device(file_path) else: msg = 'broken symlink found %s -> %s' % (file_path, os.path.realpath(file_path)) terminal.warning(msg) logger.warning(msg) if os.path.isdir(file_path): continue # the check for binary needs to go before the file, to avoid # capturing data from binary files but still be able to capture # contents from actual files later try: if system.is_binary(file_path): logger.info('skipping binary file: %s' % file_path) continue except IOError: logger.exception('skipping due to IOError on file: %s' % file_path) continue if os.path.isfile(file_path): content = self.get_contents(file_path) if 'keyring' in file_path: content = parse_keyring(content) try: osd_metadata[_file] = int(content) except ValueError: osd_metadata[_file] = content # we must scan the paths again because this might be a temporary mount path_mounts = system.get_mounts(paths=True) device = path_mounts.get(path) # it is possible to have more than one device, pick the first one, and # warn that it is possible that more than one device is 'data' if not device: terminal.error('Unable to detect device mounted for path: %s' % path) raise RuntimeError('Cannot activate OSD') osd_metadata['data'] = self.scan_device(device[0] if len(device) else None) return osd_metadata
def legacy_encrypted(device): """ Detect if a device was encrypted with ceph-disk or not. In the case of encrypted devices, include the type of encryption (LUKS, or PLAIN), and infer what the lockbox partition is. This function assumes that ``device`` will be a partition. """ if os.path.isdir(device): mounts = system.get_mounts(paths=True) # yes, rebind the device variable here because a directory isn't going # to help with parsing device = mounts.get(device, [None])[0] if not device: raise RuntimeError('unable to determine the device mounted at %s' % device) metadata = {'encrypted': False, 'type': None, 'lockbox': '', 'device': device} # check if the device is online/decrypted first active_mapper = status(device) if active_mapper: # normalize a bit to ensure same values regardless of source metadata['type'] = active_mapper['type'].lower().strip('12') # turn LUKS1 or LUKS2 into luks metadata['encrypted'] = True if metadata['type'] in ['plain', 'luks'] else False # The true device is now available to this function, so it gets # re-assigned here for the lockbox checks to succeed (it is not # possible to guess partitions from a device mapper device otherwise device = active_mapper.get('device', device) metadata['device'] = device else: uuid = get_part_entry_type(device) guid_match = constants.ceph_disk_guids.get(uuid, {}) encrypted_guid = guid_match.get('encrypted', False) if encrypted_guid: metadata['encrypted'] = True metadata['type'] = guid_match['encryption_type'] # Lets find the lockbox location now, to do this, we need to find out the # parent device name for the device so that we can query all of its # associated devices and *then* look for one that has the 'lockbox' label # on it. Thanks for being awesome ceph-disk disk_meta = lsblk(device, abspath=True) if not disk_meta: return metadata parent_device = disk_meta['PKNAME'] # With the parent device set, we can now look for the lockbox listing associated devices devices = device_family(parent_device) for i in devices: if 'lockbox' in i.get('PARTLABEL', ''): metadata['lockbox'] = i['NAME'] break return metadata
def test_tmpfs_is_reported(self, fake_proc): result = system.get_mounts() assert result['tmpfs'][0] == '/dev/shm'
def test_ignores_two_fields(self, fake_proc): result = system.get_mounts() assert result.get('/dev/sde4') is None
def test_is_mounted_(self, fake_proc): result = system.get_mounts() assert result['/dev/sdc2'] == ['/boot']
def main(self): sub_command_help = dedent(""" Scan an OSD directory (or data device) for files and configurations that will allow to take over the management of the OSD. Scanned OSDs will get their configurations stored in /etc/ceph/osd/<id>-<fsid>.json For an OSD ID of 0 with fsid of ``a9d50838-e823-43d6-b01f-2f8d0a77afc2`` that could mean a scan command that looks like:: ceph-volume lvm scan /var/lib/ceph/osd/ceph-0 Which would store the metadata in a JSON file at:: /etc/ceph/osd/0-a9d50838-e823-43d6-b01f-2f8d0a77afc2.json To a scan an existing, running, OSD: ceph-volume simple scan /var/lib/ceph/osd/{cluster}-{osd id} And to scan a device (mounted or unmounted) that has OSD data in it, for example /dev/sda1 ceph-volume simple scan /dev/sda1 """) parser = argparse.ArgumentParser( prog='ceph-volume simple scan', formatter_class=argparse.RawDescriptionHelpFormatter, description=sub_command_help, ) parser.add_argument( '-f', '--force', action='store_true', help= 'If OSD has already been scanned, the JSON file will be overwritten' ) parser.add_argument( '--stdout', action='store_true', help='Do not save to a file, output metadata to stdout') parser.add_argument( 'osd_path', metavar='OSD_PATH', type=arg_validators.OSDPath(), nargs='?', help='Path to an existing OSD directory or OSD data partition') if len(self.argv) == 0: print(sub_command_help) return args = parser.parse_args(self.argv) if disk.is_partition(args.osd_path): label = disk.lsblk(args.osd_path)['PARTLABEL'] if 'data' not in label: raise RuntimeError( 'Device must be the data partition, but got: %s' % label) # Capture some environment status, so that it can be reused all over self.device_mounts = system.get_mounts(devices=True) self.path_mounts = system.get_mounts(paths=True) self.encryption_metadata = encryption.legacy_encrypted(args.osd_path) self.is_encrypted = self.encryption_metadata['encrypted'] self.scan(args)
def main(self): sub_command_help = dedent(""" Scan running OSDs, an OSD directory (or data device) for files and configurations that will allow to take over the management of the OSD. Scanned OSDs will get their configurations stored in /etc/ceph/osd/<id>-<fsid>.json For an OSD ID of 0 with fsid of ``a9d50838-e823-43d6-b01f-2f8d0a77afc2`` that could mean a scan command that looks like:: ceph-volume lvm scan /var/lib/ceph/osd/ceph-0 Which would store the metadata in a JSON file at:: /etc/ceph/osd/0-a9d50838-e823-43d6-b01f-2f8d0a77afc2.json To scan all running OSDs: ceph-volume simple scan To a scan a specific running OSD: ceph-volume simple scan /var/lib/ceph/osd/{cluster}-{osd id} And to scan a device (mounted or unmounted) that has OSD data in it, for example /dev/sda1 ceph-volume simple scan /dev/sda1 Scanning a device or directory that belongs to an OSD not created by ceph-disk will be ingored. """) parser = argparse.ArgumentParser( prog='ceph-volume simple scan', formatter_class=argparse.RawDescriptionHelpFormatter, description=sub_command_help, ) parser.add_argument( '-f', '--force', action='store_true', help='If OSD has already been scanned, the JSON file will be overwritten' ) parser.add_argument( '--stdout', action='store_true', help='Do not save to a file, output metadata to stdout' ) parser.add_argument( 'osd_path', metavar='OSD_PATH', type=arg_validators.OSDPath(), nargs='?', default=None, help='Path to an existing OSD directory or OSD data partition' ) args = parser.parse_args(self.argv) paths = [] if args.osd_path: paths.append(args.osd_path) else: osd_ids = systemctl.get_running_osd_ids() for osd_id in osd_ids: paths.append("/var/lib/ceph/osd/{}-{}".format( conf.cluster, osd_id, )) # Capture some environment status, so that it can be reused all over self.device_mounts = system.get_mounts(devices=True) self.path_mounts = system.get_mounts(paths=True) for path in paths: args.osd_path = path device = Device(args.osd_path) if device.is_partition: if device.ceph_disk.type != 'data': label = device.ceph_disk.partlabel msg = 'Device must be the ceph data partition, but PARTLABEL reported: "%s"' % label raise RuntimeError(msg) self.encryption_metadata = encryption.legacy_encrypted(args.osd_path) self.is_encrypted = self.encryption_metadata['encrypted'] device = Device(self.encryption_metadata['device']) if not device.is_ceph_disk_member: terminal.warning("Ignoring %s because it's not a ceph-disk created osd." % path) else: self.scan(args)
def scan(self, args): osd_metadata = {'cluster_name': conf.cluster} device_mounts = system.get_mounts(devices=True) osd_path = None logger.info('detecting if argument is a device or a directory: %s', args.osd_path) if os.path.isdir(args.osd_path): logger.info('will scan directly, path is a directory') osd_path = args.osd_path else: # assume this is a device, check if it is mounted and use that path logger.info('path is not a directory, will check if mounted') if system.device_is_mounted(args.osd_path): logger.info('argument is a device, which is mounted') mounted_osd_paths = device_mounts.get(args.osd_path) osd_path = mounted_osd_paths[0] if len(mounted_osd_paths) else None # argument is not a directory, and it is not a device that is mounted # somewhere so temporarily mount it to poke inside, otherwise, scan # directly if not osd_path: logger.info('device is not mounted, will mount it temporarily to scan') with system.tmp_mount(args.osd_path) as osd_path: osd_metadata = self.scan_directory(osd_path) else: logger.info('will scan OSD directory at path: %s', osd_path) osd_metadata = self.scan_directory(osd_path) osd_id = osd_metadata['whoami'] osd_fsid = osd_metadata['fsid'] filename = '%s-%s.json' % (osd_id, osd_fsid) json_path = os.path.join(self.etc_path, filename) if os.path.exists(json_path) and not args.stdout: if not args.force: raise RuntimeError( '--force was not used and OSD metadata file exists: %s' % json_path ) if args.stdout: print(json.dumps(osd_metadata, indent=4, sort_keys=True, ensure_ascii=False)) else: with open(json_path, 'w') as fp: json.dump(osd_metadata, fp, indent=4, sort_keys=True, ensure_ascii=False) fp.write(os.linesep) terminal.success( 'OSD %s got scanned and metadata persisted to file: %s' % ( osd_id, json_path ) ) terminal.success( 'To take over managment of this scanned OSD, and disable ceph-disk and udev, run:' ) terminal.success(' ceph-volume simple activate %s %s' % (osd_id, osd_fsid)) if not osd_metadata.get('data'): msg = 'Unable to determine device mounted on %s' % args.osd_path logger.warning(msg) terminal.warning(msg) terminal.warning('OSD will not be able to start without this information:') terminal.warning(' "data": "/path/to/device",') logger.warning('Unable to determine device mounted on %s' % args.osd_path)
def test_non_skip_devs_arent_reported(self, fake_proc): result = system.get_mounts() assert result.get('cgroup') is None
def test_multiple_mounts_are_appended(self, fake_proc): result = system.get_mounts() assert len(result['tmpfs']) == 7
def main(self): sub_command_help = dedent(""" Scan an OSD directory (or data device) for files and configurations that will allow to take over the management of the OSD. Scanned OSDs will get their configurations stored in /etc/ceph/osd/<id>-<fsid>.json For an OSD ID of 0 with fsid of ``a9d50838-e823-43d6-b01f-2f8d0a77afc2`` that could mean a scan command that looks like:: ceph-volume lvm scan /var/lib/ceph/osd/ceph-0 Which would store the metadata in a JSON file at:: /etc/ceph/osd/0-a9d50838-e823-43d6-b01f-2f8d0a77afc2.json To a scan an existing, running, OSD: ceph-volume simple scan /var/lib/ceph/osd/{cluster}-{osd id} And to scan a device (mounted or unmounted) that has OSD data in it, for example /dev/sda1 ceph-volume simple scan /dev/sda1 """) parser = argparse.ArgumentParser( prog='ceph-volume simple scan', formatter_class=argparse.RawDescriptionHelpFormatter, description=sub_command_help, ) parser.add_argument( '-f', '--force', action='store_true', help='If OSD has already been scanned, the JSON file will be overwritten' ) parser.add_argument( '--stdout', action='store_true', help='Do not save to a file, output metadata to stdout' ) parser.add_argument( 'osd_path', metavar='OSD_PATH', type=arg_validators.OSDPath(), nargs='?', help='Path to an existing OSD directory or OSD data partition' ) if len(self.argv) == 0: print(sub_command_help) return args = parser.parse_args(self.argv) if disk.is_partition(args.osd_path): label = disk.lsblk(args.osd_path)['PARTLABEL'] if 'data' not in label: raise RuntimeError('Device must be the data partition, but got: %s' % label) # Capture some environment status, so that it can be reused all over self.device_mounts = system.get_mounts(devices=True) self.path_mounts = system.get_mounts(paths=True) self.encryption_metadata = encryption.legacy_encrypted(args.osd_path) self.is_encrypted = self.encryption_metadata['encrypted'] self.scan(args)