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