forked from calfonso/vdsm-hook-floppyinject
-
Notifications
You must be signed in to change notification settings - Fork 1
/
before_vm_migrate_destination.py
executable file
·84 lines (64 loc) · 2.52 KB
/
before_vm_migrate_destination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
import os
import sys
import ast
import hooking
import tempfile
import traceback
from xml.dom import minidom
def createFloppy(filename, path, content):
if os.path.exists(path):
os.remove(path)
# create floppy file system
command = ['/sbin/mkfs.msdos', '-C', path, '1440']
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /sbin/mkfs.msdos fs: %s\n' % err)
sys.exit(2)
owner = '36:36'
command = ['/bin/chown', owner, path]
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /bin/chown: %s' % err)
sys.exit(2)
# create floppy file system
command = ['/bin/chmod', '0770', path]
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /bin/chmod: %s' % err)
sys.exit(2)
# mount the floppy file
mntpoint = tempfile.mkdtemp()
command = ['/bin/mount', '-o', 'loop,uid=36,gid=36' , path, mntpoint]
sys.stderr.write('shahar: %s\n' % ' '.join(command))
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /bin/mount: %s' % err)
sys.exit(2)
# write the file content
contentpath = os.path.join(mntpoint, filename)
f = open(contentpath, 'w')
f.write(content)
f.close()
# unmounting
command = ['/bin/umount', mntpoint]
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /bin/umount: %s' % err)
sys.exit(2)
# remove tempdir
command = ['/bin/rmdir', mntpoint]
retcode, out, err = hooking.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject-before-dest-migration: error /bin/rmdir: %s' % err)
sys.exit(2)
if os.environ.has_key('floppyinject'):
try:
pos = os.environ['floppyinject'].find(':')
filename = os.environ['floppyinject'][:pos]
content = os.environ['floppyinject'][pos+1:]
path = '/tmp/%s' % filename
createFloppy(filename, path, content)
except:
sys.stderr.write('floppyinject-before-dest-migration: [unexpected error]: %s\n' % traceback.format_exc())
sys.exit(2)