- one second sleep required after wiping partition table on some hardware
[bootmanager.git] / source / steps / InstallPartitionDisks.py
1 # Copyright (c) 2003 Intel Corporation
2 # All rights reserved.
3
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7
8 #     * Redistributions of source code must retain the above copyright
9 #       notice, this list of conditions and the following disclaimer.
10
11 #     * Redistributions in binary form must reproduce the above
12 #       copyright notice, this list of conditions and the following
13 #       disclaimer in the documentation and/or other materials provided
14 #       with the distribution.
15
16 #     * Neither the name of the Intel Corporation nor the names of its
17 #       contributors may be used to endorse or promote products derived
18 #       from this software without specific prior written permission.
19
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 # EXPORT LAWS: THIS LICENSE ADDS NO RESTRICTIONS TO THE EXPORT LAWS OF
33 # YOUR JURISDICTION. It is licensee's responsibility to comply with any
34 # export regulations applicable in licensee's jurisdiction. Under
35 # CURRENT (May 2000) U.S. export regulations this software is eligible
36 # for export from the U.S. and can be downloaded by or otherwise
37 # exported or reexported worldwide EXCEPT to U.S. embargoed destinations
38 # which include Cuba, Iraq, Libya, North Korea, Iran, Syria, Sudan,
39 # Afghanistan and any other country to which the U.S. has embargoed
40 # goods and services.
41
42
43 import os, sys
44 import string
45 import popen2
46
47
48 from Exceptions import *
49 import utils
50 import BootServerRequest
51 import compatibility
52
53
54
55 def Run( vars, log ):
56     """
57     Setup the block devices for install, partition them w/ LVM
58     
59     Expect the following variables from the store:
60     INSTALL_BLOCK_DEVICES    list of block devices to install onto
61     TEMP_PATH                somewhere to store what we need to run
62     ROOT_SIZE                the size of the root logical volume
63     SWAP_SIZE                the size of the swap partition
64     BOOT_CD_VERSION          A tuple of the current bootcd version
65     
66     Sets the following variables:
67     PARTITIONS               diction of generic part. types (root/swap)
68                              and their associated devices.
69                              Current keys/values:
70                                  root    /dev/planetlab/root
71                                  swap    /dev/planetlab/swap
72     
73     """
74
75     log.write( "\n\nStep: Install: partitioning disks.\n" )
76         
77     # make sure we have the variables we need
78     try:
79         TEMP_PATH= vars["TEMP_PATH"]
80         if TEMP_PATH == "":
81             raise ValueError, "TEMP_PATH"
82
83         INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
84         if( len(INSTALL_BLOCK_DEVICES) == 0 ):
85             raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
86
87         ROOT_SIZE= vars["ROOT_SIZE"]
88         if ROOT_SIZE == "" or ROOT_SIZE == 0:
89             raise ValueError, "ROOT_SIZE invalid"
90
91         SWAP_SIZE= vars["SWAP_SIZE"]
92         if SWAP_SIZE == "" or SWAP_SIZE == 0:
93             raise ValueError, "SWAP_SIZE invalid"
94
95         BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
96         if BOOT_CD_VERSION == "":
97             raise ValueError, "BOOT_CD_VERSION"
98
99     except KeyError, var:
100         raise BootManagerException, "Missing variable in vars: %s\n" % var
101     except ValueError, var:
102         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
103
104     bs_request= BootServerRequest.BootServerRequest()
105
106     
107     # old cds need extra utilities to partition disks and setup lvm
108     if BOOT_CD_VERSION[0] == 2:
109         compatibility.setup_partdisks_2x_cd( vars, log )
110
111     import parted
112         
113     # define the basic partition paths
114     PARTITIONS= {}
115     PARTITIONS["root"]= "/dev/planetlab/root"
116     PARTITIONS["swap"]= "/dev/planetlab/swap"
117     PARTITIONS["vservers"]= "/dev/planetlab/vservers"
118     # Linux 2.6 mounts LVM with device mapper
119     PARTITIONS["mapper-root"]= "/dev/mapper/planetlab-root"
120     PARTITIONS["mapper-swap"]= "/dev/mapper/planetlab-swap"
121     PARTITIONS["mapper-vservers"]= "/dev/mapper/planetlab-vservers"
122     vars["PARTITIONS"]= PARTITIONS
123
124     
125     # disable swap if its on
126     utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
127
128     # shutdown and remove any lvm groups/volumes
129     utils.sysexec_noerr( "vgscan", log )
130     utils.sysexec_noerr( "vgchange -ay", log )        
131     utils.sysexec_noerr( "lvremove -f /dev/planetlab/root", log )
132     utils.sysexec_noerr( "lvremove -f /dev/planetlab/swap", log )
133     utils.sysexec_noerr( "lvremove -f /dev/planetlab/vservers", log )
134     utils.sysexec_noerr( "vgchange -an", log )
135     utils.sysexec_noerr( "vgremove planetlab", log )
136
137     log.write( "Running vgscan for devices\n" )
138     utils.sysexec_noerr( "vgscan", log )
139     
140     used_devices= []
141
142     for device in INSTALL_BLOCK_DEVICES:
143
144         if single_partition_device( device, vars, log ):
145             used_devices.append( device )
146             log.write( "Successfully initialized %s\n" % device )
147         else:
148             log.write( "Unable to partition %s, not using it.\n" % device )
149             continue
150
151     # list of devices to be used with vgcreate
152     vg_device_list= ""
153
154     # initialize the physical volumes
155     for device in used_devices:
156
157         part_path= get_partition_path_from_device( device, vars, log )
158         
159         if not create_lvm_physical_volume( part_path, vars, log ):
160             raise BootManagerException, "Could not create lvm physical volume " \
161                   "on partition %s" % part_path
162         
163         vg_device_list = vg_device_list + " " + part_path
164
165     # create an lvm volume group
166     utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
167
168     # create swap logical volume
169     utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
170
171     # create root logical volume
172     utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
173
174     # create vservers logical volume with all remaining space
175     # first, we need to get the number of remaining extents we can use
176     remaining_extents= get_remaining_extents_on_vg( vars, log )
177     
178     utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
179
180     # activate volume group (should already be active)
181     #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
182
183     # make swap
184     utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
185
186     # make root file system
187     utils.sysexec( "mkfs.ext2 -j %s" % PARTITIONS["root"], log )
188
189     # make vservers file system
190     utils.sysexec( "mkfs.ext2 -m 0 -j %s" % PARTITIONS["vservers"], log )
191
192     # save the list of block devices in the log
193     log.write( "Block devices used (in lvm):\n" )
194     log.write( repr(used_devices) + "\n" )
195     log.write( "End of block devices used (in lvm).\n" )
196
197     # list of block devices used may be updated
198     vars["INSTALL_BLOCK_DEVICES"]= used_devices
199
200     return 1
201
202
203
204 def single_partition_device( device, vars, log ):
205     """
206     initialize a disk by removing the old partition tables,
207     and creating a new single partition that fills the disk.
208
209     return 1 if sucessful, 0 otherwise
210     """
211
212     BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
213     if BOOT_CD_VERSION[0] == 2:
214         compatibility.setup_partdisks_2x_cd( vars, log )
215
216     import parted
217     
218     lvm_flag= parted.partition_flag_get_by_name('lvm')
219     
220     try:
221         # wipe the old partition table
222         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
223
224         # get the device
225         dev= parted.PedDevice.get(device)
226
227         # 2.x cds have different libparted that 3.x cds, and they have
228         # different interfaces
229         if BOOT_CD_VERSION[0] == 3:
230
231             # create a new partition table
232             disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
233
234             # create one big partition on each block device
235             constraint= dev.constraint_any()
236
237             new_part= disk.partition_new(
238                 parted.PARTITION_PRIMARY,
239                 parted.file_system_type_get("ext2"),
240                 0, 1 )
241
242             # make it an lvm partition
243             new_part.set_flag(lvm_flag,1)
244
245             # actually add the partition to the disk
246             disk.add_partition(new_part, constraint)
247
248             disk.maximize_partition(new_part,constraint)
249
250             disk.commit()
251             del disk
252         else:
253             # create a new partition table
254             dev.disk_create(parted.disk_type_get("msdos"))
255
256             # get the disk
257             disk= parted.PedDisk.open(dev)
258
259                 # create one big partition on each block device
260             part= disk.next_partition()
261             while part:
262                 if part.type == parted.PARTITION_FREESPACE:
263                     new_part= disk.partition_new(
264                         parted.PARTITION_PRIMARY,
265                         parted.file_system_type_get("ext2"),
266                         part.geom.start,
267                         part.geom.end )
268
269                     constraint = disk.constraint_any()
270
271                     # make it an lvm partition
272                     new_part.set_flag(lvm_flag,1)
273
274                     # actually add the partition to the disk
275                     disk.add_partition(new_part, constraint)
276
277                     break
278
279                 part= disk.next_partition(part)
280
281             disk.write()
282             disk.close()
283             del disk
284             
285     except BootManagerException, e:
286         log.write( "BootManagerException while running: %s\n" % str(e) )
287         return 0
288
289     except parted.error, e:
290         log.write( "parted exception while running: %s\n" % str(e) )
291         return 0
292                    
293     return 1
294
295
296
297 def create_lvm_physical_volume( part_path, vars, log ):
298     """
299     make the specificed partition a lvm physical volume.
300
301     return 1 if successful, 0 otherwise.
302     """
303
304     try:
305         # again, wipe any old data, this time on the partition
306         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
307         ### patch Thierry Parmentelat, required on some hardware
308         import time
309         time.sleep(1)
310         utils.sysexec( "pvcreate -fy %s" % part_path, log )
311     except BootManagerException, e:
312         log.write( "create_lvm_physical_volume failed.\n" )
313         return 0
314
315     return 1
316
317
318
319 def get_partition_path_from_device( device, vars, log ):
320     """
321     given a device, return the path of the first partition on the device
322     """
323
324     BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
325         
326     # those who wrote the cciss driver just had to make it difficult
327     if BOOT_CD_VERSION[0] == 3:
328         cciss_test= "/dev/cciss"
329         if device[:len(cciss_test)] == cciss_test:
330             part_path= device + "p1"
331         else:
332             part_path= device + "1"
333     else:
334         # since device ends in /disc, we need to make it end in
335         # /part1 to indicate the first partition (for devfs based 2.x cds)
336         dev_parts= string.split(device,"/")
337         dev_parts[len(dev_parts)-1]= "part1"
338         part_path= string.join(dev_parts,"/")
339
340     return part_path
341
342
343
344 def get_remaining_extents_on_vg( vars, log ):
345     """
346     return the free amount of extents on the planetlab volume group
347     """
348     
349     c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
350     result= string.strip(c_stdout.readline())
351     c_stdout.close()
352     c_stdin.close()
353     remaining_extents= string.split(result,":")[15]
354     
355     return remaining_extents