-
Notifications
You must be signed in to change notification settings - Fork 292
/
kernel.py
1157 lines (1049 loc) · 42.1 KB
/
kernel.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Kernel installation task
"""
from cStringIO import StringIO
import logging
import os
import re
import shlex
import urllib2
import urlparse
import ast
from teuthology import misc as teuthology
from ..orchestra import run
from ..config import config as teuth_config
from ..exceptions import UnsupportedPackageTypeError, ConfigError
log = logging.getLogger(__name__)
def normalize_config(ctx, config):
"""
Returns a config whose keys are all real roles.
Generic roles (client, mon, osd, etc.) are replaced with
the actual roles (client.0, client.1, etc.). If the config
specifies a different version for a specific role, this is
unchanged.
For example, with 4 OSDs this::
osd:
tag: v3.0
kdb: true
osd.1:
branch: new_btrfs
kdb: false
osd.3:
deb: /path/to/linux-whatever.deb
is transformed into::
osd.0:
tag: v3.0
kdb: true
osd.1:
branch: new_btrfs
kdb: false
osd.2:
tag: v3.0
kdb: true
osd.3:
deb: /path/to/linux-whatever.deb
If config is None or just specifies a version to use,
it is applied to all nodes.
:param ctx: Context
:param config: Configuration
"""
if config is None or \
len(filter(lambda x: x in ['tag', 'branch', 'sha1', 'kdb',
'deb', 'rpm', 'brew'],
config.keys())) == len(config.keys()):
new_config = {}
if config is None:
config = {'branch': 'master'}
for _, roles_for_host in ctx.cluster.remotes.iteritems():
new_config[roles_for_host[0]] = config
return new_config
new_config = {}
for role, role_config in config.iteritems():
if role_config is None:
role_config = {'branch': 'master'}
if '.' in role:
new_config[role] = role_config
else:
for id_ in teuthology.all_roles_of_type(ctx.cluster, role):
name = '{type}.{id}'.format(type=role, id=id_)
# specific overrides generic
if name not in config:
new_config[name] = role_config
return new_config
def validate_config(ctx, config):
"""
Make sure that all kernels in the list of remove kernels
refer to the same kernel.
:param ctx: Context
:param config: Configuration
"""
for _, roles_for_host in ctx.cluster.remotes.iteritems():
kernel = None
for role in roles_for_host:
role_kernel = config.get(role, kernel)
if kernel is None:
kernel = role_kernel
elif role_kernel is not None:
assert kernel == role_kernel, \
"everything on the same host must use the same kernel"
if role in config:
del config[role]
def _vsplitter(version):
"""Kernels from Calxeda are named ...ceph-<sha1>...highbank.
Kernels that we generate are named ...-g<sha1>.
This routine finds the text in front of the sha1 that is used by
need_to_install() to extract information from the kernel name.
:param version: Name of the kernel
"""
if version.endswith('highbank'):
return 'ceph-'
return '-g'
def need_to_install(ctx, role, version):
"""
Check to see if we need to install a kernel. Get the version of the
currently running kernel, and compare it against the value passed in.
:param ctx: Context
:param role: Role
:param version: value to compare against (used in checking), can be either
a utsrelease string (e.g. '3.13.0-rc3-ceph-00049-ge2817b3')
or a sha1.
"""
ret = True
log.info('Checking kernel version of {role}, want {ver}...'.format(
role=role, ver=version))
uname_fp = StringIO()
ctx.cluster.only(role).run(
args=[
'uname',
'-r',
],
stdout=uname_fp,
)
cur_version = uname_fp.getvalue().rstrip('\n')
log.debug('current kernel version is {ver}'.format(ver=cur_version))
if '.' in version:
# version is utsrelease, yay
if cur_version == version:
log.debug('utsrelease strings match, do not need to install')
ret = False
else:
# version is sha1, need to try to extract sha1 from cur_version
splt = _vsplitter(cur_version)
if splt in cur_version:
_, cur_sha1 = cur_version.rsplit(splt, 1)
dloc = cur_sha1.find('-')
if dloc > 0:
cur_sha1 = cur_sha1[0:dloc]
log.debug('extracting sha1, {ver} -> {sha1}'.format(
ver=cur_version, sha1=cur_sha1))
# FIXME: The above will match things like ...-generic on Ubuntu
# distro kernels resulting in 'eneric' cur_sha1.
m = min(len(cur_sha1), len(version))
assert m >= 6, "cur_sha1 and/or version is too short, m = %d" % m
if cur_sha1[0:m] == version[0:m]:
log.debug('extracted sha1 matches, do not need to install')
ret = False
else:
log.debug('failed to parse current kernel version')
uname_fp.close()
return ret
def install_firmware(ctx, config):
"""
Go to the github to get the latest firmware.
:param ctx: Context
:param config: Configuration
"""
linux_firmware_git_upstream = 'git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git'
uri = teuth_config.linux_firmware_git_url or linux_firmware_git_upstream
fw_dir = '/lib/firmware/updates'
for role in config.iterkeys():
if isinstance(config[role], str) and config[role].find('distro') >= 0:
log.info('Skipping firmware on distro kernel');
return
(role_remote,) = ctx.cluster.only(role).remotes.keys()
package_type = teuthology.get_system_type(role_remote)
if package_type == 'rpm':
return
log.info('Installing linux-firmware on {role}...'.format(role=role))
role_remote.run(
args=[
# kludge around mysterious 0-byte .git/HEAD files
'cd', fw_dir,
run.Raw('&&'),
'test', '-d', '.git',
run.Raw('&&'),
'test', '!', '-s', '.git/HEAD',
run.Raw('&&'),
'sudo', 'rm', '-rf', '.git',
run.Raw(';'),
# init
'sudo', 'install', '-d', '-m0755', fw_dir,
run.Raw('&&'),
'cd', fw_dir,
run.Raw('&&'),
'sudo', 'git', 'init',
],
)
role_remote.run(
args=[
'sudo', 'git', '--git-dir=%s/.git' % fw_dir, 'config',
'--get', 'remote.origin.url', run.Raw('>/dev/null'),
run.Raw('||'),
'sudo', 'git', '--git-dir=%s/.git' % fw_dir,
'remote', 'add', 'origin', uri,
],
)
role_remote.run(
args=[
'cd', fw_dir,
run.Raw('&&'),
'sudo', 'git', 'fetch', 'origin',
run.Raw('&&'),
'sudo', 'git', 'reset', '--hard', 'origin/master'
],
)
def gitbuilder_pkg_name(remote):
if remote.os.package_type == 'rpm':
pkg_name = 'kernel.x86_64.rpm'
elif remote.os.package_type == 'deb':
pkg_name = 'linux-image.deb'
else:
raise UnsupportedPackageTypeError(remote)
return pkg_name
def remote_pkg_path(remote):
"""
This is where kernel packages are copied over (in case of local
packages) or downloaded to (in case of gitbuilder packages) and
then installed from.
"""
return os.path.join('/tmp', gitbuilder_pkg_name(remote))
def download_kernel(ctx, config):
"""
Supply each remote with a kernel package:
- local kernels are copied over
- gitbuilder kernels are downloaded
- nothing is done for distro kernels
:param ctx: Context
:param config: Configuration
"""
procs = {}
for role, src in config.iteritems():
needs_download = False
if src == 'distro':
# don't need to download distro kernels
log.debug("src is distro, skipping download");
continue
(role_remote,) = ctx.cluster.only(role).remotes.keys()
if isinstance(src, dict):
# we're downloading a kernel from brew, the src dict here
# is the build_info retrieved from brew using koji
build_id = src["id"]
log.info("Downloading kernel with build_id {build_id} on {role}...".format(
build_id=build_id,
role=role
))
needs_download = True
baseurl = "{brewroot}/kernel/{ver}/{rel}/x86_64/".format(
brewroot="http://download.devel.redhat.com/brewroot/packages",
ver=src["version"],
rel=src["release"],
)
pkg_name = "kernel-{ver}-{rel}.x86_64.rpm".format(
ver=src["version"],
rel=src["release"],
)
elif src.find('/') >= 0:
# local package - src is path
log.info('Copying kernel package {path} to {role}...'.format(
path=src, role=role))
f = open(src, 'r')
proc = role_remote.run(
args=[
'python', '-c',
'import shutil, sys; shutil.copyfileobj(sys.stdin, file(sys.argv[1], "wb"))',
remote_pkg_path(role_remote),
],
wait=False,
stdin=f
)
procs[role_remote.name] = proc
else:
# gitbuilder package - src is sha1
log.info('Downloading kernel {sha1} on {role}...'.format(sha1=src,
role=role))
needs_download = True
package_type = role_remote.os.package_type
if package_type == 'rpm':
system_type, system_ver = teuthology.get_system_type(
role_remote, distro=True, version=True)
if '.' in system_ver:
system_ver = system_ver.split('.')[0]
ldist = '{system_type}{system_ver}'.format(
system_type=system_type, system_ver=system_ver)
larch = 'x86_64'
elif package_type == 'deb':
ldist, larch = role_remote.os.codename, role_remote.arch
else:
raise UnsupportedPackageTypeError(role_remote)
_, baseurl = teuthology.get_ceph_binary_url(
package='kernel',
sha1=src,
format=package_type,
flavor='basic',
arch=larch,
dist=ldist,
)
pkg_name = gitbuilder_pkg_name(role_remote)
log.info("fetching, gitbuilder baseurl is %s", baseurl)
if needs_download:
proc = role_remote.run(
args=[
'rm', '-f', remote_pkg_path(role_remote),
run.Raw('&&'),
'echo',
pkg_name,
run.Raw('|'),
'wget',
'-nv',
'-O',
remote_pkg_path(role_remote),
'--base={url}'.format(url=baseurl),
'--input-file=-',
],
wait=False)
procs[role_remote.name] = proc
for name, proc in procs.iteritems():
log.debug('Waiting for download/copy to %s to complete...', name)
proc.wait()
def _no_grub_link(in_file, remote, kernel_ver):
"""
Copy and link kernel related files if grub cannot be used
(as is the case in Arm kernels)
:param infile: kernel file or image file to be copied.
:param remote: remote machine
:param kernel_ver: kernel version
"""
boot1 = '/boot/%s' % in_file
boot2 = '%s.old' % boot1
remote.run(
args=[
'if', 'test', '-e', boot1, run.Raw(';'), 'then',
'sudo', 'mv', boot1, boot2, run.Raw(';'), 'fi',],
)
remote.run(
args=['sudo', 'ln', '-s', '%s-%s' % (in_file, kernel_ver) , boot1, ],
)
def install_and_reboot(ctx, config):
"""
Install and reboot the kernel. This mostly performs remote
installation operations. The code does check for Arm images
and skips grub operations if the kernel is Arm. Otherwise, it
extracts kernel titles from submenu entries and makes the appropriate
grub calls. The assumptions here are somewhat simplified in that
it expects kernel entries to be present under submenu entries.
:param ctx: Context
:param config: Configuration
"""
procs = {}
kernel_title = ''
for role, src in config.iteritems():
(role_remote,) = ctx.cluster.only(role).remotes.keys()
if isinstance(src, str) and src.find('distro') >= 0:
log.info('Installing distro kernel on {role}...'.format(role=role))
install_kernel(role_remote)
continue
log.info('Installing kernel {src} on {role}...'.format(src=src,
role=role))
system_type = teuthology.get_system_type(role_remote)
if system_type == 'rpm':
proc = role_remote.run(
args=[
'sudo',
'rpm',
'-ivh',
'--oldpackage',
'--replacefiles',
'--replacepkgs',
remote_pkg_path(role_remote),
])
install_kernel(role_remote, remote_pkg_path(role_remote))
continue
# TODO: Refactor this into install_kernel() so that it handles all
# cases for both rpm and deb packages.
proc = role_remote.run(
args=[
# install the kernel deb
'sudo',
'dpkg',
'-i',
remote_pkg_path(role_remote),
],
)
# collect kernel image name from the .deb
kernel_title = get_image_version(role_remote,
remote_pkg_path(role_remote))
log.info('searching for kernel {}'.format(kernel_title))
if kernel_title.endswith("-highbank"):
_no_grub_link('vmlinuz', role_remote, kernel_title)
_no_grub_link('initrd.img', role_remote, kernel_title)
proc = role_remote.run(
args=[
'sudo',
'shutdown',
'-r',
'now',
],
wait=False,
)
procs[role_remote.name] = proc
continue
# look for menuentry for our kernel, and collect any
# submenu entries for their titles. Assume that if our
# kernel entry appears later in the file than a submenu entry,
# it's actually nested under that submenu. If it gets more
# complex this will totally break.
cmdout = StringIO()
proc = role_remote.run(
args=[
'egrep',
'(submenu|menuentry.*' + kernel_title + ').*{',
'/boot/grub/grub.cfg'
],
stdout = cmdout,
)
submenu_title = ''
default_title = ''
for l in cmdout.getvalue().split('\n'):
fields = shlex.split(l)
if len(fields) >= 2:
command, title = fields[:2]
if command == 'submenu':
submenu_title = title + '>'
if command == 'menuentry':
if title.endswith(kernel_title):
default_title = title
break
cmdout.close()
log.info('submenu_title:{}'.format(submenu_title))
log.info('default_title:{}'.format(default_title))
proc = role_remote.run(
args=[
# use the title(s) to construct the content of
# the grub menu entry, so we can default to it.
'/bin/echo',
'-e',
r'cat <<EOF\nset default="' + submenu_title + \
default_title + r'"\nEOF\n',
# make it look like an emacs backup file so
# unfortunately timed update-grub runs don't pick it
# up yet; use sudo tee so we are able to write to /etc
run.Raw('|'),
'sudo',
'tee',
'--',
'/etc/grub.d/01_ceph_kernel.tmp~',
run.Raw('>/dev/null'),
run.Raw('&&'),
'sudo',
'chmod',
'a+x',
'--',
'/etc/grub.d/01_ceph_kernel.tmp~',
run.Raw('&&'),
'sudo',
'mv',
'--',
'/etc/grub.d/01_ceph_kernel.tmp~',
'/etc/grub.d/01_ceph_kernel',
# update grub again so it accepts our default
run.Raw('&&'),
'sudo',
'update-grub',
run.Raw('&&'),
'rm',
remote_pkg_path(role_remote),
run.Raw('&&'),
'sudo',
'shutdown',
'-r',
'now',
],
wait=False,
)
procs[role_remote.name] = proc
for name, proc in procs.iteritems():
log.debug('Waiting for install on %s to complete...', name)
proc.wait()
def enable_disable_kdb(ctx, config):
"""
Enable kdb on remote machines in use. Disable on those that are
not in use.
:param ctx: Context
:param config: Configuration
"""
for role, enable in config.iteritems():
(role_remote,) = ctx.cluster.only(role).remotes.keys()
if "mira" in role_remote.name:
serialdev = "ttyS2"
else:
serialdev = "ttyS1"
if enable:
log.info('Enabling kdb on {role}...'.format(role=role))
try:
role_remote.run(
args=[
'echo', serialdev,
run.Raw('|'),
'sudo', 'tee', '/sys/module/kgdboc/parameters/kgdboc'
])
except run.CommandFailedError:
log.warn('Kernel does not support kdb')
else:
log.info('Disabling kdb on {role}...'.format(role=role))
# Add true pipe so command doesn't fail on kernel without kdb support.
try:
role_remote.run(
args=[
'echo', '',
run.Raw('|'),
'sudo', 'tee', '/sys/module/kgdboc/parameters/kgdboc',
run.Raw('|'),
'true',
])
except run.CommandFailedError:
log.warn('Kernel does not support kdb')
def wait_for_reboot(ctx, need_install, timeout, distro=False):
"""
Loop reconnecting and checking kernel versions until
they're all correct or the timeout is exceeded.
:param ctx: Context
:param need_install: list of packages that we need to reinstall.
:param timeout: number of second before we timeout.
"""
import time
starttime = time.time()
while need_install:
teuthology.reconnect(ctx, timeout)
for client in need_install.keys():
if 'distro' in need_install[client]:
distro = True
log.info('Checking client {client} for new kernel version...'.format(client=client))
try:
if distro:
assert not need_to_install_distro(ctx, client), \
'failed to install new distro kernel version within timeout'
else:
assert not need_to_install(ctx, client, need_install[client]), \
'failed to install new kernel version within timeout'
del need_install[client]
except Exception:
log.exception("Saw exception")
# ignore connection resets and asserts while time is left
if time.time() - starttime > timeout:
raise
time.sleep(1)
def need_to_install_distro(ctx, role):
"""
Installing kernels on rpm won't setup grub/boot into them.
This installs the newest kernel package and checks its version
and compares against current (uname -r) and returns true if newest != current.
Similar check for deb.
"""
(role_remote,) = ctx.cluster.only(role).remotes.keys()
system_type = teuthology.get_system_type(role_remote)
output, err_mess = StringIO(), StringIO()
role_remote.run(args=['uname', '-r' ], stdout=output, stderr=err_mess )
current = output.getvalue().strip()
if system_type == 'rpm':
role_remote.run(args=['sudo', 'yum', 'install', '-y', 'kernel'], stdout=output, stderr=err_mess )
if 'Nothing to do' in output.getvalue():
output.truncate(0), err_mess.truncate(0)
role_remote.run(args=['echo', 'no', run.Raw('|'), 'sudo', 'yum', 'reinstall', 'kernel', run.Raw('||'), 'true'], stdout=output, stderr=err_mess )
if 'Skipping the running kernel' in err_mess.getvalue():
# Current running kernel is already newest and updated
log.info('Newest distro kernel already installed/running')
return False
else:
output.truncate(0), err_mess.truncate(0)
role_remote.run(args=['sudo', 'yum', 'reinstall', '-y', 'kernel', run.Raw('||'), 'true'], stdout=output, stderr=err_mess )
#reset stringIO output.
output.truncate(0), err_mess.truncate(0)
role_remote.run(args=['rpm', '-q', 'kernel', '--last' ], stdout=output, stderr=err_mess )
for kernel in output.getvalue().split():
if kernel.startswith('kernel'):
if 'ceph' not in kernel:
newest = kernel.split('kernel-')[1]
break
if system_type == 'deb':
distribution = teuthology.get_system_type(role_remote, distro=True)
newest = get_latest_image_version_deb(role_remote, distribution)
output.close()
err_mess.close()
if current in newest:
return False
log.info('Not newest distro kernel. Curent: {cur} Expected: {new}'.format(cur=current, new=newest))
return True
def maybe_generate_initrd_rpm(remote, path, version):
"""
Generate initrd with mkinitrd if the hooks that should make it
happen on its own aren't there.
:param path: rpm package path
:param version: kernel version to generate initrd for
e.g. 3.18.0-rc6-ceph-00562-g79a9fa5
"""
proc = remote.run(
args=[
'rpm',
'--scripts',
'-qp',
path,
],
stdout=StringIO())
out = proc.stdout.getvalue()
if 'bin/installkernel' in out or 'bin/kernel-install' in out:
return
log.info("No installkernel or kernel-install hook in %s, "
"will generate initrd for %s", path, version)
remote.run(
args=[
'sudo',
'mkinitrd',
'--allow-missing',
'-f', # overwrite existing initrd
'/boot/initramfs-' + version + '.img',
version,
])
def install_kernel(remote, path=None):
"""
A bit of misnomer perhaps - the actual kernel package is installed
elsewhere, this function deals with initrd and grub. Currently the
following cases are handled:
- local, gitbuilder, distro for rpm packages
- distro for deb packages - see TODO in install_and_reboot()
TODO: reboots should be issued from install_and_reboot()
:param path: package path (for local and gitbuilder cases)
"""
system_type = teuthology.get_system_type(remote)
if system_type == 'rpm':
if path:
version = get_image_version(remote, path)
# This is either a gitbuilder or a local package and both of these
# could have been built with upstream rpm targets with specs that
# don't have a %post section at all, which means no initrd.
maybe_generate_initrd_rpm(remote, path, version)
else:
version = get_latest_image_version_rpm(remote)
update_grub_rpm(remote, version)
remote.run( args=['sudo', 'shutdown', '-r', 'now'], wait=False )
return
if system_type == 'deb':
distribution = teuthology.get_system_type(remote, distro=True)
newversion = get_latest_image_version_deb(remote, distribution)
if 'ubuntu' in distribution:
grub2conf = teuthology.get_file(remote, '/boot/grub/grub.cfg', True)
submenu = ''
menuentry = ''
for line in grub2conf.split('\n'):
if 'submenu' in line:
submenu = line.split('submenu ')[1]
# Ubuntu likes to be sneaky and change formatting of
# grub.cfg between quotes/doublequotes between versions
if submenu.startswith("'"):
submenu = submenu.split("'")[1]
if submenu.startswith('"'):
submenu = submenu.split('"')[1]
if 'menuentry' in line:
if newversion in line and 'recovery' not in line:
menuentry = line.split('\'')[1]
break
if submenu:
grubvalue = submenu + '>' + menuentry
else:
grubvalue = menuentry
grubfile = 'cat <<EOF\nset default="' + grubvalue + '"\nEOF'
teuthology.delete_file(remote, '/etc/grub.d/01_ceph_kernel', sudo=True, force=True)
teuthology.sudo_write_file(remote, '/etc/grub.d/01_ceph_kernel', StringIO(grubfile), '755')
log.info('Distro Kernel Version: {version}'.format(version=newversion))
remote.run(args=['sudo', 'update-grub'])
remote.run(args=['sudo', 'shutdown', '-r', 'now'], wait=False )
return
if 'debian' in distribution:
grub2_kernel_select_generic(remote, newversion, 'deb')
log.info('Distro Kernel Version: {version}'.format(version=newversion))
remote.run( args=['sudo', 'shutdown', '-r', 'now'], wait=False )
return
def update_grub_rpm(remote, newversion):
"""
Updates grub file to boot new kernel version on both legacy grub/grub2.
"""
grub='grub2'
# Check if grub2 is isntalled
try:
remote.run(args=['sudo', 'rpm', '-qi', 'grub2'])
except Exception:
grub = 'legacy'
log.info('Updating Grub Version: {grub}'.format(grub=grub))
if grub == 'legacy':
data = ''
#Write new legacy grub entry.
newgrub = generate_legacy_grub_entry(remote, newversion)
for line in newgrub:
data += line + '\n'
temp_file_path = remote.mktemp()
teuthology.sudo_write_file(remote, temp_file_path, StringIO(data), '755')
teuthology.move_file(remote, temp_file_path, '/boot/grub/grub.conf', True)
else:
#Update grub menu entry to new version.
grub2_kernel_select_generic(remote, newversion, 'rpm')
def grub2_kernel_select_generic(remote, newversion, ostype):
"""
Can be used on DEB and RPM. Sets which entry should be boted by entrynum.
"""
if ostype == 'rpm':
grubset = 'grub2-set-default'
mkconfig = 'grub2-mkconfig'
grubconfig = '/boot/grub2/grub.cfg'
if ostype == 'deb':
grubset = 'grub-set-default'
grubconfig = '/boot/grub/grub.cfg'
mkconfig = 'grub-mkconfig'
remote.run(args=['sudo', mkconfig, '-o', grubconfig, ])
grub2conf = teuthology.get_file(remote, grubconfig, True)
entry_num = 0
for line in grub2conf.split('\n'):
if line.startswith('menuentry'):
if newversion in line:
break
entry_num += 1
remote.run(args=['sudo', grubset, str(entry_num), ])
def generate_legacy_grub_entry(remote, newversion):
"""
This will likely need to be used for ceph kernels as well
as legacy grub rpm distros don't have an easy way of selecting
a kernel just via a command. This generates an entry in legacy
grub for a new kernel version using the existing entry as a base.
"""
grubconf = teuthology.get_file(remote, '/boot/grub/grub.conf', True)
titleline = ''
rootline = ''
kernelline = ''
initline = ''
kernelversion = ''
linenum = 0
titlelinenum = 0
#Grab first kernel entry (title/root/kernel/init lines)
for line in grubconf.split('\n'):
if re.match('^title', line):
titleline = line
titlelinenum = linenum
if re.match('(^\s+)root', line):
rootline = line
if re.match('(^\s+)kernel', line):
kernelline = line
for word in line.split(' '):
if 'vmlinuz' in word:
kernelversion = word.split('vmlinuz-')[-1]
if re.match('(^\s+)initrd', line):
initline = line
if (kernelline != '') and (initline != ''):
break
else:
linenum += 1
#insert new entry into grubconfnew list:
linenum = 0
newgrubconf = []
for line in grubconf.split('\n'):
line = line.rstrip('\n')
if linenum == titlelinenum:
newtitle = re.sub(kernelversion, newversion, titleline)
newroot = re.sub(kernelversion, newversion, rootline)
newkernel = re.sub(kernelversion, newversion, kernelline)
newinit = re.sub(kernelversion, newversion, initline)
newgrubconf.append(newtitle)
newgrubconf.append(newroot)
newgrubconf.append(newkernel)
newgrubconf.append(newinit)
newgrubconf.append('')
newgrubconf.append(line)
else:
newgrubconf.append(line)
linenum += 1
return newgrubconf
def get_image_version(remote, path):
"""
Get kernel image version from (rpm or deb) package.
:param path: (rpm or deb) package path
"""
if remote.os.package_type == 'rpm':
proc = remote.run(
args=[
'rpm',
'-qlp',
path
],
stdout=StringIO())
elif remote.os.package_type == 'deb':
proc = remote.run(
args=[
'dpkg-deb',
'-c',
path
],
stdout=StringIO())
else:
raise UnsupportedPackageTypeError(remote)
files = proc.stdout.getvalue()
for file in files.split('\n'):
if '/boot/vmlinuz-' in file:
version = file.split('/boot/vmlinuz-')[1]
break
log.debug("get_image_version: %s", version)
return version
def get_latest_image_version_rpm(remote):
"""
Get kernel image version of the newest kernel rpm package.
Used for distro case.
"""
proc = remote.run(
args=[
'rpm',
'-q',
'kernel',
'--last', # order by install time
], stdout=StringIO())
out = proc.stdout.getvalue()
version = out.split()[0].split('kernel-')[1]
log.debug("get_latest_image_version_rpm: %s", version)
return version
def get_latest_image_version_deb(remote, ostype):
"""
Get kernel image version of the newest kernel deb package.
Used for distro case.
Round-about way to get the newest kernel uname -r compliant version string
from the virtual package which is the newest kenel for debian/ubuntu.
"""
output, err_mess = StringIO(), StringIO()
newest=''
#Depend of virtual package has uname -r output in package name. Grab that.
if 'debian' in ostype:
remote.run(args=['sudo', 'apt-get', '-y', 'install', 'linux-image-amd64' ], stdout=output, stderr=err_mess )
remote.run(args=['dpkg', '-s', 'linux-image-amd64' ], stdout=output, stderr=err_mess )
for line in output.getvalue().split('\n'):
if 'Depends:' in line:
newest = line.split('linux-image-')[1]
output.close()
err_mess.close()
return newest
#Ubuntu is a depend in a depend.
if 'ubuntu' in ostype:
try:
remote.run(args=['sudo', 'apt-get', '-y', 'install', 'linux-image-current-generic' ], stdout=output, stderr=err_mess )
remote.run(args=['dpkg', '-s', 'linux-image-current-generic' ], stdout=output, stderr=err_mess )
for line in output.getvalue().split('\n'):
if 'Depends:' in line:
depends = line.split('Depends: ')[1]
remote.run(args=['dpkg', '-s', depends ], stdout=output, stderr=err_mess )
except run.CommandFailedError:
# Non precise ubuntu machines (like trusty) don't have
# linux-image-current-generic so use linux-image-generic instead.
remote.run(args=['sudo', 'apt-get', '-y', 'install', 'linux-image-generic' ], stdout=output, stderr=err_mess )
remote.run(args=['dpkg', '-s', 'linux-image-generic' ], stdout=output, stderr=err_mess )
for line in output.getvalue().split('\n'):
if 'Depends:' in line:
newest = line.split('linux-image-')[1]
if ',' in newest:
newest = newest.split(',')[0]
output.close()
err_mess.close()
return newest
def get_sha1_from_pkg_name(path):
"""
Get commit hash (min 7 max 40 chars) from (rpm or deb) package name.
Sample basenames of "make deb-pkg" and "make rpm-pkg" packages
linux-image-3.10.0-ceph-rhdeb-00050-g687d1a5f0083_3.10.0-ceph-rhdeb-00050-g687d1a5f0083-6_amd64.deb
kernel-3.10.0_ceph_rhrpm_00050_g687d1a5f0083-8.x86_64.rpm
Make sure kernel was built with CONFIG_LOCALVERSION_AUTO=y.
:param path: (rpm or deb) package path (only basename is used)
"""
basename = os.path.basename(path)
match = re.search('\d+[-_]g([0-9a-f]{7,40})', basename)
sha1 = match.group(1) if match else None
log.debug("get_sha1_from_pkg_name: %s -> %s -> %s", path, basename, sha1)
return sha1
def task(ctx, config):
"""
Make sure the specified kernel is installed.
This can be a branch, tag, or sha1 of ceph-client.git or a local
kernel package.
To install ceph-client.git branch (default: master)::
kernel:
branch: testing
To install ceph-client.git tag::
kernel:
tag: v3.18
To install ceph-client.git sha1::
kernel:
sha1: 275dd19ea4e84c34f985ba097f9cddb539f54a50
To install local rpm (target should be an rpm system)::
kernel:
rpm: /path/to/appropriately-named.rpm
To install local deb (target should be a deb system)::
kernel:
deb: /path/to/appropriately-named.deb
For rpm: or deb: to work it should be able to figure out sha1 from
local kernel package basename, see get_sha1_from_pkg_name(). This
means that you can't for example install a local tag - package built
with upstream {rpm,deb}-pkg targets won't have a sha1 in its name.
If you want to schedule a run and use a local kernel package, you
have to copy the package over to a box teuthology workers are
running on and specify a path to the package on that box.
All of the above will install a specified kernel on all targets.
You can specify different kernels for each role or for all roles of
a certain type (more specific roles override less specific, see
normalize_config() for details)::
kernel:
client:
tag: v3.0
osd:
branch: btrfs_fixes
client.1: