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 # use vs_ROOT_SIZE or lxc_ROOT_SIZE as appropriate
45 varname=vars['virt']+"_ROOT_SIZE"
46 ROOT_SIZE= vars[varname]
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 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
56 PARTITIONS= vars["PARTITIONS"]
57 if PARTITIONS == None:
58 raise ValueError, "PARTITIONS"
60 if NODE_MODEL_OPTIONS & ModelOptions.RAWDISK:
62 if "VSERVERS_SIZE" in vars:
63 VSERVERS_SIZE= vars["VSERVERS_SIZE"]
64 if VSERVERS_SIZE == "" or VSERVERS_SIZE == 0:
65 raise ValueError, "VSERVERS_SIZE"
68 raise BootManagerException, "Missing variable in vars: %s\n" % var
69 except ValueError, var:
70 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
72 bs_request= BootServerRequest.BootServerRequest(vars)
75 # disable swap if its on
76 utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
78 # shutdown and remove any lvm groups/volumes
79 utils.sysexec_noerr( "vgscan", log )
80 utils.sysexec_noerr( "vgchange -ay", log )
81 utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["root"], log )
82 utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["swap"], log )
83 utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["vservers"], log )
84 utils.sysexec_noerr( "vgchange -an", log )
85 utils.sysexec_noerr( "vgremove -f planetlab", log )
87 log.write( "Running vgscan for devices\n" )
88 utils.sysexec_noerr( "vgscan", log )
92 INSTALL_BLOCK_DEVICES.sort()
93 for device in INSTALL_BLOCK_DEVICES:
95 if single_partition_device( device, vars, log ):
96 if (len(used_devices) > 0 and
97 (vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK)):
98 log.write( "Running in raw disk mode, not using %s.\n" % device )
100 used_devices.append( device )
101 log.write( "Successfully initialized %s\n" % device )
103 log.write( "Unable to partition %s, not using it.\n" % device )
106 # list of devices to be used with vgcreate
111 for device in used_devices:
112 part_path= get_partition_path_from_device( device, vars, log )
113 partitions.append(part_path)
115 # create raid partition
116 raid_partition = create_raid_partition(partitions, vars, log)
117 if raid_partition != None:
118 partitions = [raid_partition]
119 log.write("PARTITIONS %s\n" % str(partitions))
120 # initialize the physical volumes
121 for part_path in partitions:
122 if not create_lvm_physical_volume( part_path, vars, log ):
123 raise BootManagerException, "Could not create lvm physical volume " \
124 "on partition %s" % part_path
125 vg_device_list = vg_device_list + " " + part_path
127 # create an lvm volume group
128 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
130 # create swap logical volume
131 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
133 # check if we want a separate partition for VMs
134 one_partition = vars['ONE_PARTITION']=='1'
136 remaining_extents= get_remaining_extents_on_vg( vars, log )
137 utils.sysexec( "lvcreate -l%s -nroot planetlab" % remaining_extents, log )
139 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
140 if vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK and VSERVERS_SIZE != "-1":
141 utils.sysexec( "lvcreate -L%s -nvservers planetlab" % VSERVERS_SIZE, log )
142 remaining_extents= get_remaining_extents_on_vg( vars, log )
143 utils.sysexec( "lvcreate -l%s -nrawdisk planetlab" % remaining_extents, log )
145 # create vservers logical volume with all remaining space
146 # first, we need to get the number of remaining extents we can use
147 remaining_extents= get_remaining_extents_on_vg( vars, log )
148 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
150 # activate volume group (should already be active)
151 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
154 utils.sysexec( "mkswap -f %s" % PARTITIONS["swap"], log )
156 # check if badhd option has been set
159 if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
161 txt = " with bad block search enabled, which may take a while"
163 # filesystems partitions names and their corresponding
164 # reserved-blocks-percentages
165 filesystems = {"root":5,"vservers":0}
167 # ROOT filesystem is always with ext2
169 rbp = filesystems[fs]
170 devname = PARTITIONS[fs]
171 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
172 utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
173 # disable time/count based filesystems checks
174 utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % devname, log)
176 # VSERVER filesystem with btrfs to support snapshoting and stuff
178 rbp = filesystems[fs]
179 devname = PARTITIONS[fs]
180 if vars['virt']=='vs':
181 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
182 utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
183 # disable time/count based filesystems checks
184 utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % devname, log)
185 elif (not one_partition):
186 log.write("formatting %s btrfs partition (%s).\n" % (fs,devname))
187 # early BootCD's seem to come with a version of mkfs.btrfs that does not support -f
188 # let's check for that before invoking it
190 if os.system("mkfs.btrfs --help 2>&1 | grep force")==0:
193 utils.sysexec( mkfs, log )
194 # as of 2013/02 it looks like there's not yet an option to set fsck frequency with btrfs
196 # save the list of block devices in the log
197 log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
199 # list of block devices used may be updated
200 vars["INSTALL_BLOCK_DEVICES"]= used_devices
206 def single_partition_device( device, vars, log ):
208 initialize a disk by removing the old partition tables,
209 and creating a new single partition that fills the disk.
211 return 1 if sucessful, 0 otherwise
214 # two forms, depending on which version of pyparted we have
215 # v1 does not have a 'version' method
216 # v2 and above does, but to make it worse,
217 # parted-3.4 on f14 has parted.version broken and raises SystemError
220 return single_partition_device_2_x (device, vars, log)
221 except AttributeError:
222 # old parted does not have version at all
223 return single_partition_device_1_x (device, vars, log)
225 # let's assume this is >=2
226 return single_partition_device_2_x (device, vars, log)
230 def single_partition_device_1_x ( device, vars, log):
232 lvm_flag= parted.partition_flag_get_by_name('lvm')
235 log.write("Using pyparted 1.x\n")
236 # wipe the old partition table
237 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
240 dev= parted.PedDevice.get(device)
242 # create a new partition table
243 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
245 # create one big partition on each block device
246 constraint= dev.constraint_any()
248 new_part= disk.partition_new(
249 parted.PARTITION_PRIMARY,
250 parted.file_system_type_get("ext2"),
253 # make it an lvm partition
254 new_part.set_flag(lvm_flag,1)
256 # actually add the partition to the disk
257 disk.add_partition(new_part, constraint)
259 disk.maximize_partition(new_part,constraint)
264 except BootManagerException, e:
265 log.write( "BootManagerException while running: %s\n" % str(e) )
268 except parted.error, e:
269 log.write( "parted exception while running: %s\n" % str(e) )
276 def single_partition_device_2_x ( device, vars, log):
278 log.write("Using pyparted 2.x\n")
280 # Thierry june 2012 -- for disks larger than 2TB
281 # calling this with part_type='msdos' would fail at the maximizePartition stage
282 # create a new partition table
283 def partition_table (device, part_type, fs_type):
284 # wipe the old partition table
285 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
287 dev= parted.Device(device)
288 disk = parted.freshDisk(dev,part_type)
289 # create one big partition on each block device
290 constraint= parted.constraint.Constraint (device=dev)
291 geometry = parted.geometry.Geometry (device=dev, start=0, end=1)
292 fs = parted.filesystem.FileSystem (type=fs_type,geometry=geometry)
293 new_part= parted.partition.Partition (disk, type=parted.PARTITION_NORMAL,
294 fs=fs, geometry=geometry)
295 # make it an lvm partition
296 new_part.setFlag(parted.PARTITION_LVM)
297 # actually add the partition to the disk
298 disk.addPartition(new_part, constraint)
299 disk.maximizePartition(new_part,constraint)
301 log.write ("Current disk for %s - partition type %s\n%s\n"%(device,part_type,disk))
302 log.write ("Current dev for %s\n%s\n"%(device,dev))
306 partition_table (device, 'msdos', 'ext2')
308 partition_table (device, 'gpt', 'ext2')
311 log.write( "Exception inside single_partition_device_2_x : %s\n" % str(e) )
313 traceback.print_exc(file=log)
320 def create_lvm_physical_volume( part_path, vars, log ):
322 make the specificed partition a lvm physical volume.
324 return 1 if successful, 0 otherwise.
328 # again, wipe any old data, this time on the partition
329 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
330 ### patch Thierry Parmentelat, required on some hardware
333 utils.sysexec( "pvcreate -ffy %s" % part_path, log )
334 except BootManagerException, e:
335 log.write( "create_lvm_physical_volume failed.\n" )
341 def create_raid_partition(partitions, vars, log):
343 create raid array using specified partitions.
347 node_tags = BootAPI.call_api_function( vars, "GetNodeTags",
348 ({'node_id': vars['NODE_ID']},))
349 for node_tag in node_tags:
350 if node_tag['tagname'] == 'raid_enabled' and \
351 node_tag['value'] == '1':
358 log.write( "Software raid enabled.\n" )
360 utils.sysexec_noerr("mdadm --stop /dev/md0", log)
362 for part_path in partitions:
363 utils.sysexec_noerr("mdadm --zero-superblock %s " % part_path, log)
365 # assume each partiton is on a separate disk
366 num_parts = len(partitions)
368 log.write( "Not enough disks for raid. Found: %s\n" % partitions )
369 raise BootManagerException("Not enough disks for raid. Found: %s\n" % partitions)
376 part_list = " ".join(partitions)
377 raid_part = "/dev/md0"
378 cmd = "mdadm --create %(raid_part)s --chunk=128 --level=raid%(lvl)s " % locals() + \
379 "--raid-devices=%(num_parts)s %(part_list)s" % locals()
380 utils.sysexec(cmd, log)
382 except BootManagerException, e:
383 log.write("create_raid_partition failed.\n")
389 def get_partition_path_from_device( device, vars, log ):
391 given a device, return the path of the first partition on the device
394 # those who wrote the cciss driver just had to make it difficult
395 cciss_test= "/dev/cciss"
396 if device[:len(cciss_test)] == cciss_test:
397 part_path= device + "p1"
399 part_path= device + "1"
405 def get_remaining_extents_on_vg( vars, log ):
407 return the free amount of extents on the planetlab volume group
410 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
411 result= string.strip(c_stdout.readline())
414 remaining_extents= string.split(result,":")[15]
416 return remaining_extents