3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
17 import notify_messages
20 from Exceptions import BootManagerException
22 import UpdateNodeConfiguration
23 import StopRunlevelAgent
28 Load the kernel off of a node and boot to it.
29 This step assumes the disks are mounted on SYSIMG_PATH.
30 If successful, this function will not return. If it returns, no chain
33 Expect the following variables:
34 SYSIMG_PATH the path where the system image will be mounted
35 (always starts with TEMP_PATH)
36 ROOT_MOUNTED the node root file system is mounted
37 NODE_SESSION the unique session val set when we requested
38 the current boot state
39 PLCONF_DIR The directory to store PL configuration files in
41 Sets the following variables:
42 ROOT_MOUNTED the node root file system is mounted
45 log.write("\n\nStep: Chain booting node.\n")
47 # make sure we have the variables we need
49 SYSIMG_PATH = vars["SYSIMG_PATH"]
51 raise ValueError("SYSIMG_PATH")
53 PLCONF_DIR = vars["PLCONF_DIR"]
55 raise ValueError("PLCONF_DIR")
57 # its ok if this is blank
58 NODE_SESSION = vars["NODE_SESSION"]
60 NODE_MODEL_OPTIONS = vars["NODE_MODEL_OPTIONS"]
62 PARTITIONS = vars["PARTITIONS"]
63 if PARTITIONS == None:
64 raise ValueError("PARTITIONS")
66 except KeyError as var:
67 raise BootManagerException("Missing variable in vars: {}\n".format(var))
68 except ValueError as var:
69 raise BootManagerException("Variable in vars, shouldn't be: {}\n".format(var))
72 if vars.has_key('ROOT_MOUNTED'):
73 ROOT_MOUNTED = vars['ROOT_MOUNTED']
76 log.write("Mounting node partitions\n")
78 # simply creating an instance of this class and listing the system
79 # block devices will make them show up so vgscan can find the planetlab
81 systeminfo.get_block_device_list(vars, log)
83 utils.sysexec("vgscan", log)
84 utils.sysexec("vgchange -ay planetlab", log)
86 utils.makedirs(SYSIMG_PATH)
88 cmd = "mount {} {}".format(PARTITIONS["root"], SYSIMG_PATH)
89 utils.sysexec(cmd, log)
90 cmd = "mount -t proc none {}/proc".format(SYSIMG_PATH)
91 utils.sysexec(cmd, log)
92 cmd = "mount {} {}/vservers".format(PARTITIONS["vservers"], SYSIMG_PATH)
93 utils.sysexec(cmd, log)
96 vars['ROOT_MOUNTED'] = 1
99 # write out the session value /etc/planetlab/session
101 session_file_path = "{}/{}/session".format(SYSIMG_PATH, PLCONF_DIR)
102 session_file = file(session_file_path, "w")
103 session_file.write(str(NODE_SESSION))
106 log.write("Updated /etc/planetlab/session\n")
108 log.write("Unable to write out /etc/planetlab/session, continuing anyway\n")
110 # update configuration files
111 log.write("Updating configuration files.\n")
112 # avoid using conf_files initscript as we're moving to systemd on some platforms
114 if (vars['ONE_PARTITION'] != '1'):
116 cmd = "/usr/bin/env python /usr/share/NodeManager/conf_files.py --noscripts"
117 utils.sysexec_chroot(SYSIMG_PATH, cmd, log)
119 log.write("conf_files failed with \n {}".format(e))
121 # update node packages
122 log.write("Running node update.\n")
123 if os.path.exists(SYSIMG_PATH + "/usr/bin/NodeUpdate.py"):
124 cmd = "/usr/bin/NodeUpdate.py start noreboot"
126 # for backwards compatibility
127 cmd = "/usr/local/planetlab/bin/NodeUpdate.py start noreboot"
128 utils.sysexec_chroot(SYSIMG_PATH, cmd, log)
130 # Re-generate initrd right before kexec call
131 # this is not required anymore on recent depls.
132 if vars['virt'] == 'vs':
133 MakeInitrd.Run(vars, log)
135 # the following step should be done by NM
136 UpdateNodeConfiguration.Run(vars, log)
138 log.write("Updating ssh public host key with PLC.\n")
141 ssh_host_key_file = file("{}/etc/ssh/ssh_host_rsa_key.pub".format(SYSIMG_PATH), "r")
142 ssh_host_key = ssh_host_key_file.read().strip()
143 ssh_host_key_file.close()
144 ssh_host_key_file = None
149 update_vals['ssh_rsa_key'] = ssh_host_key
150 BootAPI.call_api_function(vars, "BootUpdateNode", (update_vals,))
153 # get the kernel version
155 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
158 log.write("Copying kernel and initrd for booting.\n")
159 if vars['virt'] == 'vs':
160 utils.sysexec("cp {}/boot/kernel-boot{} /tmp/kernel".format(SYSIMG_PATH, option), log)
161 utils.sysexec("cp {}/boot/initrd-boot{} /tmp/initrd".format(SYSIMG_PATH, option), log)
163 # Use chroot to call rpm, b/c the bootimage&nodeimage rpm-versions may not work together
165 kversion = os.popen("chroot {} rpm -qa kernel | tail -1 | cut -c 8-"\
166 .format(SYSIMG_PATH)).read().rstrip()
167 major_version = int(kversion[0]) # Check if the string looks like a kernel version
169 # Try a different method for non-rpm-based distributions
170 kversion = os.popen("ls -lrt {}/lib/modules | tail -1 | awk '{print $9;}'"\
171 .format(SYSIMG_PATH)).read().rstrip()
173 utils.sysexec("cp {}/boot/vmlinuz-{} /tmp/kernel".format(SYSIMG_PATH, kversion), log)
175 # f16/18: expect initramfs image here
176 candidates.append ("/boot/initramfs-{}.img".format(kversion))
177 # f20: uses a uid of some kind, e.g. /boot/543f88c129de443baaa65800cf3927ce/<kversion>/initrd
178 candidates.append ("/boot/*/{}/initrd".format(kversion))
180 candidates.append ("/boot/initrd.img-{}".format(kversion))
181 def find_file_in_sysimg (candidates):
183 for pattern in candidates:
184 matches = glob.glob(SYSIMG_PATH+pattern)
185 log.write("locating initrd: found {} matches in {}\n".format(len(matches), pattern))
188 initrd = find_file_in_sysimg(candidates)
190 utils.sysexec("cp {} /tmp/initrd".format(initrd), log)
192 raise Exception("Unable to locate initrd - bailing out")
196 log.write("Unmounting disks.\n")
198 if (vars['ONE_PARTITION'] != '1'):
199 utils.sysexec("umount {}/vservers".format(SYSIMG_PATH), log)
200 utils.sysexec("umount {}/proc".format(SYSIMG_PATH), log)
201 utils.sysexec_noerr("umount {}/dev".format(SYSIMG_PATH), log)
202 utils.sysexec_noerr("umount {}/sys".format(SYSIMG_PATH), log)
203 utils.sysexec("umount {}".format(SYSIMG_PATH), log)
204 utils.sysexec("vgchange -an", log)
207 vars['ROOT_MOUNTED'] = 0
209 # Change runlevel to 'boot' prior to kexec.
210 StopRunlevelAgent.Run(vars, log)
212 log.write("Unloading modules and chain booting to new kernel.\n")
214 # further use of log after Upload will only output to screen
215 log.Upload("/root/.bash_eternal_history")
217 # regardless of whether kexec works or not, we need to stop trying to
219 cancel_boot_flag = "/tmp/CANCEL_BOOT"
220 utils.sysexec("touch {}".format(cancel_boot_flag), log)
222 # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
223 # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
224 # there are a few buggy drivers that don't disable their hardware
225 # correctly unless they are first unloaded.
227 utils.sysexec_noerr("ifconfig eth0 down", log)
229 utils.sysexec_noerr("killall dhclient", log)
231 if vars['virt'] == 'vs':
232 utils.sysexec_noerr("umount -a -r -t ext2,ext3", log)
234 utils.sysexec_noerr("umount -a -r -t ext2,ext3,btrfs", log)
235 utils.sysexec_noerr("modprobe -r lvm-mod", log)
237 # modules that should not get unloaded
238 # unloading cpqphp causes a kernel panic
239 blacklist = [ "floppy", "cpqphp", "i82875p_edac", "mptspi"]
241 modules = file("/tmp/loadedmodules","r")
244 module = string.strip(line)
245 if module in blacklist :
246 log.write("Skipping unload of kernel module '{}'.\n".format(module))
248 log.write("Unloading {}\n".format(module))
249 utils.sysexec_noerr("modprobe -r {}".format(module), log)
250 if "e1000" in module:
251 log.write("Unloading e1000 driver; sleeping 4 seconds...\n")
256 log.write("Couldn't read /tmp/loadedmodules, continuing.\n")
259 modules = file("/proc/modules", "r")
261 # Get usage count for USB
265 # Module Size UsageCount UsedBy State LoadAddress
266 parts = string.split(line)
268 if parts[0] == "usb_storage":
269 usb_usage += int(parts[2])
270 except IndexError as e:
271 log.write("Couldn't parse /proc/modules, continuing.\n")
277 # Module Size UsageCount UsedBy State LoadAddress
278 parts = string.split(line)
280 # While we would like to remove all "unused" modules,
281 # you can't trust usage count, especially for things
282 # like network drivers or RAID array drivers. Just try
283 # and unload a few specific modules that we know cause
284 # problems during chain boot, such as USB host
285 # controller drivers (HCDs) (PL6577).
286 # if int(parts[2]) == 0:
287 if False and re.search('_hcd$', parts[0]):
289 log.write("NOT unloading {} since USB may be in use\n".format(parts[0]))
291 log.write("Unloading {}\n".format(parts[0]))
292 utils.sysexec_noerr("modprobe -r {}".format(parts[0]), log)
293 except IndexError as e:
294 log.write("Couldn't parse /proc/modules, continuing.\n")
296 log.write("Couldn't read /proc/modules, continuing.\n")
299 kargs = "root={} ramdisk_size=8192".format(PARTITIONS["mapper-root"])
300 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
301 kargs = kargs + " " + "acpi=off"
303 kargsfb = open("/kargs.txt","r")
304 moreargs = kargsfb.readline()
306 moreargs = moreargs.strip()
307 log.write('Parsed in "{}" kexec args from /kargs.txt\n'.format(moreargs))
308 kargs = kargs + " " + moreargs
310 # /kargs.txt does not exist, which is fine. Just kexec with default
311 # kargs, which is ramdisk_size=8192
314 utils.sysexec_noerr('hwclock --systohc --utc ', log)
315 utils.breakpoint ("Before kexec");
317 utils.sysexec('kexec --force --initrd=/tmp/initrd --append="{}" /tmp/kernel'.format(kargs), log)
318 except BootManagerException as e:
319 # if kexec fails, we've shut the machine down to a point where nothing
320 # can run usefully anymore (network down, all modules unloaded, file
321 # systems unmounted. write out the error, and cancel the boot process
324 log.write("-------------------------------------------------------\n")
325 log.write("kexec failed with the following error. Please report\n")
326 log.write("this problem to support@planet-lab.org.\n\n")
327 log.write(str(e) + "\n\n")
328 log.write("The boot process has been canceled.\n")
329 log.write("-------------------------------------------------------\n\n")