qvm-ls & qvm-device: Handle removal of VM during on_shutdown.

Fixes QubesOS/qubes-issues#5105
This commit is contained in:
3hhh 2021-04-09 18:19:39 +02:00
parent 1471a569dc
commit 7c977f98a5
No known key found for this signature in database
GPG Key ID: EB03A691DB2F0833
2 changed files with 32 additions and 17 deletions

View File

@ -91,8 +91,11 @@ def list_devices(args):
else: else:
for domain in app.domains: for domain in app.domains:
for dev in domain.devices[args.devclass].available(): try:
devices.add(dev) for dev in domain.devices[args.devclass].available():
devices.add(dev)
except qubesadmin.exc.QubesVMNotFoundError:
continue
except qubesadmin.exc.QubesDaemonAccessError: except qubesadmin.exc.QubesDaemonAccessError:
raise qubesadmin.exc.QubesException( raise qubesadmin.exc.QubesException(
"Failed to list '%s' devices, this device type either " "Failed to list '%s' devices, this device type either "
@ -105,16 +108,19 @@ def list_devices(args):
if domain == dev.backend_domain: if domain == dev.backend_domain:
continue continue
for assignment in domain.devices[args.devclass].assignments(): try:
if dev != assignment: for assignment in domain.devices[args.devclass].assignments():
continue if dev != assignment:
if assignment.options: continue
result[dev].frontends.append('{!s} ({})'.format( if assignment.options:
domain, ', '.join('{}={}'.format(key, value) result[dev].frontends.append('{!s} ({})'.format(
for key, value in domain, ', '.join('{}={}'.format(key, value)
assignment.options.items()))) for key, value in
else: assignment.options.items())))
result[dev].frontends.append(str(domain)) else:
result[dev].frontends.append(str(domain))
except qubesadmin.exc.QubesVMNotFoundError:
continue
qubesadmin.tools.print_table(prepare_table(result.values())) qubesadmin.tools.print_table(prepare_table(result.values()))
@ -281,7 +287,8 @@ def main(args=None, app=None):
if basename.startswith('qvm-') and basename != 'qvm-device': if basename.startswith('qvm-') and basename != 'qvm-device':
devclass = basename[4:] devclass = basename[4:]
args = get_parser(devclass).parse_args(args, app=app) parser = get_parser(devclass)
args = parser.parse_args(args, app=app)
if args.list_device_classes: if args.list_device_classes:
print('\n'.join(qubesadmin.Qubes().list_deviceclass())) print('\n'.join(qubesadmin.Qubes().list_deviceclass()))
@ -290,7 +297,7 @@ def main(args=None, app=None):
try: try:
args.func(args) args.func(args)
except qubesadmin.exc.QubesException as e: except qubesadmin.exc.QubesException as e:
print(str(e), file=sys.stderr) parser.print_error(str(e))
return 1 return 1
return 0 return 0

View File

@ -467,17 +467,25 @@ class Table(object):
table_data.append(self.get_head()) table_data.append(self.get_head())
self.spinner.update() self.spinner.update()
if self.tree_sorted: if self.tree_sorted:
#FIXME: handle qubesadmin.exc.QubesVMNotFoundError
# (see QubesOS/qubes-issues#5105)
insertion_vm_list = self.sort_to_tree(self.domains) insertion_vm_list = self.sort_to_tree(self.domains)
for insertion, vm in insertion_vm_list: for insertion, vm in insertion_vm_list:
table_data.append(self.get_row(vm, insertion)) table_data.append(self.get_row(vm, insertion))
else: else:
for vm in sorted(self.domains): for vm in sorted(self.domains):
table_data.append(self.get_row(vm)) try:
table_data.append(self.get_row(vm))
except qubesadmin.exc.QubesVMNotFoundError:
continue
self.spinner.hide() self.spinner.hide()
qubesadmin.tools.print_table(table_data, stream=stream) qubesadmin.tools.print_table(table_data, stream=stream)
else: else:
for vm in sorted(self.domains): for vm in sorted(self.domains):
stream.write('|'.join(self.get_row(vm)) + '\n') try:
stream.write('|'.join(self.get_row(vm)) + '\n')
except qubesadmin.exc.QubesVMNotFoundError:
continue
#: Available formats. Feel free to plug your own one. #: Available formats. Feel free to plug your own one.
@ -576,7 +584,7 @@ def get_parser():
parser = qubesadmin.tools.QubesArgumentParser( parser = qubesadmin.tools.QubesArgumentParser(
vmname_nargs=argparse.ZERO_OR_MORE, vmname_nargs=argparse.ZERO_OR_MORE,
formatter_class=argparse.RawTextHelpFormatter, formatter_class=argparse.RawTextHelpFormatter,
description='List Qubes domains and their parametres.', description='List Qubes domains and their parameters.',
epilog='available formats (see --help-formats):\n{}\n\n' epilog='available formats (see --help-formats):\n{}\n\n'
'available columns (see --help-columns):\n{}'.format( 'available columns (see --help-columns):\n{}'.format(
wrapper.fill(', '.join(sorted(formats.keys()))), wrapper.fill(', '.join(sorted(formats.keys()))),