6 # Copyright (c) 2003 Intel Corporation
9 # Copyright (c) 2004-2006 The Trustees of Princeton University
10 # All rights reserved.
11 # expected /proc/partitions format
18 from Exceptions import *
20 import BootServerRequest
26 Setup the block devices for install, partition them w/ LVM
28 Expect the following variables from the store:
29 INSTALL_BLOCK_DEVICES list of block devices to install onto
30 TEMP_PATH somewhere to store what we need to run
31 ROOT_SIZE the size of the root logical volume
32 SWAP_SIZE the size of the swap partition
35 log.write( "\n\nStep: Install: partitioning disks.\n" )
37 # make sure we have the variables we need
39 TEMP_PATH= vars["TEMP_PATH"]
41 raise ValueError, "TEMP_PATH"
43 INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
44 if( len(INSTALL_BLOCK_DEVICES) == 0 ):
45 raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
47 ROOT_SIZE= vars["ROOT_SIZE"]
48 if ROOT_SIZE == "" or ROOT_SIZE == 0:
49 raise ValueError, "ROOT_SIZE invalid"
51 SWAP_SIZE= vars["SWAP_SIZE"]
52 if SWAP_SIZE == "" or SWAP_SIZE == 0:
53 raise ValueError, "SWAP_SIZE invalid"
55 NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
57 PARTITIONS= vars["PARTITIONS"]
58 if PARTITIONS == None:
59 raise ValueError, "PARTITIONS"
61 if NODE_MODEL_OPTIONS & ModelOptions.RAWDISK:
63 if "VSERVERS_SIZE" in vars:
64 VSERVERS_SIZE= vars["VSERVERS_SIZE"]
65 if VSERVERS_SIZE == "" or VSERVERS_SIZE == 0:
66 raise ValueError, "VSERVERS_SIZE"
69 raise BootManagerException, "Missing variable in vars: %s\n" % var
70 except ValueError, var:
71 raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
73 bs_request= BootServerRequest.BootServerRequest(vars)
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 INSTALL_BLOCK_DEVICES.sort()
94 for device in INSTALL_BLOCK_DEVICES:
96 if single_partition_device( device, vars, log ):
97 if (len(used_devices) > 0 and
98 (vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK)):
99 log.write( "Running in raw disk mode, not using %s.\n" % device )
101 used_devices.append( device )
102 log.write( "Successfully initialized %s\n" % device )
104 log.write( "Unable to partition %s, not using it.\n" % device )
107 # list of devices to be used with vgcreate
110 # initialize the physical volumes
111 for device in used_devices:
113 part_path= get_partition_path_from_device( device, vars, log )
115 if not create_lvm_physical_volume( part_path, vars, log ):
116 raise BootManagerException, "Could not create lvm physical volume " \
117 "on partition %s" % part_path
119 vg_device_list = vg_device_list + " " + part_path
121 # create an lvm volume group
122 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
124 # create swap logical volume
125 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
127 # create root logical volume
128 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
130 if vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK and VSERVERS_SIZE != "-1":
131 utils.sysexec( "lvcreate -L%s -nvservers planetlab" % VSERVERS_SIZE, log )
132 remaining_extents= get_remaining_extents_on_vg( vars, log )
133 utils.sysexec( "lvcreate -l%s -nrawdisk planetlab" % remaining_extents, log )
135 # create vservers logical volume with all remaining space
136 # first, we need to get the number of remaining extents we can use
137 remaining_extents= get_remaining_extents_on_vg( vars, log )
139 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
141 # activate volume group (should already be active)
142 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
145 utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
147 # check if badhd option has been set
150 if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
152 txt = " with bad block search enabled, which may take a while"
154 # filesystems partitions names and their corresponding
155 # reserved-blocks-percentages
156 filesystems = {"root":5,"vservers":0}
158 # make the file systems
159 for fs in filesystems.keys():
160 # get the reserved blocks percentage
161 rbp = filesystems[fs]
162 devname = PARTITIONS[fs]
163 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
164 utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
166 # disable time/count based filesystems checks
167 for filesystem in ("root","vservers"):
168 utils.sysexec_noerr( "tune2fs -c -1 -i 0 %s" % PARTITIONS[filesystem], log)
170 # save the list of block devices in the log
171 log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
173 # list of block devices used may be updated
174 vars["INSTALL_BLOCK_DEVICES"]= used_devices
180 def single_partition_device( device, vars, log ):
182 initialize a disk by removing the old partition tables,
183 and creating a new single partition that fills the disk.
185 return 1 if sucessful, 0 otherwise
188 # two forms, depending on which version of pyparted we have
190 version=parted.version()
191 return single_partition_device_2_x (device, vars, log)
193 return single_partition_device_1_x (device, vars, log)
197 def single_partition_device_1_x ( device, vars, log):
199 lvm_flag= parted.partition_flag_get_by_name('lvm')
202 print >>log, "Using pyparted 1.x"
203 # wipe the old partition table
204 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
207 dev= parted.PedDevice.get(device)
209 # create a new partition table
210 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
212 # create one big partition on each block device
213 constraint= dev.constraint_any()
215 new_part= disk.partition_new(
216 parted.PARTITION_PRIMARY,
217 parted.file_system_type_get("ext2"),
220 # make it an lvm partition
221 new_part.set_flag(lvm_flag,1)
223 # actually add the partition to the disk
224 disk.add_partition(new_part, constraint)
226 disk.maximize_partition(new_part,constraint)
231 except BootManagerException, e:
232 log.write( "BootManagerException while running: %s\n" % str(e) )
235 except parted.error, e:
236 log.write( "parted exception while running: %s\n" % str(e) )
243 def single_partition_device_2_x ( device, vars, log):
245 print >>log, "Using pyparted 2.x"
246 # wipe the old partition table
247 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
249 dev= parted.Device(device)
250 # create a new partition table
251 disk= parted.freshDisk(dev,'msdos')
252 # create one big partition on each block device
253 constraint= parted.constraint.Constraint (device=dev)
254 geometry = parted.geometry.Geometry (device=dev, start=0, end=1)
255 fs = parted.filesystem.FileSystem (type="ext2",geometry=geometry)
256 new_part= parted.partition.Partition (disk, type=parted.PARTITION_NORMAL,
257 fs=fs, geometry=geometry)
258 # make it an lvm partition
259 new_part.setFlag(parted.PARTITION_LVM)
260 # actually add the partition to the disk
261 disk.addPartition(new_part, constraint)
262 disk.maximizePartition(new_part,constraint)
264 print >>log, 'Current disk for %s'%device,disk
265 print >>log, 'Current dev for %s'%device,dev
268 log.write( "Exception inside single_partition_device_2_x : %s\n" % str(e) )
270 traceback.print_exc(file=log)
277 def create_lvm_physical_volume( part_path, vars, log ):
279 make the specificed partition a lvm physical volume.
281 return 1 if successful, 0 otherwise.
285 # again, wipe any old data, this time on the partition
286 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
287 ### patch Thierry Parmentelat, required on some hardware
290 utils.sysexec( "pvcreate -ffy %s" % part_path, log )
291 except BootManagerException, e:
292 log.write( "create_lvm_physical_volume failed.\n" )
299 def get_partition_path_from_device( device, vars, log ):
301 given a device, return the path of the first partition on the device
304 # those who wrote the cciss driver just had to make it difficult
305 cciss_test= "/dev/cciss"
306 if device[:len(cciss_test)] == cciss_test:
307 part_path= device + "p1"
309 part_path= device + "1"
315 def get_remaining_extents_on_vg( vars, log ):
317 return the free amount of extents on the planetlab volume group
320 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
321 result= string.strip(c_stdout.readline())
324 remaining_extents= string.split(result,":")[15]
326 return remaining_extents