Fall back to up kernel if smp model option is set but the smp kernel is not installed
[bootmanager.git] / source / steps / ValidateNodeInstall.py
1 import os
2
3 from Exceptions import *
4 import utils
5 from systeminfo import systeminfo
6 import compatibility
7 import ModelOptions
8
9
10 def Run( vars, log ):
11     """
12     See if a node installation is valid. More checks should certainly be
13     done in the future, but for now, make sure that the sym links kernel-boot
14     and initrd-boot exist in /boot
15     
16     Expect the following variables to be set:
17     SYSIMG_PATH              the path where the system image will be mounted
18                              (always starts with TEMP_PATH)
19     BOOT_CD_VERSION          A tuple of the current bootcd version
20     ROOT_MOUNTED             the node root file system is mounted
21     NODE_ID                  The db node_id for this machine
22     PLCONF_DIR               The directory to store the configuration file in
23     
24     Set the following variables upon successfully running:
25     ROOT_MOUNTED             the node root file system is mounted
26     """
27
28     log.write( "\n\nStep: Validating node installation.\n" )
29
30     # make sure we have the variables we need
31     try:
32         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
33         if BOOT_CD_VERSION == "":
34             raise ValueError, "BOOT_CD_VERSION"
35
36         SYSIMG_PATH= vars["SYSIMG_PATH"]
37         if SYSIMG_PATH == "":
38             raise ValueError, "SYSIMG_PATH"
39
40         NODE_ID= vars["NODE_ID"]
41         if NODE_ID == "":
42             raise ValueError, "NODE_ID"
43
44         PLCONF_DIR= vars["PLCONF_DIR"]
45         if PLCONF_DIR == "":
46             raise ValueError, "PLCONF_DIR"
47         
48         NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
49
50     except KeyError, var:
51         raise BootManagerException, "Missing variable in vars: %s\n" % var
52     except ValueError, var:
53         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
54
55
56     ROOT_MOUNTED= 0
57     if 'ROOT_MOUNTED' in vars.keys():
58         ROOT_MOUNTED= vars['ROOT_MOUNTED']
59
60     # mount the root system image if we haven't already.
61     # capture BootManagerExceptions during the vgscan/change and mount
62     # calls, so we can return 0 instead
63     if ROOT_MOUNTED == 0:
64         # old cds need extra utilities to run lvm
65         if BOOT_CD_VERSION[0] == 2:
66             compatibility.setup_lvm_2x_cd( vars, log )
67             
68         # simply creating an instance of this class and listing the system
69         # block devices will make them show up so vgscan can find the planetlab
70         # volume group
71         systeminfo().get_block_device_list()
72
73         try:
74             utils.sysexec( "vgscan", log )
75             utils.sysexec( "vgchange -ay planetlab", log )
76         except BootManagerException, e:
77             log.write( "BootManagerException during vgscan/vgchange: %s\n" %
78                        str(e) )
79             return 0
80             
81         utils.makedirs( SYSIMG_PATH )
82
83         try:
84             utils.sysexec( "mount /dev/planetlab/root %s" % SYSIMG_PATH, log )
85             utils.sysexec( "mount /dev/planetlab/vservers %s/vservers" %
86                            SYSIMG_PATH, log )
87             utils.sysexec( "mount -t proc none %s/proc" % SYSIMG_PATH, log )
88         except BootManagerException, e:
89             log.write( "BootManagerException during vgscan/vgchange: %s\n" %
90                        str(e) )
91             return 0
92
93         ROOT_MOUNTED= 1
94         vars['ROOT_MOUNTED']= 1
95         
96     
97     # check if the base kernel is installed
98     try:
99         os.stat("%s/boot/kernel-boot" % SYSIMG_PATH)
100         os.stat("%s/boot/initrd-boot" % SYSIMG_PATH)
101     except OSError, e:            
102         log.write( "FATAL: Couldn't locate base kernel.\n")                
103         return 0
104
105     # check if the model specified kernel is installed
106     option = ''
107     if NODE_MODEL_OPTIONS & ModelOptions.SMP:
108         option = 'smp'
109         try:
110             os.stat("%s/boot/kernel-boot%s" % (SYSIMG_PATH,option))
111             os.stat("%s/boot/initrd-boot%s" % (SYSIMG_PATH,option))
112         except OSError, e:
113             # smp kernel is not there; remove option from modeloptions
114             # such that the rest of the code base thinks we are just
115             # using the base kernel.
116             NODE_MODEL_OPTIONS = NODE_MODEL_OPTIONS & ~ModelOptions.SMP
117             vars["NODE_MODEL_OPTIONS"] = NODE_MODEL_OPTIONS
118             log.write( "WARNING: Couldn't locate smp kernel.\n")
119             
120     # write out the node id to /etc/planetlab/node_id. if this fails, return
121     # 0, indicating the node isn't a valid install.
122     try:
123         node_id_file_path= "%s/%s/node_id" % (SYSIMG_PATH,PLCONF_DIR)
124         node_id_file= file( node_id_file_path, "w" )
125         node_id_file.write( str(NODE_ID) )
126         node_id_file.close()
127         node_id_file= None
128         log.write( "Updated /etc/planetlab/node_id\n" )
129     except IOError, e:
130         log.write( "Unable to write out /etc/planetlab/node_id\n" )
131         return 0
132
133     log.write( "Everything appears to be ok\n" )
134     
135     return 1