1 # Copyright (c) 2003 Intel Corporation
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
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.
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.
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.
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
48 from Exceptions import *
50 import BootServerRequest
57 Setup the block devices for install, partition them w/ LVM
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
66 Sets the following variables:
67 PARTITIONS diction of generic part. types (root/swap)
68 and their associated devices.
70 root /dev/planetlab/root
71 swap /dev/planetlab/swap
75 log.write( "\n\nStep: Install: partitioning disks.\n" )
77 # make sure we have the variables we need
79 TEMP_PATH= vars["TEMP_PATH"]
81 raise ValueError, "TEMP_PATH"
83 INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
84 if( len(INSTALL_BLOCK_DEVICES) == 0 ):
85 raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
87 ROOT_SIZE= vars["ROOT_SIZE"]
88 if ROOT_SIZE == "" or ROOT_SIZE == 0:
89 raise ValueError, "ROOT_SIZE invalid"
91 SWAP_SIZE= vars["SWAP_SIZE"]
92 if SWAP_SIZE == "" or SWAP_SIZE == 0:
93 raise ValueError, "SWAP_SIZE invalid"
95 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
96 if BOOT_CD_VERSION == "":
97 raise ValueError, "BOOT_CD_VERSION"
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
104 bs_request= BootServerRequest.BootServerRequest()
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 )
113 # define the basic partition paths
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
125 # disable swap if its on
126 utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
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 )
137 log.write( "Running vgscan for devices\n" )
138 utils.sysexec_noerr( "vgscan", log )
142 for device in INSTALL_BLOCK_DEVICES:
144 if single_partition_device( device, vars, log ):
145 used_devices.append( device )
146 log.write( "Successfully initialized %s\n" % device )
148 log.write( "Unable to partition %s, not using it.\n" % device )
151 # list of devices to be used with vgcreate
154 # initialize the physical volumes
155 for device in used_devices:
157 part_path= get_partition_path_from_device( device, vars, log )
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
163 vg_device_list = vg_device_list + " " + part_path
165 # create an lvm volume group
166 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
168 # create swap logical volume
169 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
171 # create root logical volume
172 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
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 )
178 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
180 # activate volume group (should already be active)
181 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
184 utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
186 # make root file system
187 utils.sysexec( "mkfs.ext2 -j %s" % PARTITIONS["root"], log )
189 # make vservers file system
190 utils.sysexec( "mkfs.ext2 -m 0 -j %s" % PARTITIONS["vservers"], log )
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" )
197 # list of block devices used may be updated
198 vars["INSTALL_BLOCK_DEVICES"]= used_devices
204 def single_partition_device( device, vars, log ):
206 initialize a disk by removing the old partition tables,
207 and creating a new single partition that fills the disk.
209 return 1 if sucessful, 0 otherwise
212 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
213 if BOOT_CD_VERSION[0] == 2:
214 compatibility.setup_partdisks_2x_cd( vars, log )
218 lvm_flag= parted.partition_flag_get_by_name('lvm')
221 # wipe the old partition table
222 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
225 dev= parted.PedDevice.get(device)
227 # 2.x cds have different libparted that 3.x cds, and they have
228 # different interfaces
229 if BOOT_CD_VERSION[0] == 3:
231 # create a new partition table
232 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
234 # create one big partition on each block device
235 constraint= dev.constraint_any()
237 new_part= disk.partition_new(
238 parted.PARTITION_PRIMARY,
239 parted.file_system_type_get("ext2"),
242 # make it an lvm partition
243 new_part.set_flag(lvm_flag,1)
245 # actually add the partition to the disk
246 disk.add_partition(new_part, constraint)
248 disk.maximize_partition(new_part,constraint)
253 # create a new partition table
254 dev.disk_create(parted.disk_type_get("msdos"))
257 disk= parted.PedDisk.open(dev)
259 # create one big partition on each block device
260 part= disk.next_partition()
262 if part.type == parted.PARTITION_FREESPACE:
263 new_part= disk.partition_new(
264 parted.PARTITION_PRIMARY,
265 parted.file_system_type_get("ext2"),
269 constraint = disk.constraint_any()
271 # make it an lvm partition
272 new_part.set_flag(lvm_flag,1)
274 # actually add the partition to the disk
275 disk.add_partition(new_part, constraint)
279 part= disk.next_partition(part)
285 except BootManagerException, e:
286 log.write( "BootManagerException while running: %s\n" % str(e) )
289 except parted.error, e:
290 log.write( "parted exception while running: %s\n" % str(e) )
297 def create_lvm_physical_volume( part_path, vars, log ):
299 make the specificed partition a lvm physical volume.
301 return 1 if successful, 0 otherwise.
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 utils.sysexec( "pvcreate -fy %s" % part_path, log )
308 except BootManagerException, e:
309 log.write( "create_lvm_physical_volume failed.\n" )
316 def get_partition_path_from_device( device, vars, log ):
318 given a device, return the path of the first partition on the device
321 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
323 # those who wrote the cciss driver just had to make it difficult
324 if BOOT_CD_VERSION[0] == 3:
325 cciss_test= "/dev/cciss"
326 if device[:len(cciss_test)] == cciss_test:
327 part_path= device + "p1"
329 part_path= device + "1"
331 # since device ends in /disc, we need to make it end in
332 # /part1 to indicate the first partition (for devfs based 2.x cds)
333 dev_parts= string.split(device,"/")
334 dev_parts[len(dev_parts)-1]= "part1"
335 part_path= string.join(dev_parts,"/")
341 def get_remaining_extents_on_vg( vars, log ):
343 return the free amount of extents on the planetlab volume group
346 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
347 result= string.strip(c_stdout.readline())
350 remaining_extents= string.split(result,":")[15]
352 return remaining_extents