upgrade mode was not working; CheckForNewDisks was erroneously taking the disk as...
[bootmanager.git] / source / steps / CheckForNewDisks.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 string
10
11 import InstallPartitionDisks
12 from Exceptions import *
13 import systeminfo
14 import utils
15 import os
16
17 import ModelOptions
18
19
20 def Run(vars, log):
21     """
22     Find any new large block devices we can add to the vservers volume group
23     
24     Expect the following variables to be set:
25     SYSIMG_PATH          the path where the system image will be mounted
26     MINIMUM_DISK_SIZE       any disks smaller than this size, in GB, are not used
27     NODE_MODEL_OPTIONS   the node's model options
28     
29     Set the following variables upon successfully running:
30     ROOT_MOUNTED             the node root file system is mounted
31     """
32
33     log.write("\n\nStep: Checking for unused disks to add to LVM.\n")
34
35     # make sure we have the variables we need
36     try:
37         SYSIMG_PATH = vars["SYSIMG_PATH"]
38         if SYSIMG_PATH == "":
39             raise ValueError("SYSIMG_PATH")
40
41         MINIMUM_DISK_SIZE = int(vars["MINIMUM_DISK_SIZE"])
42
43         PARTITIONS = vars["PARTITIONS"]
44         if PARTITIONS == None:
45             raise ValueError("PARTITIONS")
46         
47         NODE_MODEL_OPTIONS = vars["NODE_MODEL_OPTIONS"]
48     except KeyError as var:
49         raise BootManagerException("Missing variable in vars: {}\n".format(var))
50     except ValueError as var:
51         raise BootManagerException("Variable in vars, shouldn't be: {}\n".format(var))
52
53     devices_dict = systeminfo.get_block_devices_dict(vars, log)
54     
55     # will contain the new devices to add to the volume group
56     new_devices = []
57
58     # total amount of new space in gb
59     extended_gb_size = 0
60     
61     for device, details in devices_dict.items():
62
63         (major, minor, blocks, gb_size, readonly) = details
64
65         if device[:14] == "/dev/planetlab":
66             log.write("Skipping device {} in volume group.\n".format(device))
67             continue
68
69         if readonly:
70             log.write("Skipping read only device {}\n".format(device))
71             continue
72
73         if gb_size < MINIMUM_DISK_SIZE:
74             log.write("Skipping too small device {} ({:4.2f}) Gb\n"\
75                       .format(device, gb_size))
76             continue
77
78         log.write("Checking device {} to see if it is part " \
79                    "of the volume group.\n".format(device))
80
81         # Thierry - June 2015
82         # when introducing the 'upgrade' verb, we ran into the situation
83         # where 'pvdisplay' at this point displays e.g. /dev/sda, instead
84         # of /dev/sda1
85         # we thus consider that if either of these is known, then
86         # the disk is already part of LVM
87         first_partition = InstallPartitionDisks.get_partition_path_from_device(device, vars, log)
88         probe_first_part = "pvdisplay {} | grep -q planetlab".format(first_partition)
89         probe_device     = "pvdisplay {} | grep -q planetlab".format(device)
90         already_added = utils.sysexec_noerr(probe_first_part, log, shell=True) \
91                      or utils.sysexec_noerr(probe_device, log, shell=True) 
92
93         if already_added:
94             log.write("It appears {} is part of the volume group, continuing.\n"\
95                       .format(device))
96             continue
97
98         # just to be extra paranoid, ignore the device if it already has
99         # an lvm partition on it (new disks won't have this, and that is
100         # what this code is for, so it should be ok).
101         cmd = "parted --script --list {} | grep -q lvm$".format(device)
102         has_lvm = utils.sysexec_noerr(cmd, log, shell=True)
103         if has_lvm:
104             log.write("It appears {} has lvm already setup on it.\n".format(device))
105             paranoid = False
106             if paranoid:
107                 log.write("Too paranoid to add {} to vservers lvm.\n".format(device))
108                 continue
109         
110         if not InstallPartitionDisks.single_partition_device(device, vars, log):
111             log.write("Unable to partition {}, not using it.\n".format(device))
112             continue
113
114         log.write("Successfully partitioned {}\n".format(device))
115
116         if NODE_MODEL_OPTIONS & ModelOptions.RAWDISK:
117             log.write("Running on a raw disk node, not using it.\n")
118             continue
119
120         part_path = InstallPartitionDisks.get_partition_path_from_device(device,
121                                                                          vars, log)
122
123         log.write("Attempting to add {} to the volume group\n".format(device))
124
125         if not InstallPartitionDisks.create_lvm_physical_volume(part_path,
126                                                                  vars, log):
127             log.write("Unable to create lvm physical volume {}, not using it.\n"\
128                       .format(part_path))
129             continue
130
131         log.write("Adding {} to list of devices to add to "
132                    "planetlab volume group.\n".format(device))
133
134         extended_gb_size = extended_gb_size + gb_size
135         new_devices.append(part_path)
136         
137
138     if len(new_devices) > 0:
139
140         log.write("Extending planetlab volume group.\n")
141         
142         log.write("Unmounting disks.\n")
143         try:
144             # backwards compat, though, we should never hit this case post PL 3.2
145             os.stat("{}/rcfs/taskclass".format(SYSIMG_PATH))
146             utils.sysexec_chroot_noerr(SYSIMG_PATH, "umount /rcfs", log)
147         except OSError as e:
148             pass
149
150         # umount in order to extend disk size
151         utils.sysexec_noerr("umount {}/proc".format(SYSIMG_PATH), log)
152         utils.sysexec_noerr("umount {}/vservers".format(SYSIMG_PATH), log)
153         utils.sysexec_noerr("umount {}".format(SYSIMG_PATH), log)
154         utils.sysexec("vgchange -an", log)
155         
156         vars['ROOT_MOUNTED'] = 0
157
158         while True:
159             cmd = "vgextend planetlab {}".format(" ".join(new_devices))
160             if not utils.sysexec_noerr(cmd, log):
161                 log.write("Failed to add physical volumes {} to "\
162                            "volume group, continuing.\n".format(" ".join(new_devices)))
163                 res = 1
164                 break
165             
166             # now, get the number of unused extents, and extend the vserver
167             # logical volume by that much.
168             remaining_extents = \
169                InstallPartitionDisks.get_remaining_extents_on_vg(vars, log)
170
171             log.write("Extending vservers logical volume.\n")
172             utils.sysexec("vgchange -ay", log)
173             cmd = "lvextend -l +{} {}".format(remaining_extents, PARTITIONS["vservers"])
174             if not utils.sysexec_noerr(cmd, log):
175                 log.write("Failed to extend vservers logical volume, continuing\n")
176                 res = 1
177                 break
178
179             log.write("making the ext filesystem match new logical volume size.\n")
180
181             vars['ROOT_MOUNTED'] = 1
182             cmd = "mount {} {}".format(PARTITIONS["root"], SYSIMG_PATH)
183             utils.sysexec_noerr(cmd, log)
184             cmd = "mount {} {}/vservers".format(PARTITIONS["vservers"], SYSIMG_PATH)
185             utils.sysexec_noerr(cmd, log)
186             cmd = "resize2fs {}".format(PARTITIONS["vservers"])
187             resize = utils.sysexec_noerr(cmd,log)
188             utils.sysexec_noerr("umount {}/vservers".format(SYSIMG_PATH), log)
189             utils.sysexec_noerr("umount {}".format(SYSIMG_PATH), log)
190             vars['ROOT_MOUNTED'] = 0
191
192             utils.sysexec("vgchange -an", log)
193
194             if not resize:
195                 log.write("Failed to resize vservers partition, continuing.\n")
196                 res = 1
197                 break
198             else:
199                 log.write("Extended vservers partition by {:4.2f} Gb\n"\
200                           .format(extended_gb_size))
201                 res = 1
202                 break
203
204     else:
205         log.write("No new disk devices to add to volume group.\n")
206         res = 1
207
208     return res