From 5c7b57e6909ca2c92d50492230d4b606e5e09a1a Mon Sep 17 00:00:00 2001 From: WillyPillow Date: Tue, 14 Jul 2020 01:44:36 +0800 Subject: [PATCH] Add admin.vm.volume.Clear call (QubesOS/qubes-issues#5946) --- qubes/api/admin.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/qubes/api/admin.py b/qubes/api/admin.py index 60417b18..c6656e7a 100644 --- a/qubes/api/admin.py +++ b/qubes/api/admin.py @@ -27,6 +27,7 @@ import itertools import os import string import subprocess +import pathlib import libvirt import lxml.etree @@ -486,6 +487,32 @@ class QubesAdminAPI(qubes.api.AbstractQubesAPI): finally: # even if calling qubes.ResizeDisk inside the VM failed self.app.save() + @qubes.api.method('admin.vm.volume.Clear', no_payload=True, + scope='local', write=True) + @asyncio.coroutine + def vm_volume_clear(self): + self.enforce(self.arg in self.dest.volumes.keys()) + + self.fire_event_for_permission() + + volume = self.dest.volumes[self.arg] + size = volume.size + + # Clear the volume by importing empty data into it + path = yield from volume.import_data(size) + self.dest.fire_event('domain-volume-import-begin', + volume=self.arg, size=size) + pathlib.Path(path).touch() + try: + yield from volume.import_data_end(True) + except: + self.dest.fire_event('domain-volume-import-end', + volume=self.arg, success=False) + raise + self.dest.fire_event('domain-volume-import-end', + volume=self.arg, success=True) + self.app.save() + @qubes.api.method('admin.vm.volume.Set.revisions_to_keep', scope='local', write=True) @asyncio.coroutine