def lvrename(self, options, args): try: if len(args) < 3: old_path, new_id = args volume_group, old_id = old_path.rsplit('/', 1) else: volume_group, old_id, new_id = args old_path = os.path.join(self.storage.device_prefix, volume_group, old_id) except ValueError: errmsg = 'Old and new logical volume names required' raise utils.ProcessError(self.cmdline, '', errmsg, 3) if not os.path.isabs(old_path): errmsg = 'Path required for Logical Volume "%s"' % old_id raise utils.ProcessError(self.cmdline, '', errmsg, 3) if not os.path.exists(old_path): errmsg = 'Existing logical volume "%s" not found in volume ' \ 'group "%s"' % (old_id, volume_group) raise utils.ProcessError(self.cmdline, '', errmsg, 5) # find & remove old old_v = [v for v in self.storage.volumes if v['lv_name'] == old_id and v['vg_id'] == volume_group][0] self.storage.volumes.remove(old_v) os.unlink(old_path) # create new v = dict(old_v) v['lv_name'] = new_id self.storage.volumes.append(v) lun_path = os.path.join(self.storage.device_prefix, volume_group, new_id) try: os.makedirs(os.path.dirname(lun_path)) except OSError, e: if e.errno != errno.EEXIST: raise
def communicate(self): self.cmd = os.path.basename(self.cmd) try: f = getattr(self, self.cmd) except AttributeError: out = '%s: command not found' % self.cmd raise utils.ProcessError(self.cmdline, out, '', 127) try: parser = globals()['%s_parser' % self.cmd] except KeyError: raise Exception('global %s_parser is not defined!' % self.cmd) try: options, args = parser.parse_args(self.args) except SystemExit: orig_stdout, orig_stderr = sys.stdout, sys.stderr stdout, stderr = StringIO(), StringIO() sys.stdout, sys.stderr = stdout, stderr try: options, args = parser.parse_args(self.args) except SystemExit, e: errcode = e.args[0] sys.stdout, sys.stderr = orig_stdout, orig_stderr raise utils.ProcessError(self.cmdline, stdout.getvalue(), stderr.getvalue(), errcode)
def delete(): if options.tid.isdigit(): tid = int(options.tid) else: tid = len(self.storage.exports) + 1 try: export = self.storage.exports[tid - 1] except IndexError: # TODO(clayg): confirm error code raise utils.ProcessError(self.cmdline, 'Invalid argument', '', 234) if options.lun: # delete lun first for lun in export['luns']: if lun['lun'] == options.lun: break else: # TODO(clayg): confirm error code raise utils.ProcessError(self.cmdline, 'Invalid argument', '', 234) export['luns'].remove(lun) else: # delete target self.storage.exports.remove(export) return ''
def new(): if options.tid.isdigit(): tid = int(options.tid) else: tid = len(self.storage.exports) + 1 try: export = self.storage.exports[tid - 1] except IndexError: if 'Name' not in params: raise utils.ProcessError(self.cmdline, 'Invalid argument.', '', 234) for export in self.storage.exports: if export['name'] == params['Name']: # duplicate target name raise utils.ProcessError(self.cmdline, '', 'Invalid argument.', 234) export = { 'tid': tid, 'name': params['Name'], 'luns': [], } if options.lun: for volume in self.storage.volumes: if params['Path'].endswith(volume['lv_name']): break else: # did not find path, and break out - path does not exist! raise utils.ProcessError(self.cmdline, '', 'No such file.', 254) # create new lun lun = { 'lun': options.lun, 'path': params['Path'], 'state': 0, 'iotype': 'fileio', 'iomode': 'wt', } i = int(options.lun) try: export['luns'][i] except IndexError: pass else: if export['luns'][i]['lun'] == options.lun: # TODO(clayg): confirm error code raise utils.ProcessError(self.cmdline, 'Invalid argument.', '', 234) export['luns'].insert(i, lun) while len(self.storage.exports) < tid: self.storage.exports.append(None) self.storage.exports[tid - 1] = export return ''
def lvcreate(self, options, args): if options.snapshot: path = args.pop(0) volume_group, origin = path.rsplit('/', 2)[-2:] else: origin = '' volume_group = args.pop(0) conflicts = [v for v in self.storage.volumes if ( v['lv_name'] == options.name and v['vg_id'] == volume_group)] if conflicts: errmsg = 'Logical volume "%s" already exists in ' \ 'volume group "%s"' % (options.name, volume_group) raise utils.ProcessError(self.cmdline, '', errmsg, 5) v = {'vg_id': volume_group} v['lv_name'] = options.name v['lv_size'] = str(unit_size_to_bytes(options.size)) v['origin'] = origin v['origin_size'] = str(unit_size_to_bytes(options.size)) v['lv_tags'] = options.addtag or '' v['lv_kernel_major'] = '253' self.storage.volumes.append(v) v['lv_kernel_minor'] = str(len(self.storage.volumes)) lun_path = os.path.join( self.storage.device_prefix, volume_group, options.name) try: os.makedirs(os.path.dirname(lun_path)) except OSError, e: if e.errno != errno.EEXIST: raise
def lvs(self, options, args): volume_group = args[0] lines = [] headers = options.options.split(',') if not options.noheadings: line = options.separator.join(headers) lines.append(line) if '/' in volume_group: group, name = volume_group.split('/') volumes = [v for v in self.storage.volumes if ( v['lv_name'] == name and v['vg_id'] == group)] if not volumes: errmsg = "One or more specified logical volume(s) not found." raise utils.ProcessError(self.cmdline, '', errmsg, 5) else: volumes = [v for v in self.storage.volumes if v['vg_id'] == volume_group] for v in volumes: values = [] for key in headers: if 'size' in key: value = '%sB' % v[key] else: value = v[key] values.append(value) line = options.separator.join(values) lines.append(line) return '\n'.join(lines)
def lvremove(self, options, args): volume_group, name = args.pop(0).rsplit('/', 2)[-2:] if not options.force: choice = raw_input("Do you really want to remove active " "logical volume vol1? [y/n]: ") if not choice.lower.startswith('y'): errmsg = "Logical volume %s not removed" % name raise utils.ProcessError(self.cmdline, errmsg, '', 5) v = [v for v in self.storage.volumes if v['lv_name'] == name and v['vg_id'] == volume_group][0] self.storage.volumes.remove(v) lun_path = os.path.join(self.storage.device_prefix, volume_group, name) os.unlink(lun_path) return 'Logical volume "%s" successfully removed' % name
def vgs(self, options, args): volume_group = args[0] lines = [] headers = options.options.split(',') if not options.noheadings: line = options.separator.join(headers) lines.append(line) volumes = [ v for v in self.storage.volumes if v['vg_id'] == volume_group ] if not volumes: errmsg = "Volume group '%s' not found." % volume_group raise utils.ProcessError(self.cmdline, '', errmsg, 5) bytes_used = sum([int(v['lv_size']) for v in volumes]) vg_size = 10000000000 info = {'vg_size': '%sB' % vg_size, 'lv_count': str(len(volumes))} info['vg_free'] = "%sB" % (vg_size - bytes_used) values = [info[key] for key in headers] line = options.separator.join(values) lines.append(line) return '\n'.join(lines)
def umount(self, options, args): mnt = args[0] if mnt not in self.storage.mounts: raise utils.ProcessError(self.cmdline, '', 'not mounted', 1) del self.storage.mounts[mnt] return 'unmounted'