3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
14 import UpdateBootStateWithPLC
15 from Exceptions import *
20 import notify_messages
26 Load the kernel off of a node and boot to it.
27 This step assumes the disks are mounted on SYSIMG_PATH.
28 If successful, this function will not return. If it returns, no chain
31 Expect the following variables:
32 BOOT_CD_VERSION A tuple of the current bootcd version
33 SYSIMG_PATH the path where the system image will be mounted
34 (always starts with TEMP_PATH)
35 ROOT_MOUNTED the node root file system is mounted
36 NODE_SESSION the unique session val set when we requested
37 the current boot state
38 PLCONF_DIR The directory to store PL configuration files in
40 Sets the following variables:
41 ROOT_MOUNTED the node root file system is mounted
44 log.write( "\n\nStep: Chain booting node.\n" )
46 # make sure we have the variables we need
48 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
49 if BOOT_CD_VERSION == "":
50 raise ValueError, "BOOT_CD_VERSION"
52 SYSIMG_PATH= vars["SYSIMG_PATH"]
54 raise ValueError, "SYSIMG_PATH"
56 PLCONF_DIR= vars["PLCONF_DIR"]
58 raise ValueError, "PLCONF_DIR"
60 # its ok if this is blank
61 NODE_SESSION= vars["NODE_SESSION"]
63 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
65 PARTITIONS= vars["PARTITIONS"]
66 if PARTITIONS == None:
67 raise ValueError, "PARTITIONS"
70 raise BootManagerException, "Missing variable in vars: %s\n" % var
71 except ValueError, var:
72 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
75 if 'ROOT_MOUNTED' in vars.keys():
76 ROOT_MOUNTED= vars['ROOT_MOUNTED']
79 log.write( "Mounting node partitions\n" )
81 # old cds need extra utilities to run lvm
82 if BOOT_CD_VERSION[0] == 2:
83 compatibility.setup_lvm_2x_cd( vars, log )
85 # simply creating an instance of this class and listing the system
86 # block devices will make them show up so vgscan can find the planetlab
88 systeminfo.get_block_device_list(vars, log)
90 utils.sysexec( "vgscan", log )
91 utils.sysexec( "vgchange -ay planetlab", log )
93 utils.makedirs( SYSIMG_PATH )
95 cmd = "mount %s %s" % (PARTITIONS["root"],SYSIMG_PATH)
96 utils.sysexec( cmd, log )
97 cmd = "mount %s %s/vservers" % (PARTITIONS["vservers"],SYSIMG_PATH)
98 utils.sysexec( cmd, log )
99 cmd = "mount -t proc none %s/proc" % SYSIMG_PATH
100 utils.sysexec( cmd, log )
103 vars['ROOT_MOUNTED']= 1
106 log.write( "Running node update.\n" )
107 cmd = "chroot %s /usr/local/planetlab/bin/NodeUpdate.py start noreboot" \
109 utils.sysexec( cmd, log )
111 log.write( "Updating ssh public host key with PLC.\n" )
114 ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
115 ssh_host_key= ssh_host_key_file.read().strip()
116 ssh_host_key_file.close()
117 ssh_host_key_file= None
121 # write out the session value /etc/planetlab/session
123 session_file_path= "%s/%s/session" % (SYSIMG_PATH,PLCONF_DIR)
124 session_file= file( session_file_path, "w" )
125 session_file.write( str(NODE_SESSION) )
128 log.write( "Updated /etc/planetlab/session\n" )
130 log.write( "Unable to write out /etc/planetlab/session, continuing anyway\n" )
133 update_vals['ssh_host_key']= ssh_host_key
134 BootAPI.call_api_function( vars, "BootUpdateNode", (update_vals,) )
136 # get the kernel version
138 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
141 log.write( "Copying kernel and initrd for booting.\n" )
142 utils.sysexec( "cp %s/boot/kernel-boot%s /tmp/kernel" % (SYSIMG_PATH,option), log )
143 utils.sysexec( "cp %s/boot/initrd-boot%s /tmp/initrd" % (SYSIMG_PATH,option), log )
145 log.write( "Unmounting disks.\n" )
147 # backwards compat, though, we should never hit this case post PL 3.2
148 os.stat("%s/rcfs/taskclass"%SYSIMG_PATH)
149 utils.sysexec_noerr( "chroot %s umount /rcfs" % SYSIMG_PATH, log )
153 utils.sysexec_noerr( "umount %s/proc" % SYSIMG_PATH, log )
154 utils.sysexec_noerr( "umount -r %s/vservers" % SYSIMG_PATH, log )
155 utils.sysexec_noerr( "umount -r %s" % SYSIMG_PATH, log )
156 utils.sysexec_noerr( "vgchange -an", log )
159 vars['ROOT_MOUNTED']= 0
161 log.write( "Unloading modules and chain booting to new kernel.\n" )
163 # further use of log after Upload will only output to screen
166 # regardless of whether kexec works or not, we need to stop trying to
168 cancel_boot_flag= "/tmp/CANCEL_BOOT"
169 utils.sysexec( "touch %s" % cancel_boot_flag, log )
171 # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
172 # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
173 # there are a few buggy drivers that don't disable their hardware
174 # correctly unless they are first unloaded.
176 utils.sysexec_noerr( "ifconfig eth0 down", log )
178 if BOOT_CD_VERSION[0] == 2:
179 utils.sysexec_noerr( "killall dhcpcd", log )
180 elif BOOT_CD_VERSION[0] == 3:
181 utils.sysexec_noerr( "killall dhclient", log )
183 utils.sysexec_noerr( "umount -a -r -t ext2,ext3", log )
184 utils.sysexec_noerr( "modprobe -r lvm-mod", log )
187 modules= file("/tmp/loadedmodules","r")
190 module= string.strip(line)
192 log.write( "Unloading %s\n" % module )
193 utils.sysexec_noerr( "modprobe -r %s" % module, log )
197 log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
200 modules= file("/proc/modules", "r")
202 # Get usage count for USB
206 # Module Size UsageCount UsedBy State LoadAddress
207 parts= string.split(line)
209 if parts[0] == "usb_storage":
210 usb_usage += int(parts[2])
211 except IndexError, e:
212 log.write( "Couldn't parse /proc/modules, continuing.\n" )
218 # Module Size UsageCount UsedBy State LoadAddress
219 parts= string.split(line)
221 # While we would like to remove all "unused" modules,
222 # you can't trust usage count, especially for things
223 # like network drivers or RAID array drivers. Just try
224 # and unload a few specific modules that we know cause
225 # problems during chain boot, such as USB host
226 # controller drivers (HCDs) (PL6577).
227 # if int(parts[2]) == 0:
228 if re.search('_hcd$', parts[0]):
230 log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
232 log.write( "Unloading %s\n" % parts[0] )
233 utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
234 except IndexError, e:
235 log.write( "Couldn't parse /proc/modules, continuing.\n" )
237 log.write( "Couldn't read /proc/modules, continuing.\n" )
240 kargs = "root=%s ramdisk_size=8192" % PARTITIONS["mapper-root"]
241 if NODE_MODEL_OPTIONS & ModelOptions.SMP:
242 kargs = kargs + " " + "acpi=off"
244 kargsfb = open("/kargs.txt","r")
245 moreargs = kargsfb.readline()
247 moreargs = moreargs.strip()
248 log.write( 'Parsed in "%s" kexec args from /kargs.txt\n' % moreargs )
249 kargs = kargs + " " + moreargs
251 # /kargs.txt does not exist, which is fine. Just kexec with default
252 # kargs, which is ramdisk_size=8192
256 utils.sysexec( 'kexec --force --initrd=/tmp/initrd ' \
257 '--append="%s" /tmp/kernel' % kargs)
258 except BootManagerException, e:
259 # if kexec fails, we've shut the machine down to a point where nothing
260 # can run usefully anymore (network down, all modules unloaded, file
261 # systems unmounted. write out the error, and cancel the boot process
264 log.write( "-------------------------------------------------------\n" )
265 log.write( "kexec failed with the following error. Please report\n" )
266 log.write( "this problem to support@planet-lab.org.\n\n" )
267 log.write( str(e) + "\n\n" )
268 log.write( "The boot process has been canceled.\n" )
269 log.write( "-------------------------------------------------------\n\n" )