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