- PL6577: at least on the ufl.edu nodes, the USB host drivers need to be
[bootmanager.git] / source / steps / ChainBootNode.py
1 import string
2 import re
3
4 from Exceptions import *
5 import utils
6 import compatibility
7 from systeminfo import systeminfo
8 import BootAPI
9
10
11 def Run( vars, log ):
12     """
13     Load the kernel off of a node and boot to it.
14     This step assumes the disks are mounted on SYSIMG_PATH.
15     
16     Expect the following variables:
17     BOOT_CD_VERSION       A tuple of the current bootcd version
18     SYSIMG_PATH           the path where the system image will be mounted
19                           (always starts with TEMP_PATH)
20     ROOT_MOUNTED          the node root file system is mounted
21
22     Sets the following variables:
23     ROOT_MOUNTED          the node root file system is mounted
24     """
25
26     log.write( "\n\nStep: Chain booting node.\n" )
27
28     # make sure we have the variables we need
29     try:
30         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
31         if BOOT_CD_VERSION == "":
32             raise ValueError, "BOOT_CD_VERSION"
33
34         SYSIMG_PATH= vars["SYSIMG_PATH"]
35         if SYSIMG_PATH == "":
36             raise ValueError, "SYSIMG_PATH"
37         
38     except KeyError, var:
39         raise BootManagerException, "Missing variable in vars: %s\n" % var
40     except ValueError, var:
41         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
42
43     ROOT_MOUNTED= 0
44     if 'ROOT_MOUNTED' in vars.keys():
45         ROOT_MOUNTED= vars['ROOT_MOUNTED']
46     
47     if ROOT_MOUNTED == 0:
48         log.write( "Mounting node partitions\n" )
49
50         # old cds need extra utilities to run lvm
51         if BOOT_CD_VERSION[0] == 2:
52             compatibility.setup_lvm_2x_cd( vars, log )
53             
54         # simply creating an instance of this class and listing the system
55         # block devices will make them show up so vgscan can find the planetlab
56         # volume group
57         systeminfo().get_block_device_list()
58         
59         utils.sysexec( "vgscan", log )
60         utils.sysexec( "vgchange -ay planetlab", log )
61
62         utils.makedirs( SYSIMG_PATH )
63
64         utils.sysexec( "mount /dev/planetlab/root %s" % SYSIMG_PATH, log )
65         utils.sysexec( "mount /dev/planetlab/vservers %s/vservers" %
66                        SYSIMG_PATH, log )
67
68         ROOT_MOUNTED= 1
69         vars['ROOT_MOUNTED']= 1
70         
71
72     node_update_cmd= "/usr/local/planetlab/bin/NodeUpdate.py start noreboot"
73
74     log.write( "Running node update.\n" )
75     utils.sysexec( "chroot %s %s" % (SYSIMG_PATH,node_update_cmd), log )
76
77     log.write( "Updating ssh public host key with PLC.\n" )
78     ssh_host_key= ""
79     try:
80         ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
81         ssh_host_key= ssh_host_key_file.read().strip()
82         ssh_host_key_file.close()
83         ssh_host_key_file= None
84     except IOError, e:
85         pass
86     
87     update_vals= {}
88     update_vals['ssh_host_key']= ssh_host_key
89     BootAPI.call_api_function( vars, "BootUpdateNode", (update_vals,) )
90
91
92     log.write( "Copying kernel and initrd for booting.\n" )
93     utils.sysexec( "cp %s/boot/kernel-boot /tmp/kernel" % SYSIMG_PATH, log )
94     utils.sysexec( "cp %s/boot/initrd-boot /tmp/initrd" % SYSIMG_PATH, log )
95
96     log.write( "Unmounting disks.\n" )
97     utils.sysexec_noerr( "chroot %s umount /rcfs" % SYSIMG_PATH, log )
98     utils.sysexec_noerr( "umount -r /dev/planetlab/vservers", log )
99     utils.sysexec_noerr( "umount -r /dev/planetlab/root", log )
100     utils.sysexec_noerr( "vgchange -an", log )
101
102     ROOT_MOUNTED= 0
103     vars['ROOT_MOUNTED']= 0
104
105     log.write( "Unloading modules and chaining booting to new kernel.\n" )
106
107     # further use of log after Upload will only output to screen
108     log.Upload()
109
110     # regardless of whether kexec works or not, we need to stop trying to
111     # run anything
112     cancel_boot_flag= "/tmp/CANCEL_BOOT"
113     utils.sysexec( "touch %s" % cancel_boot_flag, log )
114
115     # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything
116     # to get kexec to work correctly. Even on 3.x cds (2.6 kernel),
117     # there are a few buggy drivers that don't disable their hardware
118     # correctly unless they are first unloaded.
119     
120     utils.sysexec_noerr( "ifconfig eth0 down", log )
121
122     if BOOT_CD_VERSION[0] == 2:
123         utils.sysexec_noerr( "killall dhcpcd", log )
124     elif BOOT_CD_VERSION[0] == 3:
125         utils.sysexec_noerr( "killall dhclient", log )
126         
127     utils.sysexec_noerr( "umount -a -r -t ext2,ext3", log )
128     utils.sysexec_noerr( "modprobe -r lvm-mod", log )
129     
130     try:
131         modules= file("/tmp/loadedmodules","r")
132         
133         for line in modules:
134             module= string.strip(line)
135             if module != "":
136                 log.write( "Unloading %s\n" % module )
137                 utils.sysexec_noerr( "modprobe -r %s" % module, log )
138
139         modules.close()
140
141         modules= file("/proc/modules", "r")
142
143         for line in modules:
144             try:
145                 # Module Size UsageCount UsedBy State LoadAddress
146                 parts= string.split(line)
147
148                 # You can't trust usage count, especially for things
149                 # like network drivers or RAID array drivers. Just try
150                 # and unload a few specific modules that we know cause
151                 # problems during chain boot, such as USB host
152                 # controller drivers (HCDs) (PL6577).
153                 # if int(parts[2]) == 0:
154                 if re.search('_hcd$', parts[0]):
155                     log.write( "Unloading %s\n" % parts[0] )
156                     utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
157             except IndexError, e:
158                 log.write( "Couldn't parse /proc/modules, continuing.\n" )
159     except IOError:
160         log.write( "Couldn't load /tmp/loadedmodules to unload, continuing.\n" )
161
162     try:
163         utils.sysexec( "kexec --force --initrd=/tmp/initrd " \
164                        "--append=ramdisk_size=8192 /tmp/kernel" )
165     except BootManagerException, e:
166         # if kexec fails, we've shut the machine down to a point where nothing
167         # can run usefully anymore (network down, all modules unloaded, file
168         # systems unmounted. write out the error, and cancel the boot process
169
170         log.write( "\n\n" )
171         log.write( "-------------------------------------------------------\n" )
172         log.write( "kexec failed with the following error. Please report\n" )
173         log.write( "this problem to support@planet-lab.org.\n\n" )
174         log.write( str(e) + "\n\n" )
175         log.write( "The boot process has been canceled.\n" )
176         log.write( "-------------------------------------------------------\n\n" )
177
178     return