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