-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbefore_vm_start.py
executable file
·137 lines (106 loc) · 3.79 KB
/
before_vm_start.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python
import os
import base64
import sys
import ast
import utils
import hooking
import tempfile
import traceback
from xml.dom import minidom
'''
floppyinject vdsm hook
======================
Hook create a floppy disk on the fly with user provided content.
ie. user supply file name and file content and hooks will create
floppy disk on destination host, will mount it as floppy disk -
and will handle migration.
giving the input "myfile.vfd=data" will create a floppy with single
file name myfile.vfd and the file content will be "data"
syntax:
floppyinject=myfile.txt:<file content>
libvirt:
<disk type='file' device='floppy'>
<source file='/tmp/my.vfd'/>
<target dev='fda' />
</disk>
Note:
some linux distro need to load the floppy disk kernel module:
# modprobe floppy
'''
def addFloppyElement(domxml, path):
if not os.path.isfile(path):
sys.stderr.write('floppyinject: file not exists: %s\n' % path)
sys.exit(2)
devices = domxml.getElementsByTagName('devices')[0]
disk = domxml.createElement('disk')
disk.setAttribute('type', 'file')
disk.setAttribute('device', 'floppy')
devices.appendChild(disk)
source = domxml.createElement('source')
source.setAttribute('file', path)
disk.appendChild(source)
target = domxml.createElement('target')
target.setAttribute('dev', 'fda')
disk.appendChild(target)
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 = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: error /sbin/mkfs.msdos fs: %s\n' % err)
sys.exit(2)
owner = '36:36'
command = ['/bin/chown', owner, path]
retcode, out, err = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: error /bin/chown: %s' % err)
sys.exit(2)
# create floppy file system
command = ['/bin/chmod', '0770', path]
retcode, out, err = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: 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 = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: error /bin/mount: %s' % err)
sys.exit(2)
# base64 decode the content
content = base64.decodestring(content)
# 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 = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: error /bin/umount: %s' % err)
sys.exit(2)
# remove tempdir
command = ['/bin/rmdir', mntpoint]
retcode, out, err = utils.execCmd(command, sudo=True, raw=True)
if retcode != 0:
sys.stderr.write('floppyinject: 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)
domxml = hooking.read_domxml()
addFloppyElement(domxml, path)
hooking.write_domxml(domxml)
except:
sys.stderr.write('floppyinject: [unexpected error]: %s\n' % traceback.format_exc())
sys.exit(2)