+ # Use chroot to call rpm, b/c the bootimage&nodeimage rpm-versions may not work together
+ try:
+ kversion = os.popen("chroot {} rpm -qa kernel | tail -1 | cut -c 8-"\
+ .format(SYSIMG_PATH)).read().rstrip()
+ major_version = int(kversion[0]) # Check if the string looks like a kernel version
+ except:
+ # Try a different method for non-rpm-based distributions
+ kversion = os.popen("ls -lrt {}/lib/modules | tail -1 | awk '{{print $9;}}'"\
+ .format(SYSIMG_PATH)).read().rstrip()
+
+ # kernel and initrd come in various locations depending on the distro
+
+ kernel_candidates = []
+ kernel_candidates.append("/boot/vmlinux-{}*".format(kversion))
+ # on f23 starting dec. 2015 - what a twisted naming scheme
+ kernel_candidates.append("/boot/*/{}*/initrd".format(kversion))
+
+ initrd_candidates = []
+ # f16/18: expect initramfs image here
+ initrd_candidates.append ("/boot/initramfs-{}.img".format(kversion))
+ # f20: uses a uid of some kind, e.g. /boot/543f88c129de443baaa65800cf3927ce/<kversion>/initrd
+ initrd_candidates.append ("/boot/*/{}/initrd".format(kversion))
+ # Ubuntu:
+ initrd_candidates.append ("/boot/initrd.img-{}".format(kversion))
+
+ def find_file_in_sysimg (candidates):
+ import glob
+ for pattern in candidates:
+ matches = glob.glob(SYSIMG_PATH+pattern)
+ log.write("locating initrd: found {} matches in {}\n".format(len(matches), pattern))
+ if matches:
+ return matches[0]
+
+ kernel = find_file_in_sysimg(kernel_candidates)
+ if kernel:
+ utils.sysexec("cp {} /tmp/kernel".format(kernel), log)
+ else:
+ raise Exception("Unable to locate kernel - bailing out")
+
+
+ initrd = find_file_in_sysimg(initrd_candidates)
+ if initrd:
+ utils.sysexec("cp {} /tmp/initrd".format(initrd), log)
+ else:
+ raise Exception("Unable to locate initrd - bailing out")
+
+ BootAPI.save(vars)
+
+ log.write("Unmounting disks.\n")
+
+ if (vars['ONE_PARTITION'] != '1'):
+ utils.sysexec("umount {}/vservers".format(SYSIMG_PATH), log)
+ utils.sysexec("umount {}/proc".format(SYSIMG_PATH), log)
+ utils.sysexec_noerr("umount {}/dev".format(SYSIMG_PATH), log)
+ utils.sysexec_noerr("umount {}/sys".format(SYSIMG_PATH), log)
+ utils.sysexec("umount {}".format(SYSIMG_PATH), log)
+ utils.sysexec("vgchange -an", log)
+
+ ROOT_MOUNTED = 0
+ vars['ROOT_MOUNTED'] = 0
+
+ # Change runlevel to 'boot' prior to kexec.
+ StopRunlevelAgent.Run(vars, log)
+
+ log.write("Unloading modules and chain booting to new kernel.\n")