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