better handle the kexec failure cases by notifying the user of the exact
[bootmanager.git] / source / steps / ChainBootNode.py
1 import string
2
3 from Exceptions import *
4 import utils
5 import compatibility
6 from systeminfo import systeminfo
7 import BootAPI
8
9
10 def Run( vars, log ):
11     """
12     Load the kernel off of a node and boot to it.
13     This step assumes the disks are mounted on SYSIMG_PATH.
14     
15     Expect the following variables:
16     BOOT_CD_VERSION       A tuple of the current bootcd version
17     SYSIMG_PATH           the path where the system image will be mounted
18                           (always starts with TEMP_PATH)
19     ROOT_MOUNTED          the node root file system is mounted
20
21     Sets the following variables:
22     ROOT_MOUNTED          the node root file system is mounted
23     """
24
25     log.write( "\n\nStep: Chain booting node.\n" )
26
27     # make sure we have the variables we need
28     try:
29         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
30         if BOOT_CD_VERSION == "":
31             raise ValueError, "BOOT_CD_VERSION"
32
33         SYSIMG_PATH= vars["SYSIMG_PATH"]
34         if SYSIMG_PATH == "":
35             raise ValueError, "SYSIMG_PATH"
36         
37     except KeyError, var:
38         raise BootManagerException, "Missing variable in vars: %s\n" % var
39     except ValueError, var:
40         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
41
42     ROOT_MOUNTED= 0
43     if 'ROOT_MOUNTED' in vars.keys():
44         ROOT_MOUNTED= vars['ROOT_MOUNTED']
45     
46     if ROOT_MOUNTED == 0:
47         log.write( "Mounting node partitions\n" )
48
49         # old cds need extra utilities to run lvm
50         if BOOT_CD_VERSION[0] == 2:
51             compatibility.setup_lvm_2x_cd( vars, log )
52             
53         # simply creating an instance of this class and listing the system
54         # block devices will make them show up so vgscan can find the planetlab
55         # volume group
56         systeminfo().get_block_device_list()
57         
58         utils.sysexec( "vgscan", log )
59         utils.sysexec( "vgchange -ay planetlab", log )
60
61         utils.makedirs( SYSIMG_PATH )
62
63         utils.sysexec( "mount /dev/planetlab/root %s" % SYSIMG_PATH, log )
64         utils.sysexec( "mount /dev/planetlab/vservers %s/vservers" %
65                        SYSIMG_PATH, log )
66
67         ROOT_MOUNTED= 1
68         vars['ROOT_MOUNTED']= 1
69         
70
71     node_update_cmd= "/usr/local/planetlab/bin/NodeUpdate.py start noreboot"
72
73     log.write( "Running node update.\n" )
74     utils.sysexec( "chroot %s %s" % (SYSIMG_PATH,node_update_cmd), log )
75
76     log.write( "Updating ssh public host key with PLC.\n" )
77     ssh_host_key= ""
78     try:
79         ssh_host_key_file= file("%s/etc/ssh/ssh_host_rsa_key.pub"%SYSIMG_PATH,"r")
80         ssh_host_key= ssh_host_key_file.read().strip()
81         ssh_host_key_file.close()
82         ssh_host_key_file= None
83     except IOError, e:
84         pass
85     
86     update_vals= {}
87     update_vals['ssh_host_key']= ssh_host_key
88     BootAPI.call_api_function( vars, "BootUpdateNode", (update_vals,) )
89
90
91     log.write( "Copying kernel and initrd for booting.\n" )
92     utils.sysexec( "cp %s/boot/kernel-boot /tmp/kernel" % SYSIMG_PATH, log )
93     utils.sysexec( "cp %s/boot/initrd-boot /tmp/initrd" % SYSIMG_PATH, log )
94
95     log.write( "Unmounting disks.\n" )
96     utils.sysexec_noerr( "chroot %s umount /rcfs" % SYSIMG_PATH, log )
97     utils.sysexec_noerr( "umount -r /dev/planetlab/vservers", log )
98     utils.sysexec_noerr( "umount -r /dev/planetlab/root", log )
99     utils.sysexec_noerr( "vgchange -an", log )
100
101     ROOT_MOUNTED= 0
102     vars['ROOT_MOUNTED']= 0
103
104     if BOOT_CD_VERSION[0] == 2:
105         log.write( "Unloading modules and chaining booting to new kernel.\n" )
106     else:
107         log.write( "Chaining booting to new kernel.\n" )
108
109     # further use of log after Upload will only output to screen
110     log.Upload()
111
112     # regardless of whether kexec works or not, we need to stop trying to
113     # run anything
114     cancel_boot_flag= "/tmp/CANCEL_BOOT"
115     utils.sysexec( "touch %s" % cancel_boot_flag, log )
116
117     # on 2.x cds (2.4 kernel) for sure, we need to shutdown everything to
118     # get kexec to work correctly
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                 utils.sysexec_noerr( "modprobe -r %s" % module, log )
137     except IOError:
138         log.write( "Couldn't load /tmp/loadedmodules to unload, continuing.\n" )
139
140     try:
141         utils.sysexec( "kexec --force --initrd=/tmp/initrd " \
142                        "--append=ramdisk_size=8192 /tmp/kernel" )
143     except BootManagerException, e:
144         # if kexec fails, we've shut the machine down to a point where nothing
145         # can run usefully anymore (network down, all modules unloaded, file
146         # systems unmounted. write out the error, and cancel the boot process
147
148         log.write( "\n\n" )
149         log.write( "-------------------------------------------------------\n" )
150         log.write( "kexec failed with the following error. Please report\n" )
151         log.write( "this problem to support@planet-lab.org.\n\n" )
152         log.write( str(e) + "\n\n" )
153         log.write( "The boot process has been canceled.\n" )
154         log.write( "-------------------------------------------------------\n\n" )
155
156     return