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
23 Setup the block devices for install, partition them w/ LVM
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
32 log.write( "\n\nStep: Install: partitioning disks.\n" )
34 # make sure we have the variables we need
36 TEMP_PATH= vars["TEMP_PATH"]
38 raise ValueError, "TEMP_PATH"
40 INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
41 if( len(INSTALL_BLOCK_DEVICES) == 0 ):
42 raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
44 ROOT_SIZE= vars["ROOT_SIZE"]
45 if ROOT_SIZE == "" or ROOT_SIZE == 0:
46 raise ValueError, "ROOT_SIZE invalid"
48 SWAP_SIZE= vars["SWAP_SIZE"]
49 if SWAP_SIZE == "" or SWAP_SIZE == 0:
50 raise ValueError, "SWAP_SIZE invalid"
52 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
54 PARTITIONS= vars["PARTITIONS"]
55 if PARTITIONS == None:
56 raise ValueError, "PARTITIONS"
58 if NODE_MODEL_OPTIONS & ModelOptions.RAWDISK:
60 if "VSERVERS_SIZE" in vars:
61 VSERVERS_SIZE= vars["VSERVERS_SIZE"]
62 if VSERVERS_SIZE == "" or VSERVERS_SIZE == 0:
63 raise ValueError, "VSERVERS_SIZE"
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
70 bs_request= BootServerRequest.BootServerRequest(vars)
73 # disable swap if its on
74 utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
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 -f planetlab", log )
85 log.write( "Running vgscan for devices\n" )
86 utils.sysexec_noerr( "vgscan", log )
90 INSTALL_BLOCK_DEVICES.sort()
91 for device in INSTALL_BLOCK_DEVICES:
93 if single_partition_device( device, vars, log ):
94 if (len(used_devices) > 0 and
95 (vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK)):
96 log.write( "Running in raw disk mode, not using %s.\n" % device )
98 used_devices.append( device )
99 log.write( "Successfully initialized %s\n" % device )
101 log.write( "Unable to partition %s, not using it.\n" % device )
104 # list of devices to be used with vgcreate
109 for device in used_devices:
110 part_path= get_partition_path_from_device( device, vars, log )
111 partitions.append(part_path)
113 # create raid partition
114 raid_partition = create_raid_partition(partitions, vars, log)
115 if raid_partition != None:
116 partitions = [raid_partition]
117 log.write("PARTITIONS %s\n" % str(partitions))
118 # initialize the physical volumes
119 for part_path in partitions:
120 if not create_lvm_physical_volume( part_path, vars, log ):
121 raise BootManagerException, "Could not create lvm physical volume " \
122 "on partition %s" % part_path
123 vg_device_list = vg_device_list + " " + part_path
125 # create an lvm volume group
126 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
128 # create swap logical volume
129 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
131 # create root logical volume
132 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
134 if vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK and VSERVERS_SIZE != "-1":
135 utils.sysexec( "lvcreate -L%s -nvservers planetlab" % VSERVERS_SIZE, log )
136 remaining_extents= get_remaining_extents_on_vg( vars, log )
137 utils.sysexec( "lvcreate -l%s -nrawdisk planetlab" % remaining_extents, log )
139 # create vservers logical volume with all remaining space
140 # first, we need to get the number of remaining extents we can use
141 remaining_extents= get_remaining_extents_on_vg( vars, log )
143 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
145 # activate volume group (should already be active)
146 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
149 utils.sysexec( "mkswap -f %s" % PARTITIONS["swap"], log )
151 # check if badhd option has been set
154 if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
156 txt = " with bad block search enabled, which may take a while"
158 # filesystems partitions names and their corresponding
159 # reserved-blocks-percentages
160 filesystems = {"root":5,"vservers":0}
162 # ROOT filesystem with ext2
164 rbp = filesystems[fs]
165 devname = PARTITIONS[fs]
166 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
167 utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
169 # VSERVER filesystem with btrfs to support snapshoting and stuff
171 rbp = filesystems[fs]
172 devname = PARTITIONS[fs]
173 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
174 utils.sysexec( "mkfs.btrfs %s" % (devname), log )
176 # disable time/count based filesystems checks
177 for filesystem in ["root"]:
178 utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % PARTITIONS[filesystem], log)
180 # save the list of block devices in the log
181 log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
183 # list of block devices used may be updated
184 vars["INSTALL_BLOCK_DEVICES"]= used_devices
190 def single_partition_device( device, vars, log ):
192 initialize a disk by removing the old partition tables,
193 and creating a new single partition that fills the disk.
195 return 1 if sucessful, 0 otherwise
198 # two forms, depending on which version of pyparted we have
199 # v1 does not have a 'version' method
200 # v2 and above does, but to make it worse,
201 # parted-3.4 on f14 has parted.version broken and raises SystemError
204 return single_partition_device_2_x (device, vars, log)
205 except AttributeError:
206 # old parted does not have version at all
207 return single_partition_device_1_x (device, vars, log)
209 # let's assume this is >=2
210 return single_partition_device_2_x (device, vars, log)
214 def single_partition_device_1_x ( device, vars, log):
216 lvm_flag= parted.partition_flag_get_by_name('lvm')
219 log.write("Using pyparted 1.x\n")
220 # wipe the old partition table
221 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
224 dev= parted.PedDevice.get(device)
226 # create a new partition table
227 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
229 # create one big partition on each block device
230 constraint= dev.constraint_any()
232 new_part= disk.partition_new(
233 parted.PARTITION_PRIMARY,
234 parted.file_system_type_get("ext2"),
237 # make it an lvm partition
238 new_part.set_flag(lvm_flag,1)
240 # actually add the partition to the disk
241 disk.add_partition(new_part, constraint)
243 disk.maximize_partition(new_part,constraint)
248 except BootManagerException, e:
249 log.write( "BootManagerException while running: %s\n" % str(e) )
252 except parted.error, e:
253 log.write( "parted exception while running: %s\n" % str(e) )
260 def single_partition_device_2_x ( device, vars, log):
262 log.write("Using pyparted 2.x\n")
263 # wipe the old partition table
264 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
266 dev= parted.Device(device)
267 # create a new partition table
269 disk= parted.freshDisk(dev,'msdos')
270 # use gpt as a fallback for disks larger than 2TB
272 disk= parted.freshDisk(dev,'gpt')
273 # create one big partition on each block device
274 constraint= parted.constraint.Constraint (device=dev)
275 geometry = parted.geometry.Geometry (device=dev, start=0, end=1)
276 fs = parted.filesystem.FileSystem (type="ext2",geometry=geometry)
277 new_part= parted.partition.Partition (disk, type=parted.PARTITION_NORMAL,
278 fs=fs, geometry=geometry)
279 # make it an lvm partition
280 new_part.setFlag(parted.PARTITION_LVM)
281 # actually add the partition to the disk
282 disk.addPartition(new_part, constraint)
283 disk.maximizePartition(new_part,constraint)
285 print >>log, 'Current disk for %s'%device,disk
286 print >>log, 'Current dev for %s'%device,dev
289 log.write( "Exception inside single_partition_device_2_x : %s\n" % str(e) )
291 traceback.print_exc(file=log)
298 def create_lvm_physical_volume( part_path, vars, log ):
300 make the specificed partition a lvm physical volume.
302 return 1 if successful, 0 otherwise.
306 # again, wipe any old data, this time on the partition
307 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
308 ### patch Thierry Parmentelat, required on some hardware
311 utils.sysexec( "pvcreate -ffy %s" % part_path, log )
312 except BootManagerException, e:
313 log.write( "create_lvm_physical_volume failed.\n" )
319 def create_raid_partition(partitions, vars, log):
321 create raid array using specified partitions.
325 node_tags = BootAPI.call_api_function( vars, "GetNodeTags",
326 ({'node_id': vars['NODE_ID']},))
327 for node_tag in node_tags:
328 if node_tag['tagname'] == 'raid_enabled' and \
329 node_tag['value'] == '1':
336 log.write( "Software raid enabled.\n" )
338 utils.sysexec_noerr("mdadm --stop /dev/md0", log)
340 for part_path in partitions:
341 utils.sysexec_noerr("mdadm --zero-superblock %s " % part_path, log)
343 # assume each partiton is on a separate disk
344 num_parts = len(partitions)
346 log.write( "Not enough disks for raid. Found: %s\n" % partitions )
347 raise BootManagerException("Not enough disks for raid. Found: %s\n" % partitions)
354 part_list = " ".join(partitions)
355 raid_part = "/dev/md0"
356 cmd = "mdadm --create %(raid_part)s --chunk=128 --level=raid%(lvl)s " % locals() + \
357 "--raid-devices=%(num_parts)s %(part_list)s" % locals()
358 utils.sysexec(cmd, log)
360 except BootManagerException, e:
361 log.write("create_raid_partition failed.\n")
367 def get_partition_path_from_device( device, vars, log ):
369 given a device, return the path of the first partition on the device
372 # those who wrote the cciss driver just had to make it difficult
373 cciss_test= "/dev/cciss"
374 if device[:len(cciss_test)] == cciss_test:
375 part_path= device + "p1"
377 part_path= device + "1"
383 def get_remaining_extents_on_vg( vars, log ):
385 return the free amount of extents on the planetlab volume group
388 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
389 result= string.strip(c_stdout.readline())
392 remaining_extents= string.split(result,":")[15]
394 return remaining_extents