get nodeimage kernel version using nodeimage rpm via chroot; this avoids rpm-db versi...
[bootmanager.git] / source / steps / ValidateNodeInstall.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 import os
10
11 from Exceptions import *
12 import utils
13 import systeminfo
14 import ModelOptions
15
16
17 def Run( vars, log ):
18     """
19     See if a node installation is valid. More checks should certainly be
20     done in the future, but for now, make sure that the sym links kernel-boot
21     exist in /boot
22     
23     Expect the following variables to be set:
24     SYSIMG_PATH              the path where the system image will be mounted
25                              (always starts with TEMP_PATH)
26     ROOT_MOUNTED             the node root file system is mounted
27     NODE_ID                  The db node_id for this machine
28     PLCONF_DIR               The directory to store the configuration file in
29     
30     Set the following variables upon successfully running:
31     ROOT_MOUNTED             the node root file system is mounted
32     """
33
34     log.write( "\n\nStep: Validating node installation.\n" )
35
36     # make sure we have the variables we need
37     try:
38         SYSIMG_PATH= vars["SYSIMG_PATH"]
39         if SYSIMG_PATH == "":
40             raise ValueError, "SYSIMG_PATH"
41
42         NODE_ID= vars["NODE_ID"]
43         if NODE_ID == "":
44             raise ValueError, "NODE_ID"
45
46         PLCONF_DIR= vars["PLCONF_DIR"]
47         if PLCONF_DIR == "":
48             raise ValueError, "PLCONF_DIR"
49         
50         NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
51
52         PARTITIONS= vars["PARTITIONS"]
53         if PARTITIONS == None:
54             raise ValueError, "PARTITIONS"
55
56     except KeyError, var:
57         raise BootManagerException, "Missing variable in vars: %s\n" % var
58     except ValueError, var:
59         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
60
61
62     ROOT_MOUNTED= 0
63     if vars.has_key('ROOT_MOUNTED'):
64         ROOT_MOUNTED= vars['ROOT_MOUNTED']
65
66     # mount the root system image if we haven't already.
67     # capture BootManagerExceptions during the vgscan/change and mount
68     # calls, so we can return 0 instead
69     if ROOT_MOUNTED == 0:
70             
71         # simply creating an instance of this class and listing the system
72         # block devices will make them show up so vgscan can find the planetlab
73         # volume group
74         systeminfo.get_block_device_list(vars, log)
75
76         try:
77             utils.sysexec( "vgscan", log )
78             utils.sysexec( "vgchange -ay planetlab", log )
79         except BootManagerException, e:
80             log.write( "BootManagerException during vgscan/vgchange: %s\n" %
81                        str(e) )
82             return 0
83             
84         utils.makedirs( SYSIMG_PATH )
85
86         for filesystem in ["root"]:
87             try:
88                 # first run fsck to prevent fs corruption from hanging mount...
89                 log.write( "fsck %s file system\n" % filesystem )
90                 utils.sysexec("e2fsck -v -p %s" % (PARTITIONS[filesystem]), log, fsck=True)
91             except BootManagerException, e:
92                 log.write( "BootManagerException during fsck of %s (%s) filesystem : %s\n" %
93                            (filesystem, PARTITIONS[filesystem], str(e)) )
94                 try:
95                     log.write( "Trying to recover filesystem errors on %s\n" % filesystem )
96                     utils.sysexec("e2fsck -v -y %s" % (PARTITIONS[filesystem]),log, fsck=True)
97                 except BootManagerException, e:
98                     log.write( "BootManagerException during trying to recover filesystem errors on %s (%s) filesystem : %s\n" %
99                            (filesystem, PARTITIONS[filesystem], str(e)) )
100                     return -1
101             else:
102                 # disable time/count based filesystems checks
103                 utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % PARTITIONS[filesystem], log)
104         
105         # TODO: add fschk for btrfs vserver volume!! 
106
107         try:
108             # then attempt to mount them
109             log.write( "mounting root file system\n" )
110             utils.sysexec("mount -t ext3 %s %s" % (PARTITIONS["root"],SYSIMG_PATH),log)
111         except BootManagerException, e:
112             log.write( "BootManagerException during mount of /root: %s\n" % str(e) )
113             return -2
114             
115         try:
116             PROC_PATH = "%s/proc" % SYSIMG_PATH
117             utils.makedirs(PROC_PATH)
118             log.write( "mounting /proc\n" )
119             utils.sysexec( "mount -t proc none %s" % PROC_PATH, log )
120         except BootManagerException, e:
121             log.write( "BootManagerException during mount of /proc: %s\n" % str(e) )
122             return -2
123
124         try:
125             VSERVERS_PATH = "%s/vservers" % SYSIMG_PATH
126             utils.makedirs(VSERVERS_PATH)
127             log.write( "mounting vserver partition in root file system\n" )
128             utils.sysexec("mount -t btrfs %s %s" % (PARTITIONS["vservers"], VSERVERS_PATH), log)
129         except BootManagerException, e:
130             log.write( "BootManagerException during mount of /vservers: %s\n" % str(e) )
131             return -2
132
133         ROOT_MOUNTED= 1
134         vars['ROOT_MOUNTED']= 1
135         
136     # check if the base kernel is installed 
137     # these 2 links are created by our kernel's post-install scriplet
138     log.write("Checking for a custom kernel\n")
139     try:
140         # Use chroot to call rpm, b/c the bootimage&nodeimage rpm-versions may not work together
141         kversion = os.popen("chroot %s rpm -qa kernel | tail -1 | cut -c 8-" % SYSIMG_PATH).read().rstrip()
142         os.stat("%s/boot/vmlinuz-%s" % (SYSIMG_PATH,kversion))
143     except OSError, e:            
144         log.write( "Couldn't locate base kernel (you might be using the stock kernel).\n")
145         return -3
146
147     # check if the model specified kernel is installed
148     option = ''
149     if NODE_MODEL_OPTIONS & ModelOptions.SMP:
150         option = 'smp'
151         try:
152             os.stat("%s/boot/kernel-boot%s" % (SYSIMG_PATH,option))
153         except OSError, e:
154             # smp kernel is not there; remove option from modeloptions
155             # such that the rest of the code base thinks we are just
156             # using the base kernel.
157             NODE_MODEL_OPTIONS = NODE_MODEL_OPTIONS & ~ModelOptions.SMP
158             vars["NODE_MODEL_OPTIONS"] = NODE_MODEL_OPTIONS
159             log.write( "WARNING: Couldn't locate smp kernel.\n")
160             
161     # write out the node id to /etc/planetlab/node_id. if this fails, return
162     # 0, indicating the node isn't a valid install.
163     try:
164         node_id_file_path= "%s/%s/node_id" % (SYSIMG_PATH,PLCONF_DIR)
165         node_id_file= file( node_id_file_path, "w" )
166         node_id_file.write( str(NODE_ID) )
167         node_id_file.close()
168         node_id_file= None
169         log.write( "Updated /etc/planetlab/node_id\n" )
170     except IOError, e:
171         log.write( "Unable to write out /etc/planetlab/node_id\n" )
172         return 0
173
174     log.write( "Node installation appears to be ok\n" )
175     
176     return 1