3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
14 import UpdateNodeConfiguration
16 import StopRunlevelAgent
17 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 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"
67 raise BootManagerException, "Missing variable in vars: %s\n" % var
68 except ValueError, var:
69 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % 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 %s %s" % (PARTITIONS["root"],SYSIMG_PATH)
89 utils.sysexec( cmd, log )
90 cmd = "mount -t proc none %s/proc" % SYSIMG_PATH
91 utils.sysexec( cmd, log )
92 cmd = "mount %s %s/vservers" % (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= "%s/%s/session" % (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" )
113 cmd = "/etc/init.d/conf_files start --noscripts"
114 utils.sysexec_chroot( SYSIMG_PATH, cmd, log )
116 log.write("conf_files failed with \n %s" % e)
118 # update node packages
119 log.write( "Running node update.\n" )
120 if os.path.exists( SYSIMG_PATH + "/usr/bin/NodeUpdate.py" ):
121 cmd = "/usr/bin/NodeUpdate.py start noreboot"
123 # for backwards compatibility
124 cmd = "/usr/local/planetlab/bin/NodeUpdate.py start noreboot"
125 utils.sysexec_chroot( SYSIMG_PATH, cmd, log )
127 # Re-generate initrd right before kexec call
128 MakeInitrd.Run( vars, log )
130 # the following step should be done by NM
131 UpdateNodeConfiguration.Run( vars, log )
133 log.write( "Updating ssh public host key with PLC.\n" )
136 ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
137 ssh_host_key= ssh_host_key_file.read().strip()
138 ssh_host_key_file.close()
139 ssh_host_key_file= None
144 update_vals['ssh_rsa_key']= ssh_host_key
145 BootAPI.call_api_function( vars, "BootUpdateNode", (update_vals,) )
148 # get the kernel version
150 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
153 log.write( "Copying kernel and initrd for booting.\n" )
154 utils.sysexec( "cp %s/boot/kernel-boot%s /tmp/kernel" % (SYSIMG_PATH,option), log )
155 utils.sysexec( "cp %s/boot/initrd-boot%s /tmp/initrd" % (SYSIMG_PATH,option), log )
159 log.write( "Unmounting disks.\n" )
160 utils.sysexec( "umount %s/vservers" % SYSIMG_PATH, log )
161 utils.sysexec( "umount %s/proc" % SYSIMG_PATH, log )
162 utils.sysexec_noerr( "umount %s/dev" % SYSIMG_PATH, log )
163 utils.sysexec_noerr( "umount %s/sys" % SYSIMG_PATH, log )
164 utils.sysexec( "umount %s" % SYSIMG_PATH, log )
165 utils.sysexec( "vgchange -an", log )
168 vars['ROOT_MOUNTED']= 0
170 # Change runlevel to 'boot' prior to kexec.
171 StopRunlevelAgent.Run( vars, log )
173 log.write( "Unloading modules and chain booting to new kernel.\n" )
175 # further use of log after Upload will only output to screen
176 log.Upload("/root/.bash_eternal_history")
178 # regardless of whether kexec works or not, we need to stop trying to
180 cancel_boot_flag= "/tmp/CANCEL_BOOT"
181 utils.sysexec( "touch %s" % cancel_boot_flag, log )
183 # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
184 # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
185 # there are a few buggy drivers that don't disable their hardware
186 # correctly unless they are first unloaded.
188 utils.sysexec_noerr( "ifconfig eth0 down", log )
190 utils.sysexec_noerr( "killall dhclient", log )
192 utils.sysexec_noerr( "umount -a -r -t ext2,ext3", log )
193 utils.sysexec_noerr( "modprobe -r lvm-mod", log )
195 # modules that should not get unloaded
196 # unloading cpqphp causes a kernel panic
197 blacklist = [ "floppy", "cpqphp", "i82875p_edac", "mptspi"]
199 modules= file("/tmp/loadedmodules","r")
202 module= string.strip(line)
203 if module in blacklist :
204 log.write("Skipping unload of kernel module '%s'.\n"%module)
206 log.write( "Unloading %s\n" % module )
207 utils.sysexec_noerr( "modprobe -r %s" % module, log )
208 if "e1000" in module:
209 log.write("Unloading e1000 driver; sleeping 4 seconds...\n")
214 log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
217 modules= file("/proc/modules", "r")
219 # Get usage count for USB
223 # Module Size UsageCount UsedBy State LoadAddress
224 parts= string.split(line)
226 if parts[0] == "usb_storage":
227 usb_usage += int(parts[2])
228 except IndexError, e:
229 log.write( "Couldn't parse /proc/modules, continuing.\n" )
235 # Module Size UsageCount UsedBy State LoadAddress
236 parts= string.split(line)
238 # While we would like to remove all "unused" modules,
239 # you can't trust usage count, especially for things
240 # like network drivers or RAID array drivers. Just try
241 # and unload a few specific modules that we know cause
242 # problems during chain boot, such as USB host
243 # controller drivers (HCDs) (PL6577).
244 # if int(parts[2]) == 0:
245 if False and re.search('_hcd$', parts[0]):
247 log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
249 log.write( "Unloading %s\n" % parts[0] )
250 utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
251 except IndexError, e:
252 log.write( "Couldn't parse /proc/modules, continuing.\n" )
254 log.write( "Couldn't read /proc/modules, continuing.\n" )
257 kargs = "root=%s ramdisk_size=8192" % PARTITIONS["mapper-root"]
258 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
259 kargs = kargs + " " + "acpi=off"
261 kargsfb = open("/kargs.txt","r")
262 moreargs = kargsfb.readline()
264 moreargs = moreargs.strip()
265 log.write( 'Parsed in "%s" kexec args from /kargs.txt\n' % moreargs )
266 kargs = kargs + " " + moreargs
268 # /kargs.txt does not exist, which is fine. Just kexec with default
269 # kargs, which is ramdisk_size=8192
272 utils.sysexec_noerr( 'hwclock --systohc --utc ', log )
273 utils.breakpoint ("Before kexec");
275 utils.sysexec( 'kexec --force --initrd=/tmp/initrd --append="%s" /tmp/kernel' % kargs, log)
276 except BootManagerException, e:
277 # if kexec fails, we've shut the machine down to a point where nothing
278 # can run usefully anymore (network down, all modules unloaded, file
279 # systems unmounted. write out the error, and cancel the boot process
282 log.write( "-------------------------------------------------------\n" )
283 log.write( "kexec failed with the following error. Please report\n" )
284 log.write( "this problem to support@planet-lab.org.\n\n" )
285 log.write( str(e) + "\n\n" )
286 log.write( "The boot process has been canceled.\n" )
287 log.write( "-------------------------------------------------------\n\n" )