dont unload cpqphp
[bootmanager.git] / source / steps / ChainBootNode.py
1 #!/usr/bin/python2
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8
9
10 import string
11 import re
12 import os
13
14 import UpdateBootStateWithPLC
15 from Exceptions import *
16 import utils
17 import compatibility
18 import systeminfo
19 import BootAPI
20 import notify_messages
21 import time
22
23 import ModelOptions
24
25 def Run( vars, log ):
26     """
27     Load the kernel off of a node and boot to it.
28     This step assumes the disks are mounted on SYSIMG_PATH.
29     If successful, this function will not return. If it returns, no chain
30     booting has occurred.
31     
32     Expect the following variables:
33     BOOT_CD_VERSION       A tuple of the current bootcd version
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
40     
41     Sets the following variables:
42     ROOT_MOUNTED          the node root file system is mounted
43     """
44
45     log.write( "\n\nStep: Chain booting node.\n" )
46
47     # make sure we have the variables we need
48     try:
49         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
50         if BOOT_CD_VERSION == "":
51             raise ValueError, "BOOT_CD_VERSION"
52
53         SYSIMG_PATH= vars["SYSIMG_PATH"]
54         if SYSIMG_PATH == "":
55             raise ValueError, "SYSIMG_PATH"
56
57         PLCONF_DIR= vars["PLCONF_DIR"]
58         if PLCONF_DIR == "":
59             raise ValueError, "PLCONF_DIR"
60
61         # its ok if this is blank
62         NODE_SESSION= vars["NODE_SESSION"]
63
64         NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
65
66         PARTITIONS= vars["PARTITIONS"]
67         if PARTITIONS == None:
68             raise ValueError, "PARTITIONS"
69
70     except KeyError, var:
71         raise BootManagerException, "Missing variable in vars: %s\n" % var
72     except ValueError, var:
73         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
74
75     ROOT_MOUNTED= 0
76     if 'ROOT_MOUNTED' in vars.keys():
77         ROOT_MOUNTED= vars['ROOT_MOUNTED']
78     
79     if ROOT_MOUNTED == 0:
80         log.write( "Mounting node partitions\n" )
81
82         # old cds need extra utilities to run lvm
83         if BOOT_CD_VERSION[0] == 2:
84             compatibility.setup_lvm_2x_cd( vars, log )
85             
86         # simply creating an instance of this class and listing the system
87         # block devices will make them show up so vgscan can find the planetlab
88         # volume group
89         systeminfo.get_block_device_list(vars, log)
90         
91         utils.sysexec( "vgscan", log )
92         utils.sysexec( "vgchange -ay planetlab", log )
93
94         utils.makedirs( SYSIMG_PATH )
95
96         cmd = "mount %s %s" % (PARTITIONS["root"],SYSIMG_PATH)
97         utils.sysexec( cmd, log )
98         cmd = "mount %s %s/vservers" % (PARTITIONS["vservers"],SYSIMG_PATH)
99         utils.sysexec( cmd, log )
100         cmd = "mount -t proc none %s/proc" % SYSIMG_PATH
101         utils.sysexec( cmd, log )
102
103         ROOT_MOUNTED= 1
104         vars['ROOT_MOUNTED']= 1
105         
106
107     # write out the session value /etc/planetlab/session
108     try:
109         session_file_path= "%s/%s/session" % (SYSIMG_PATH,PLCONF_DIR)
110         session_file= file( session_file_path, "w" )
111         session_file.write( str(NODE_SESSION) )
112         session_file.close()
113         session_file= None
114         log.write( "Updated /etc/planetlab/session\n" )
115     except IOError, e:
116         log.write( "Unable to write out /etc/planetlab/session, continuing anyway\n" )
117
118     # update configuration files
119     log.write( "Updating configuration files.\n" )
120     try:
121         cmd = "/etc/init.d/conf_files start --noscripts"
122         utils.sysexec( "chroot %s %s" % (SYSIMG_PATH, cmd), log )
123     except IOError, e:
124         log.write("conf_files failed with \n %s" % e)
125
126     # update node packages
127     log.write( "Running node update.\n" )
128     if os.path.exists( SYSIMG_PATH + "/usr/bin/NodeUpdate.py" ):
129         cmd = "chroot %s /usr/bin/NodeUpdate.py start noreboot" % SYSIMG_PATH
130     else:
131         # for backwards compatibility
132         cmd = "chroot %s /usr/local/planetlab/bin/NodeUpdate.py start noreboot" % SYSIMG_PATH
133     utils.sysexec( cmd, log )
134
135     log.write( "Updating ssh public host key with PLC.\n" )
136     ssh_host_key= ""
137     try:
138         ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
139         ssh_host_key= ssh_host_key_file.read().strip()
140         ssh_host_key_file.close()
141         ssh_host_key_file= None
142     except IOError, e:
143         pass
144
145     update_vals= {}
146     update_vals['ssh_host_key']= ssh_host_key
147     BootAPI.call_api_function( vars, "BootUpdateNode", (update_vals,) )
148
149     # get the kernel version
150     option = ''
151     if NODE_MODEL_OPTIONS & ModelOptions.SMP:
152         option = 'smp'
153
154     log.write( "Copying kernel and initrd for booting.\n" )
155     utils.sysexec( "cp %s/boot/kernel-boot%s /tmp/kernel" % (SYSIMG_PATH,option), log )
156     utils.sysexec( "cp %s/boot/initrd-boot%s /tmp/initrd" % (SYSIMG_PATH,option), log )
157
158     BootAPI.save(vars)
159
160     log.write( "Unmounting disks.\n" )
161     try:
162         # backwards compat, though, we should never hit this case post PL 3.2
163         os.stat("%s/rcfs/taskclass"%SYSIMG_PATH)
164         utils.sysexec_noerr( "chroot %s umount /rcfs" % SYSIMG_PATH, log )
165     except OSError, e:
166         pass
167
168     utils.sysexec_noerr( "umount %s/proc" % SYSIMG_PATH, log )
169     utils.sysexec_noerr( "umount -r %s/vservers" % SYSIMG_PATH, log )
170     utils.sysexec_noerr( "umount -r %s" % SYSIMG_PATH, log )
171     utils.sysexec_noerr( "vgchange -an", log )
172
173     ROOT_MOUNTED= 0
174     vars['ROOT_MOUNTED']= 0
175
176     log.write( "Unloading modules and chain booting to new kernel.\n" )
177
178     # further use of log after Upload will only output to screen
179     log.Upload()
180
181     # regardless of whether kexec works or not, we need to stop trying to
182     # run anything
183     cancel_boot_flag= "/tmp/CANCEL_BOOT"
184     utils.sysexec( "touch %s" % cancel_boot_flag, log )
185
186     # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
187     # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
188     # there are a few buggy drivers that don't disable their hardware
189     # correctly unless they are first unloaded.
190     
191     utils.sysexec_noerr( "ifconfig eth0 down", log )
192
193     if BOOT_CD_VERSION[0] == 2:
194         utils.sysexec_noerr( "killall dhcpcd", log )
195     elif BOOT_CD_VERSION[0] >= 3:
196         utils.sysexec_noerr( "killall dhclient", log )
197         
198     utils.sysexec_noerr( "umount -a -r -t ext2,ext3", log )
199     utils.sysexec_noerr( "modprobe -r lvm-mod", log )
200     
201     # modules that should not get unloaded
202     # unloading cpqphp causes a kernel panic
203     blacklist = [ "floppy", "cpqphp" ]
204     try:
205         modules= file("/tmp/loadedmodules","r")
206         
207         for line in modules:
208             module= string.strip(line)
209             if module in blacklist :
210                 log.write("Skipping unload of kernel module '%s'.\n"%module)
211             elif module != "":
212                 log.write( "Unloading %s\n" % module )
213                 utils.sysexec_noerr( "modprobe -r %s" % module, log )
214                 if module == "e1000":
215                     log.write("Unloading e1000 driver; sleeping 4 seconds...\n")
216                     time.sleep(4)
217
218         modules.close()
219     except IOError:
220         log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
221
222     try:
223         modules= file("/proc/modules", "r")
224
225         # Get usage count for USB
226         usb_usage = 0
227         for line in modules:
228             try:
229                 # Module Size UsageCount UsedBy State LoadAddress
230                 parts= string.split(line)
231
232                 if parts[0] == "usb_storage":
233                     usb_usage += int(parts[2])
234             except IndexError, e:
235                 log.write( "Couldn't parse /proc/modules, continuing.\n" )
236
237         modules.seek(0)
238
239         for line in modules:
240             try:
241                 # Module Size UsageCount UsedBy State LoadAddress
242                 parts= string.split(line)
243
244                 # While we would like to remove all "unused" modules,
245                 # you can't trust usage count, especially for things
246                 # like network drivers or RAID array drivers. Just try
247                 # and unload a few specific modules that we know cause
248                 # problems during chain boot, such as USB host
249                 # controller drivers (HCDs) (PL6577).
250                 # if int(parts[2]) == 0:
251                 if False and re.search('_hcd$', parts[0]):
252                     if usb_usage > 0:
253                         log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
254                     else:
255                         log.write( "Unloading %s\n" % parts[0] )
256                         utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
257             except IndexError, e:
258                 log.write( "Couldn't parse /proc/modules, continuing.\n" )
259     except IOError:
260         log.write( "Couldn't read /proc/modules, continuing.\n" )
261
262
263     kargs = "root=%s ramdisk_size=8192" % PARTITIONS["mapper-root"]
264     if NODE_MODEL_OPTIONS & ModelOptions.SMP:
265         kargs = kargs + " " + "acpi=off"
266     try:
267         kargsfb = open("/kargs.txt","r")
268         moreargs = kargsfb.readline()
269         kargsfb.close()
270         moreargs = moreargs.strip()
271         log.write( 'Parsed in "%s" kexec args from /kargs.txt\n' % moreargs )
272         kargs = kargs + " " + moreargs
273     except IOError:
274         # /kargs.txt does not exist, which is fine. Just kexec with default
275         # kargs, which is ramdisk_size=8192
276         pass 
277
278     utils.breakpoint ("Before kexec");
279     try:
280         utils.sysexec( 'kexec --force --initrd=/tmp/initrd ' \
281                        '--append="%s" /tmp/kernel' % kargs)
282     except BootManagerException, e:
283         # if kexec fails, we've shut the machine down to a point where nothing
284         # can run usefully anymore (network down, all modules unloaded, file
285         # systems unmounted. write out the error, and cancel the boot process
286
287         log.write( "\n\n" )
288         log.write( "-------------------------------------------------------\n" )
289         log.write( "kexec failed with the following error. Please report\n" )
290         log.write( "this problem to support@planet-lab.org.\n\n" )
291         log.write( str(e) + "\n\n" )
292         log.write( "The boot process has been canceled.\n" )
293         log.write( "-------------------------------------------------------\n\n" )
294
295     return