- fix some log messages
[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 chain 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     except IOError:
141         log.write( "Couldn't read /tmp/loadedmodules, continuing.\n" )
142
143     try:
144         modules= file("/proc/modules", "r")
145
146         # Get usage count for USB
147         usb_usage = 0
148         for line in modules:
149             try:
150                 # Module Size UsageCount UsedBy State LoadAddress
151                 parts= string.split(line)
152
153                 if parts[0] == "usb_storage":
154                     usb_usage += int(parts[2])
155             except IndexError, e:
156                 log.write( "Couldn't parse /proc/modules, continuing.\n" )
157
158         modules.seek(0)
159
160         for line in modules:
161             try:
162                 # Module Size UsageCount UsedBy State LoadAddress
163                 parts= string.split(line)
164
165                 # While we would like to remove all "unused" modules,
166                 # you can't trust usage count, especially for things
167                 # like network drivers or RAID array drivers. Just try
168                 # and unload a few specific modules that we know cause
169                 # problems during chain boot, such as USB host
170                 # controller drivers (HCDs) (PL6577).
171                 # if int(parts[2]) == 0:
172                 if re.search('_hcd$', parts[0]):
173                     if usb_usage > 0:
174                         log.write( "NOT unloading %s since USB may be in use\n" % parts[0] )
175                     else:
176                         log.write( "Unloading %s\n" % parts[0] )
177                         utils.sysexec_noerr( "modprobe -r %s" % parts[0], log )
178             except IndexError, e:
179                 log.write( "Couldn't parse /proc/modules, continuing.\n" )
180     except IOError:
181         log.write( "Couldn't read /proc/modules, continuing.\n" )
182
183     try:
184         utils.sysexec( "kexec --force --initrd=/tmp/initrd " \
185                        "--append=ramdisk_size=8192 /tmp/kernel" )
186     except BootManagerException, e:
187         # if kexec fails, we've shut the machine down to a point where nothing
188         # can run usefully anymore (network down, all modules unloaded, file
189         # systems unmounted. write out the error, and cancel the boot process
190
191         log.write( "\n\n" )
192         log.write( "-------------------------------------------------------\n" )
193         log.write( "kexec failed with the following error. Please report\n" )
194         log.write( "this problem to support@planet-lab.org.\n\n" )
195         log.write( str(e) + "\n\n" )
196         log.write( "The boot process has been canceled.\n" )
197         log.write( "-------------------------------------------------------\n\n" )
198
199     return