3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
14 import UpdateBootStateWithPLC
15 import UpdateNodeConfiguration
16 from Exceptions import *
21 import notify_messages
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 BOOT_CD_VERSION A tuple of the current bootcd version
35 SYSIMG_PATH the path where the system image will be mounted
36 (always starts with TEMP_PATH)
37 ROOT_MOUNTED the node root file system is mounted
38 NODE_SESSION the unique session val set when we requested
39 the current boot state
40 PLCONF_DIR The directory to store PL configuration files in
42 Sets the following variables:
43 ROOT_MOUNTED the node root file system is mounted
46 log.write( "\n\nStep: Chain booting node.\n" )
48 # make sure we have the variables we need
50 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
51 if BOOT_CD_VERSION == "":
52 raise ValueError, "BOOT_CD_VERSION"
54 SYSIMG_PATH= vars["SYSIMG_PATH"]
56 raise ValueError, "SYSIMG_PATH"
58 PLCONF_DIR= vars["PLCONF_DIR"]
60 raise ValueError, "PLCONF_DIR"
62 # its ok if this is blank
63 NODE_SESSION= vars["NODE_SESSION"]
65 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
67 PARTITIONS= vars["PARTITIONS"]
68 if PARTITIONS == None:
69 raise ValueError, "PARTITIONS"
72 raise BootManagerException, "Missing variable in vars: %s\n" % var
73 except ValueError, var:
74 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
77 if 'ROOT_MOUNTED' in vars.keys():
78 ROOT_MOUNTED= vars['ROOT_MOUNTED']
81 log.write( "Mounting node partitions\n" )
83 # old cds need extra utilities to run lvm
84 if BOOT_CD_VERSION[0] == 2:
85 compatibility.setup_lvm_2x_cd( vars, log )
87 # simply creating an instance of this class and listing the system
88 # block devices will make them show up so vgscan can find the planetlab
90 systeminfo.get_block_device_list(vars, log)
92 utils.sysexec( "vgscan", log )
93 utils.sysexec( "vgchange -ay planetlab", log )
95 utils.makedirs( SYSIMG_PATH )
97 cmd = "mount %s %s" % (PARTITIONS["root"],SYSIMG_PATH)
98 utils.sysexec( cmd, log )
99 cmd = "mount %s %s/vservers" % (PARTITIONS["vservers"],SYSIMG_PATH)
100 utils.sysexec( cmd, log )
101 cmd = "mount -t proc none %s/proc" % SYSIMG_PATH
102 utils.sysexec( cmd, log )
105 vars['ROOT_MOUNTED']= 1
108 # write out the session value /etc/planetlab/session
110 session_file_path= "%s/%s/session" % (SYSIMG_PATH,PLCONF_DIR)
111 session_file= file( session_file_path, "w" )
112 session_file.write( str(NODE_SESSION) )
115 log.write( "Updated /etc/planetlab/session\n" )
117 log.write( "Unable to write out /etc/planetlab/session, continuing anyway\n" )
119 # update configuration files
120 log.write( "Updating configuration files.\n" )
122 cmd = "/etc/init.d/conf_files start --noscripts"
123 utils.sysexec( "chroot %s %s" % (SYSIMG_PATH, cmd), log )
125 log.write("conf_files failed with \n %s" % e)
127 # update node packages
128 log.write( "Running node update.\n" )
129 if os.path.exists( SYSIMG_PATH + "/usr/bin/NodeUpdate.py" ):
130 cmd = "chroot %s /usr/bin/NodeUpdate.py start noreboot" % SYSIMG_PATH
132 # for backwards compatibility
133 cmd = "chroot %s /usr/local/planetlab/bin/NodeUpdate.py start noreboot" % SYSIMG_PATH
134 utils.sysexec( cmd, log )
136 # the following step should be done by NM
137 UpdateNodeConfiguration.Run( vars, log )
139 log.write( "Updating ssh public host key with PLC.\n" )
142 ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
143 ssh_host_key= ssh_host_key_file.read().strip()
144 ssh_host_key_file.close()
145 ssh_host_key_file= None
150 update_vals['ssh_host_key']= ssh_host_key
151 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 utils.sysexec( "cp %s/boot/kernel-boot%s /tmp/kernel" % (SYSIMG_PATH,option), log )
160 utils.sysexec( "cp %s/boot/initrd-boot%s /tmp/initrd" % (SYSIMG_PATH,option), log )
164 log.write( "Unmounting disks.\n" )
166 # backwards compat, though, we should never hit this case post PL 3.2
167 os.stat("%s/rcfs/taskclass"%SYSIMG_PATH)
168 utils.sysexec_noerr( "chroot %s umount /rcfs" % SYSIMG_PATH, log )
172 utils.sysexec_noerr( "umount %s/proc" % SYSIMG_PATH, log )
173 utils.sysexec_noerr( "umount -r %s/vservers" % SYSIMG_PATH, log )
174 utils.sysexec_noerr( "umount -r %s" % SYSIMG_PATH, log )
175 utils.sysexec_noerr( "vgchange -an", log )
178 vars['ROOT_MOUNTED']= 0
180 log.write( "Unloading modules and chain booting to new kernel.\n" )
182 # further use of log after Upload will only output to screen
185 # regardless of whether kexec works or not, we need to stop trying to
187 cancel_boot_flag= "/tmp/CANCEL_BOOT"
188 utils.sysexec( "touch %s" % cancel_boot_flag, log )
190 # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
191 # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
192 # there are a few buggy drivers that don't disable their hardware
193 # correctly unless they are first unloaded.
195 utils.sysexec_noerr( "ifconfig eth0 down", log )
197 if BOOT_CD_VERSION[0] == 2:
198 utils.sysexec_noerr( "killall dhcpcd", log )
199 elif BOOT_CD_VERSION[0] >= 3:
200 utils.sysexec_noerr( "killall dhclient", log )
202 utils.sysexec_noerr( "umount -a -r -t ext2,ext3", log )
203 utils.sysexec_noerr( "modprobe -r lvm-mod", log )
205 # modules that should not get unloaded
206 # unloading cpqphp causes a kernel panic
207 blacklist = [ "floppy", "cpqphp", "i82875p_edac" ]
209 modules= file("/tmp/loadedmodules","r")
212 module= string.strip(line)
213 if module in blacklist :
214 log.write("Skipping unload of kernel module '%s'.\n"%module)
216 log.write( "Unloading %s\n" % module )
217 utils.sysexec_noerr( "modprobe -r %s" % module, log )
218 if "e1000" in module:
219 log.write("Unloading e1000 driver; sleeping 4 seconds...\n")
224 log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
227 modules= file("/proc/modules", "r")
229 # Get usage count for USB
233 # Module Size UsageCount UsedBy State LoadAddress
234 parts= string.split(line)
236 if parts[0] == "usb_storage":
237 usb_usage += int(parts[2])
238 except IndexError, e:
239 log.write( "Couldn't parse /proc/modules, continuing.\n" )
245 # Module Size UsageCount UsedBy State LoadAddress
246 parts= string.split(line)
248 # While we would like to remove all "unused" modules,
249 # you can't trust usage count, especially for things
250 # like network drivers or RAID array drivers. Just try
251 # and unload a few specific modules that we know cause
252 # problems during chain boot, such as USB host
253 # controller drivers (HCDs) (PL6577).
254 # if int(parts[2]) == 0:
255 if False and re.search('_hcd$', parts[0]):
257 log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
259 log.write( "Unloading %s\n" % parts[0] )
260 utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
261 except IndexError, e:
262 log.write( "Couldn't parse /proc/modules, continuing.\n" )
264 log.write( "Couldn't read /proc/modules, continuing.\n" )
267 kargs = "root=%s ramdisk_size=8192" % PARTITIONS["mapper-root"]
268 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
269 kargs = kargs + " " + "acpi=off"
271 kargsfb = open("/kargs.txt","r")
272 moreargs = kargsfb.readline()
274 moreargs = moreargs.strip()
275 log.write( 'Parsed in "%s" kexec args from /kargs.txt\n' % moreargs )
276 kargs = kargs + " " + moreargs
278 # /kargs.txt does not exist, which is fine. Just kexec with default
279 # kargs, which is ramdisk_size=8192
282 utils.breakpoint ("Before kexec");
284 utils.sysexec( 'kexec --force --initrd=/tmp/initrd ' \
285 '--append="%s" /tmp/kernel' % kargs)
286 except BootManagerException, e:
287 # if kexec fails, we've shut the machine down to a point where nothing
288 # can run usefully anymore (network down, all modules unloaded, file
289 # systems unmounted. write out the error, and cancel the boot process
292 log.write( "-------------------------------------------------------\n" )
293 log.write( "kexec failed with the following error. Please report\n" )
294 log.write( "this problem to support@planet-lab.org.\n\n" )
295 log.write( str(e) + "\n\n" )
296 log.write( "The boot process has been canceled.\n" )
297 log.write( "-------------------------------------------------------\n\n" )