/
disk_image.py
389 lines (316 loc) · 12.6 KB
/
disk_image.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# Copyright (C) 2011-2012 Kostiantyn Danylov aka koder <koder.mail@gmail.com>
#
# This file is part of tiny_cloud library.
#
# tiny_cloud is free software; you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# tiny_cloud is distrubuted in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with tiny_cloud; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
import os
import uuid
import glob
import crypt
import random
import subprocess
import contextlib
try:
import guestfs
no_guestfs = False
except ImportError:
no_guestfs = True
from utils import netsz2netmask, ip2int, int2ip, logger
from common import CloudError
def run(cmd):
return subprocess.check_output(cmd, shell=True)
@contextlib.contextmanager
def make_image(src_fname,
tempo_files_dir,
dst_format,
qcow2_compress=False,
qcow2_preallocate=False,
lvm_dev1=None,
lvm_dev2=None,
delete_on_exit=True):
bstore_raw = None
is_bstore_dev = False
if dst_format == 'qcow2_on_lvm':
bstore_raw = lvm_dev2
is_bstore_dev = True
dst_fname = os.path.join(tempo_files_dir, str(uuid.uuid1()))
dst_format = 'qcow2_on_raw'
is_dst_dev = True
rm_files = [dst_fname]
elif dst_format == 'qcow2_on_raw':
bstore_raw = os.path.join(tempo_files_dir, str(uuid.uuid1()))
dst_fname = os.path.join(tempo_files_dir, str(uuid.uuid1()))
dst_format = 'qcow2_on_raw'
is_dst_dev = True
rm_files = [dst_fname, bstore_raw]
elif dst_format == 'lvm':
dst_format = 'raw'
dst_fname = lvm_dev1
is_dst_dev = True
else:
dst_fname = os.path.join(tempo_files_dir, str(uuid.uuid1()))
rm_files = [dst_fname]
is_dst_dev = False
convert = lambda cmd: \
run("qemu-img convert -f qcow2 -O {0} {1} {2}".format(\
cmd, src_fname, dst_fname))
opts = ""
if qcow2_preallocate:
assert dst_format in ('qcow2', 'qcow2_on_raw', 'qcow2_on_qcow2')
opts = " -o preallocation=metadata "
if qcow2_compress:
assert dst_format in ('qcow2', 'qcow2_on_raw', 'qcow2_on_qcow2')
opts = opts + " -c "
if dst_format == 'qcow2':
run('cp {0} {1}'.format(src_fname, dst_fname))
elif dst_format == 'qcow':
convert('qcow')
elif dst_format == 'raw':
if is_dst_dev:
convert("host_device")
else:
convert("raw")
elif dst_format == 'qcow2_on_qcow2':
run("qemu-img create {0} -f qcow2 -o backing_file={1} {2}".format(opts, src_fname, dst_fname))
elif dst_format == 'qcow2_on_raw':
if is_bstore_dev:
frmt = 'host_device'
else:
frmt = 'raw'
run("qemu-img convert -f qcow2 -O {0} {1} {2}".format(frmt, src_fname, bstore_raw))
run("qemu-img create {0} -f qcow2 -o backing_fmt=raw,backing_file={1} {2}".format(
opts, bstore_raw, dst_fname))
else:
raise RuntimeError("Unknown storage type %r" % (dst_format,))
try:
yield dst_fname
finally:
if delete_on_exit:
map(os.unlink, rm_files)
class LocalGuestFS(object):
def __init__(self, root):
self.root = root
def path(self, rpath):
return os.path.join(self.root, rpath[1:])
def write(self, fname, val):
open(self.path(fname), 'w').write(val)
def read_file(self, fname):
return open(self.path(fname), 'r').read()
def exists(self, path):
return os.path.exists(self.path(path))
def rm(self, path):
path = self.path(path)
if os.path.exists(path):
return os.unlink(path)
def mkdir_p(self, path):
cp = self.root
path_els = path.split(path[1:], '/')
for el in path_els:
cp = os.path.join(cp, el)
os.mkdir(cp)
def prepare_guest(*dt, **mp):
if no_guestfs:
raise CloudError("No libguestfs found. Can't manage vm images")
return prepare_guest_debian(*dt, **mp)
@contextlib.contextmanager
def mount_dimage(image, mdir):
for dev in glob.glob('/dev/nbd*'):
if not os.path.exists('/sys/block/{dev}/pid'.format(dev.split('/')[-1])):
break
if os.path.exists('/sys/block/{dev}/pid'.format(dev.split('/')[-1])):
msg = "Can't found free nbd device"
logger.error(msg)
raise CloudError(msg)
cmd = "qemu-nbd -c {dev} {img}".format(dev=dev, image=image)
subprocess.check_call(cmd, shell=True)
try:
for pdev in glob.glob(dev + 'p*'):
subprocess.check_call('mount {dev} {mdir}'.format(dev=dev, mdir=mdir))
if os.path.exists(os.path.isdir(mdir, 'etc')):
break
subprocess.check_call("umount " + pdev)
if not os.path.exists(os.path.isdir(mdir, 'etc')):
raise CloudError("Can't found root partition in file " + image)
try:
yield LocalGuestFS(mdir)
finally:
subprocess.call('umount ' + pdev)
finally:
subprocess.check_call("qemu-nbd -d " + dev, shell=True)
ifconfig_script = \
"""
description "startup ifconfig"
start on filesystem or runlevel [2345]
pre-start script
{0}
end script
exec ps
"""
# eth_devs => {'eth0' : (hw, ip/'dhcp', sz/None, gw/None)}
# passwords => {login:passwd}
def prepare_guest_debian(disk_path, hostname, passwords, eth_devs, format=None, apt_proxy_ip=None):
logger.info("Prepare image for " + hostname)
if format == 'lxc':
gfs = LocalGuestFS(disk_path)
gfs.rm('/etc/init/udev.conf')
interfaces = []
for dev, (hw, ip, sz, gw) in eth_devs.items():
if ip == 'dhcp':
interfaces.append("dhclient {0}".format(dev))
else:
interfaces.append("ifconfig {0} {1}/{2} up".format(dev, ip, sz))
gfs.write('/etc/init/lxc_lan.conf', ifconfig_script.format("\n".join(interfaces)))
else:
gfs = guestfs.GuestFS()
gfs.add_drive_opts(disk_path, format=format)
logger.debug("Launch libguestfs vm")
gfs.launch()
logger.debug("ok")
os_devs = gfs.inspect_os()
if len(os_devs) > 1:
msg = "Two or more bootable partitions - disk prepare impossible " + disk_path
logger.error(msg)
raise CloudError(msg)
# for dev, fs_type in gfs.list_filesystems():
# logger.debug("Fount partition {0} with fs type {1}".format(dev, fs_type))
# # TODO: add lvm support
# if fs_type in 'ext2 ext3 reiserfs3 reiserfs4 xfs jfs btrfs':
# gfs.mount(dev, '/')
# if gfs.exists('/etc'):
# logger.debug("Fount /etc on partition {0} - will work on it".format(dev))
# break
# gfs.umount(dev)
# logger.debug("No /etc dir found - continue")
if 0 == len(os_devs):
mounts = sorted(gfs.inspect_get_mountpoints(os_devs[0]))
for mpoint, dev in mounts:
gfs.mount(dev, mpoint)
if not gfs.exists('/etc'):
msg = "Can't fount /etc dir in image " + disk_path
logger.error(msg)
raise CloudError(msg)
else:
gfs.mount(os_devs[0], '/')
#gfs.mount('/dev/vda1', '/')
if not gfs.exists('/etc'):
msg = "Can't fount /etc dir in image " + disk_path
logger.error(msg)
raise CloudError(msg)
logger.debug("Launch ok. Set hostname")
#hostname
gfs.write('/etc/hostname', hostname)
#set device names
logger.debug("Set device names and network imterfaces")
templ = 'SUBSYSTEM=="net", DRIVERS=="?*", ATTR{{address}}=="{hw}", NAME="{name}"'
rules_fc = []
interfaces = ["auto lo\niface lo inet loopback"]
for dev, (hw, ip, sz, gw) in eth_devs.items():
rules_fc.append(templ.format(hw=hw, name=dev))
interfaces.append("auto " + dev)
if ip == 'dhcp':
interfaces.append("iface {0} inet dhcp".format(dev))
else:
interfaces.append("iface {0} inet static".format(dev))
interfaces.append(" address " + ip)
network = int2ip(ip2int(ip) & ip2int(netsz2netmask(sz)))
interfaces.append(" network " + network)
interfaces.append(" netmask " + netsz2netmask(sz))
gfs.write('/etc/udev/rules.d/70-persistent-net.rules', "\n".join(rules_fc))
# gfs.write('/etc/network/interfaces', "\n".join(interfaces))
gfs.write('/etc/network/interfaces.d/eth0', "\n".join(interfaces))
# update passwords
logger.debug("Update passwords")
chars = "".join(chr(i) for i in range(ord('a'), ord('z') + 1))
chars += "".join(chr(i) for i in range(ord('A'), ord('Z') + 1))
chars += "".join(chr(i) for i in range(ord('0'), ord('9') + 1))
hashes = {}
for login, passwd in passwords.items():
salt = "".join(random.choice(chars) for _ in range(8))
hashes[login] = crypt.crypt(passwd, "$6$" + salt)
new_shadow = []
need_logins = set(hashes)
for ln in gfs.read_file('/etc/shadow').split('\n'):
ln = ln.strip()
if ln != '' and ln[0] != '#':
login = ln.split(':', 1)[0]
if login in hashes:
sh_templ = "{login}:{hash}:{rest}"
sh_line = sh_templ.format(login=login,
hash=hashes[login],
rest=ln.split(':', 2)[2])
new_shadow.append(sh_line)
need_logins.remove(login)
else:
new_shadow.append(ln)
for login in need_logins:
new_sh_templ = "{login}:{hash}:{rest}"
new_sh_line = new_sh_templ.format(login=login,
hash=hashes[login],
rest="0:0:99999:7:::")
new_shadow.append(new_sh_line)
gfs.write('/etc/shadow', "\n".join(new_shadow))
# add new users to passwd
ids = []
logins = []
passwd = gfs.read_file('/etc/passwd')
for ln in passwd.split('\n'):
ln = ln.strip()
if ln != '' and ln[0] != '#':
logins.append(ln.split(':', 1)[0])
ids.append(ln.split(':')[2])
ids.append(ln.split(':')[3])
add_lines = []
try:
mid = max(i for i in ids if i < 65000)
except ValueError:
mid = 0
mid += 1024
for login in set(hashes) - set(logins):
home = '/home/' + login
add_lines.append(":".join([login, 'x', str(mid), str(mid), "", home, '/bin/bash']))
if not gfs.exists(home):
gfs.mkdir_p(home)
mid += 1
if add_lines != []:
gfs.write('/etc/passwd', passwd.rstrip() + "\n" + "\n".join(add_lines))
# if apt_proxy_ip is not None:
# logger.debug("Set apt-proxy to http://{0}:3142".format(apt_proxy_ip))
# fc = 'Acquire::http {{ Proxy "http://{0}:3142"; }};'.format(apt_proxy_ip)
# gfs.write('/etc/apt/apt.conf.d/02proxy', fc)
logger.debug("Update hosts")
hosts = gfs.read_file('/etc/hosts')
new_hosts = ["127.0.0.1 localhost\n127.0.0.1 " + hostname]
for ln in hosts.split('#'):
if not ln.strip().startswith('127.0.0.1'):
new_hosts.append(ln)
gfs.write('/etc/hosts', "\n".join(new_hosts))
# allow ssh passwd auth
if gfs.is_file('/etc/ssh/ssh_config'):
name = '/etc/ssh/ssh_config'
elif gfs.is_file('/etc/ssh/sshd_config'):
name = '/etc/ssh/sshd_config'
else:
logger.warning("Both '/etc/ssh/sshd_config' and '/etc/ssh/ssh_config' are absent. Skip ssh config patching")
name = None
if name is not None:
sshd_conf = gfs.read_file('/etc/ssh/ssh_config')
sshd_conf_lines = sshd_conf.split("\n")
for pos, ln in enumerate(sshd_conf_lines):
if "PasswordAuthentication" in ln:
sshd_conf_lines[pos] = "PasswordAuthentication yes"
break
else:
sshd_conf_lines.append("PasswordAuthentication yes")
gfs.write('/etc/ssh/ssh_config', "\n".join(sshd_conf_lines))