3 # Copyright (c) 2003 Intel Corporation
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
8 # expected /proc/partitions format
15 from Exceptions import *
17 import BootServerRequest
24 Setup the block devices for install, partition them w/ LVM
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
34 log.write( "\n\nStep: Install: partitioning disks.\n" )
36 # make sure we have the variables we need
38 TEMP_PATH= vars["TEMP_PATH"]
40 raise ValueError, "TEMP_PATH"
42 INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
43 if( len(INSTALL_BLOCK_DEVICES) == 0 ):
44 raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
46 ROOT_SIZE= vars["ROOT_SIZE"]
47 if ROOT_SIZE == "" or ROOT_SIZE == 0:
48 raise ValueError, "ROOT_SIZE invalid"
50 SWAP_SIZE= vars["SWAP_SIZE"]
51 if SWAP_SIZE == "" or SWAP_SIZE == 0:
52 raise ValueError, "SWAP_SIZE invalid"
54 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
55 if BOOT_CD_VERSION == "":
56 raise ValueError, "BOOT_CD_VERSION"
58 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
60 PARTITIONS= vars["PARTITIONS"]
61 if PARTITIONS == None:
62 raise ValueError, "PARTITIONS"
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
69 bs_request= BootServerRequest.BootServerRequest()
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 )
76 # disable swap if its on
77 utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
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 )
88 log.write( "Running vgscan for devices\n" )
89 utils.sysexec_noerr( "vgscan", log )
93 for device in INSTALL_BLOCK_DEVICES:
95 if single_partition_device( device, vars, log ):
96 used_devices.append( device )
97 log.write( "Successfully initialized %s\n" % device )
99 log.write( "Unable to partition %s, not using it.\n" % device )
102 # list of devices to be used with vgcreate
105 # initialize the physical volumes
106 for device in used_devices:
108 part_path= get_partition_path_from_device( device, vars, log )
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
114 vg_device_list = vg_device_list + " " + part_path
116 # create an lvm volume group
117 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
119 # create swap logical volume
120 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
122 # create root logical volume
123 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
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 )
129 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
131 # activate volume group (should already be active)
132 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
135 utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
137 # check if badhd option has been set
140 if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
142 txt = " with bad block search enabled, which may take a while"
144 # filesystems partitions names and their corresponding
145 # reserved-blocks-percentages
146 filesystems = {"root":5,"vservers":0}
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 )
156 # save the list of block devices in the log
157 log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
159 # list of block devices used may be updated
160 vars["INSTALL_BLOCK_DEVICES"]= used_devices
166 def single_partition_device( device, vars, log ):
168 initialize a disk by removing the old partition tables,
169 and creating a new single partition that fills the disk.
171 return 1 if sucessful, 0 otherwise
174 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
175 if BOOT_CD_VERSION[0] == 2:
176 compatibility.setup_partdisks_2x_cd( vars, log )
180 lvm_flag= parted.partition_flag_get_by_name('lvm')
183 # wipe the old partition table
184 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
187 dev= parted.PedDevice.get(device)
189 # 2.x cds have different libparted that 3.x cds, and they have
190 # different interfaces
191 if BOOT_CD_VERSION[0] >= 3:
193 # create a new partition table
194 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
196 # create one big partition on each block device
197 constraint= dev.constraint_any()
199 new_part= disk.partition_new(
200 parted.PARTITION_PRIMARY,
201 parted.file_system_type_get("ext2"),
204 # make it an lvm partition
205 new_part.set_flag(lvm_flag,1)
207 # actually add the partition to the disk
208 disk.add_partition(new_part, constraint)
210 disk.maximize_partition(new_part,constraint)
215 # create a new partition table
216 dev.disk_create(parted.disk_type_get("msdos"))
219 disk= parted.PedDisk.open(dev)
221 # create one big partition on each block device
222 part= disk.next_partition()
224 if part.type == parted.PARTITION_FREESPACE:
225 new_part= disk.partition_new(
226 parted.PARTITION_PRIMARY,
227 parted.file_system_type_get("ext2"),
231 constraint = disk.constraint_any()
233 # make it an lvm partition
234 new_part.set_flag(lvm_flag,1)
236 # actually add the partition to the disk
237 disk.add_partition(new_part, constraint)
241 part= disk.next_partition(part)
247 except BootManagerException, e:
248 log.write( "BootManagerException while running: %s\n" % str(e) )
251 except parted.error, e:
252 log.write( "parted exception while running: %s\n" % str(e) )
259 def create_lvm_physical_volume( part_path, vars, log ):
261 make the specificed partition a lvm physical volume.
263 return 1 if successful, 0 otherwise.
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
272 utils.sysexec( "pvcreate -ffy %s" % part_path, log )
273 except BootManagerException, e:
274 log.write( "create_lvm_physical_volume failed.\n" )
281 def get_partition_path_from_device( device, vars, log ):
283 given a device, return the path of the first partition on the device
286 BOOT_CD_VERSION= vars["BOOT_CD_VERSION"]
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"
294 part_path= device + "1"
296 # if 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 if dev_parts[-1] == "disc":
300 dev_parts[-1]= "part1"
301 part_path= string.join(dev_parts,"/")
303 part_path= device + "1"
309 def get_remaining_extents_on_vg( vars, log ):
311 return the free amount of extents on the planetlab volume group
314 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
315 result= string.strip(c_stdout.readline())
318 remaining_extents= string.split(result,":")[15]
320 return remaining_extents