From 9797f6b83d727933b1a0daab239f3034675a1d0e Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Wed, 11 Dec 2019 14:54:16 +0100 Subject: [PATCH] lvm: add deactivate subcommand This new subcommand unmounts and OSDs tmpfs mount and closes crypt devices if there are any. Signed-off-by: Jan Fajerski --- .../ceph_volume/devices/lvm/deactivate.py | 90 +++++++++++++++++++ .../ceph_volume/devices/lvm/main.py | 2 + 2 files changed, 92 insertions(+) create mode 100644 src/ceph-volume/ceph_volume/devices/lvm/deactivate.py diff --git a/src/ceph-volume/ceph_volume/devices/lvm/deactivate.py b/src/ceph-volume/ceph_volume/devices/lvm/deactivate.py new file mode 100644 index 0000000000000..b3f42faa94d01 --- /dev/null +++ b/src/ceph-volume/ceph_volume/devices/lvm/deactivate.py @@ -0,0 +1,90 @@ +import argparse +import logging +import sys +from textwrap import dedent +from ceph_volume import conf +from ceph_volume.util import encryption, system +from ceph_volume.api.lvm import get_lvs_by_tag + +logger = logging.getLogger(__name__) + + +def deactivate_osd(osd_id=None, osd_uuid=None): + + lvs = [] + if osd_uuid: + lvs = get_lvs_by_tag('ceph.osd_fsid={}'.format(osd_uuid)) + else: + lvs = get_lvs_by_tag('ceph.osd_id={}'.format(osd_id)) + + try: + data_lv = next(lv for lv in lvs if lv.tags['ceph.type'] in ['data', 'block']) + except StopIteration: + logger.error('No data or block LV found for OSD {}'.format(osd_id)) + sys.exit(1) + conf.cluster = data_lv.tags['ceph.cluster_name'] + logger.debug('Found cluster name {}'.format(conf.cluster)) + + tmpfs_path = '/var/lib/ceph/osd/{}-{}'.format(conf.cluster, osd_id) + system.unmount_tmpfs(tmpfs_path) + + for lv in lvs: + if lv.tags.get('ceph.encrypted', '0') == '1': + encryption.dmcrypt_close(lv.lv_uuid) + + +class Deactivate(object): + + help = 'Deactivate OSDs' + + def deactivate(self, args=None): + if args: + self.args = args + deactivate_osd(self.args.osd_id, self.args.osd_uuid) + + def __init__(self, argv): + self.argv = argv + + def main(self): + sub_command_help = dedent(""" + Deactivate unmounts and OSDs tmpfs and closes any crypt devices. + + ceph-volume lvm deactivate {ID} {FSID} + + To deactivate all volumes use the --all flag. + ceph-volume lvm deactivate --all + """) + parser = argparse.ArgumentParser( + prog='ceph-volume lvm deactivate', + formatter_class=argparse.RawDescriptionHelpFormatter, + description=sub_command_help, + ) + + parser.add_argument( + 'osd_id', + nargs='?', + help='The ID of the OSD' + ) + parser.add_argument( + 'osd_uuid', + nargs='?', + help='The UUID of the OSD, similar to a SHA1, takes precedence over osd_id' + ) + parser.add_argument( + '--all', + action='store_true', + help='Deactivate all OSD volumes found in the system', + ) + if len(self.argv) == 0: + print(sub_command_help) + return + args = parser.parse_args(self.argv) + # Default to bluestore here since defaulting it in add_argument may + # cause both to be True + if args.all: + self.deactivate_all(args) + else: + if not args.osd_id and not args.osd_uuid: + raise ValueError(('Can not identify OSD, pass either all or' + 'osd_id or osd_uuid')) + self.deactivate(args) diff --git a/src/ceph-volume/ceph_volume/devices/lvm/main.py b/src/ceph-volume/ceph_volume/devices/lvm/main.py index abe77b57e130f..f910baa826827 100644 --- a/src/ceph-volume/ceph_volume/devices/lvm/main.py +++ b/src/ceph-volume/ceph_volume/devices/lvm/main.py @@ -2,6 +2,7 @@ import argparse from textwrap import dedent from ceph_volume import terminal from . import activate +from . import deactivate from . import prepare from . import create from . import trigger @@ -22,6 +23,7 @@ class LVM(object): mapper = { 'activate': activate.Activate, + 'deactivate': deactivate.Deactivate, 'batch': batch.Batch, 'prepare': prepare.Prepare, 'create': create.Create, -- 2.39.5