d7c64688eea55fc0ffd22d92f4bbc7d9ff6ffe7a
[bootmanager.git] / source / steps / InstallPartitionDisks.py
1 #!/usr/bin/python2
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 # expected /proc/partitions format
9
10 import os, sys
11 import string
12 import popen2
13
14
15 from Exceptions import *
16 import utils
17 import BootServerRequest
18 import compatibility
19
20 import ModelOptions
21
22 def Run( vars, log ):
23     """
24     Setup the block devices for install, partition them w/ LVM
25     
26     Expect the following variables from the store:
27     INSTALL_BLOCK_DEVICES    list of block devices to install onto
28     TEMP_PATH                somewhere to store what we need to run
29     ROOT_SIZE                the size of the root logical volume
30     SWAP_SIZE                the size of the swap partition
31     BOOT_CD_VERSION          A tuple of the current bootcd version
32     """
33
34     log.write( "\n\nStep: Install: partitioning disks.\n" )
35         
36     # make sure we have the variables we need
37     try:
38         TEMP_PATH= vars["TEMP_PATH"]
39         if TEMP_PATH == "":
40             raise ValueError, "TEMP_PATH"
41
42         INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
43         if( len(INSTALL_BLOCK_DEVICES) == 0 ):
44             raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
45
46         ROOT_SIZE= vars["ROOT_SIZE"]
47         if ROOT_SIZE == "" or ROOT_SIZE == 0:
48             raise ValueError, "ROOT_SIZE invalid"
49
50         SWAP_SIZE= vars["SWAP_SIZE"]
51         if SWAP_SIZE == "" or SWAP_SIZE == 0:
52             raise ValueError, "SWAP_SIZE invalid"
53
54         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
55         if BOOT_CD_VERSION == "":
56             raise ValueError, "BOOT_CD_VERSION"
57
58         NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
59
60         PARTITIONS= vars["PARTITIONS"]
61         if PARTITIONS == None:
62             raise ValueError, "PARTITIONS"
63
64     except KeyError, var:
65         raise BootManagerException, "Missing variable in vars: %s\n" % var
66     except ValueError, var:
67         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
68
69     bs_request= BootServerRequest.BootServerRequest()
70
71     
72     # old cds need extra utilities to partition disks and setup lvm
73     if BOOT_CD_VERSION[0] == 2:
74         compatibility.setup_partdisks_2x_cd( vars, log )
75
76     # disable swap if its on
77     utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
78
79     # shutdown and remove any lvm groups/volumes
80     utils.sysexec_noerr( "vgscan", log )
81     utils.sysexec_noerr( "vgchange -ay", log )        
82     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["root"], log )
83     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["swap"], log )
84     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["vservers"], log )
85     utils.sysexec_noerr( "vgchange -an", log )
86     utils.sysexec_noerr( "vgremove planetlab", log )
87
88     log.write( "Running vgscan for devices\n" )
89     utils.sysexec_noerr( "vgscan", log )
90     
91     used_devices= []
92
93     for device in INSTALL_BLOCK_DEVICES:
94
95         if single_partition_device( device, vars, log ):
96             used_devices.append( device )
97             log.write( "Successfully initialized %s\n" % device )
98         else:
99             log.write( "Unable to partition %s, not using it.\n" % device )
100             continue
101
102     # list of devices to be used with vgcreate
103     vg_device_list= ""
104
105     # initialize the physical volumes
106     for device in used_devices:
107
108         part_path= get_partition_path_from_device( device, vars, log )
109         
110         if not create_lvm_physical_volume( part_path, vars, log ):
111             raise BootManagerException, "Could not create lvm physical volume " \
112                   "on partition %s" % part_path
113         
114         vg_device_list = vg_device_list + " " + part_path
115
116     # create an lvm volume group
117     utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
118
119     # create swap logical volume
120     utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
121
122     # create root logical volume
123     utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
124
125     # create vservers logical volume with all remaining space
126     # first, we need to get the number of remaining extents we can use
127     remaining_extents= get_remaining_extents_on_vg( vars, log )
128     
129     utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
130
131     # activate volume group (should already be active)
132     #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
133
134     # make swap
135     utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
136
137     # check if badhd option has been set
138     option = ''
139     txt = ''
140     if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
141         option = '-c'
142         txt = " with bad block search enabled, which may take a while"
143     
144     # filesystems partitions names and their corresponding
145     # reserved-blocks-percentages
146     filesystems = {"root":5,"vservers":0}
147
148     # make the file systems
149     for fs in filesystems.keys():
150         # get the reserved blocks percentage
151         rbp = filesystems[fs]
152         devname = PARTITIONS[fs]
153         log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
154         utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
155
156     # save the list of block devices in the log
157     log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
158
159     # list of block devices used may be updated
160     vars["INSTALL_BLOCK_DEVICES"]= used_devices
161
162     return 1
163
164
165
166 def single_partition_device( device, vars, log ):
167     """
168     initialize a disk by removing the old partition tables,
169     and creating a new single partition that fills the disk.
170
171     return 1 if sucessful, 0 otherwise
172     """
173
174     BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
175     if BOOT_CD_VERSION[0] == 2:
176         compatibility.setup_partdisks_2x_cd( vars, log )
177
178     import parted
179     
180     lvm_flag= parted.partition_flag_get_by_name('lvm')
181     
182     try:
183         # wipe the old partition table
184         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
185
186         # get the device
187         dev= parted.PedDevice.get(device)
188
189         # 2.x cds have different libparted that 3.x cds, and they have
190         # different interfaces
191         if BOOT_CD_VERSION[0] == 3:
192
193             # create a new partition table
194             disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
195
196             # create one big partition on each block device
197             constraint= dev.constraint_any()
198
199             new_part= disk.partition_new(
200                 parted.PARTITION_PRIMARY,
201                 parted.file_system_type_get("ext2"),
202                 0, 1 )
203
204             # make it an lvm partition
205             new_part.set_flag(lvm_flag,1)
206
207             # actually add the partition to the disk
208             disk.add_partition(new_part, constraint)
209
210             disk.maximize_partition(new_part,constraint)
211
212             disk.commit()
213             del disk
214         else:
215             # create a new partition table
216             dev.disk_create(parted.disk_type_get("msdos"))
217
218             # get the disk
219             disk= parted.PedDisk.open(dev)
220
221                 # create one big partition on each block device
222             part= disk.next_partition()
223             while part:
224                 if part.type == parted.PARTITION_FREESPACE:
225                     new_part= disk.partition_new(
226                         parted.PARTITION_PRIMARY,
227                         parted.file_system_type_get("ext2"),
228                         part.geom.start,
229                         part.geom.end )
230
231                     constraint = disk.constraint_any()
232
233                     # make it an lvm partition
234                     new_part.set_flag(lvm_flag,1)
235
236                     # actually add the partition to the disk
237                     disk.add_partition(new_part, constraint)
238
239                     break
240
241                 part= disk.next_partition(part)
242
243             disk.write()
244             disk.close()
245             del disk
246             
247     except BootManagerException, e:
248         log.write( "BootManagerException while running: %s\n" % str(e) )
249         return 0
250
251     except parted.error, e:
252         log.write( "parted exception while running: %s\n" % str(e) )
253         return 0
254                    
255     return 1
256
257
258
259 def create_lvm_physical_volume( part_path, vars, log ):
260     """
261     make the specificed partition a lvm physical volume.
262
263     return 1 if successful, 0 otherwise.
264     """
265
266     try:
267         # again, wipe any old data, this time on the partition
268         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
269         ### patch Thierry Parmentelat, required on some hardware
270         import time
271         time.sleep(1)
272         utils.sysexec( "pvcreate -ffy %s" % part_path, log )
273     except BootManagerException, e:
274         log.write( "create_lvm_physical_volume failed.\n" )
275         return 0
276
277     return 1
278
279
280
281 def get_partition_path_from_device( device, vars, log ):
282     """
283     given a device, return the path of the first partition on the device
284     """
285
286     BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
287         
288     # those who wrote the cciss driver just had to make it difficult
289     if BOOT_CD_VERSION[0] == 3:
290         cciss_test= "/dev/cciss"
291         if device[:len(cciss_test)] == cciss_test:
292             part_path= device + "p1"
293         else:
294             part_path= device + "1"
295     else:
296         # since device ends in /disc, we need to make it end in
297         # /part1 to indicate the first partition (for devfs based 2.x cds)
298         dev_parts= string.split(device,"/")
299         dev_parts[len(dev_parts)-1]= "part1"
300         part_path= string.join(dev_parts,"/")
301
302     return part_path
303
304
305
306 def get_remaining_extents_on_vg( vars, log ):
307     """
308     return the free amount of extents on the planetlab volume group
309     """
310     
311     c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
312     result= string.strip(c_stdout.readline())
313     c_stdout.close()
314     c_stdin.close()
315     remaining_extents= string.split(result,":")[15]
316     
317     return remaining_extents