dom0: block_cleaner: removes ejected devices from xenstore

When device is ejected by some VM (state=6, effectively inactive), it should be
removed from xenstore to free slot for some another device. This should be done
by libxl toolstack, but not implemented in xen 4.1 - AFAIR done in xen 4.2.
This commit is contained in:
Marek Marczykowski 2012-06-01 20:59:45 +02:00
parent 65a94681b2
commit 1f194cbe08
3 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,53 @@
#!/usr/bin/python
import xen.lowlevel.xs
import time
import subprocess
xs = xen.lowlevel.xs.xs()
domain_list = []
def setup_watches():
global domain_list
new_domain_list = xs.ls('', '/local/domain')
for dom in new_domain_list:
if dom not in domain_list:
print "Adding: %s" % dom
xs.watch('/local/domain/%s/backend/vbd' % dom, int(dom))
for dom in domain_list:
if dom not in new_domain_list:
print "Removing: %s" % dom
xs.unwatch('/local/domain/%s/backend/vbd' % dom, int(dom))
domain_list = new_domain_list
def handle_vbd_state(path):
state = xs.read('', path)
if state == '6':
# Closed state; wait a moment to not interrupt reconnect
time.sleep(0.500)
state = xs.read('', path)
if state == '6':
# If still closed, detach device
path_components = path.split('/')
# /local/domain/<BACK XID>/backend/vbd/<FRONT XID>/<DEV>/...
vm_xid = path_components[6]
vm_dev = path_components[7]
if vm_xid in domain_list:
subprocess.call('xl', 'block-detach', vm_xid, vm_dev)
def main():
xs.watch('@introduceDomain', 'reload')
xs.watch('@releaseDomain', 'reload')
setup_watches()
while True:
(path, token) = xs.read_watch()
if token == 'reload':
setup_watches()
else:
if path.endswith('/state'):
handle_vbd_state(path)
main()

View File

@ -53,6 +53,8 @@ start()
MEMINFO_DELAY_USEC=100000
/usr/lib/qubes/meminfo-writer $MEM_CHANGE_THRESHOLD_KB $MEMINFO_DELAY_USEC &
/usr/lib/qubes/block_cleaner_daemon.py > /var/log/qubes/block_cleaner.log 2>&1 &
# Reply block events to hide mounted devices from qubes-block list (at first udev run, only / is mounted)
udevadm trigger --subsystem-match=block --action=add

View File

@ -114,6 +114,7 @@ cp aux-tools/qubes-receive-updates $RPM_BUILD_ROOT/usr/lib/qubes/
cp ../misc/block_add_change $RPM_BUILD_ROOT/usr/lib/qubes/
cp ../misc/block_remove $RPM_BUILD_ROOT/usr/lib/qubes/
cp ../misc/block_cleanup $RPM_BUILD_ROOT/usr/lib/qubes/
cp aux-tools/block_cleaner_daemon.py $RPM_BUILD_ROOT/usr/lib/qubes/
mkdir -p $RPM_BUILD_ROOT/etc/qubes_rpc/policy
cp ../qubes_rpc/qubes.Filecopy.policy $RPM_BUILD_ROOT/etc/qubes_rpc/policy/qubes.Filecopy
@ -344,6 +345,7 @@ fi
/usr/lib/qubes/block_add_change
/usr/lib/qubes/block_remove
/usr/lib/qubes/block_cleanup
/usr/lib/qubes/block_cleaner_daemon.py*
%attr(4750,root,qubes) /usr/lib/qubes/qfile-dom0-unpacker
%attr(770,root,qubes) %dir /var/lib/qubes
%attr(770,root,qubes) %dir /var/lib/qubes/vm-templates