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 "VSERVER_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 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
107 # initialize the physical volumes
108 for device in used_devices:
110 part_path= get_partition_path_from_device( device, vars, log )
112 if not create_lvm_physical_volume( part_path, vars, log ):
113 raise BootManagerException, "Could not create lvm physical volume " \
114 "on partition %s" % part_path
116 vg_device_list = vg_device_list + " " + part_path
118 # create an lvm volume group
119 utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
121 # create swap logical volume
122 utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
124 # create root logical volume
125 utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
127 if vars['NODE_MODEL_OPTIONS'] & ModelOptions.RAWDISK and VSERVERS_SIZE != "-1":
128 utils.sysexec( "lvcreate -L%s -nvservers planetlab" % VSERVERS_SIZE, log )
129 remaining_extents= get_remaining_extents_on_vg( vars, log )
130 utils.sysexec( "lvcreate -l%s -nrawdisk planetlab" % remaining_extents, log )
132 # create vservers logical volume with all remaining space
133 # first, we need to get the number of remaining extents we can use
134 remaining_extents= get_remaining_extents_on_vg( vars, log )
136 utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
138 # activate volume group (should already be active)
139 #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
142 utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
144 # check if badhd option has been set
147 if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
149 txt = " with bad block search enabled, which may take a while"
151 # filesystems partitions names and their corresponding
152 # reserved-blocks-percentages
153 filesystems = {"root":5,"vservers":0}
155 # make the file systems
156 for fs in filesystems.keys():
157 # get the reserved blocks percentage
158 rbp = filesystems[fs]
159 devname = PARTITIONS[fs]
160 log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
161 utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
163 # save the list of block devices in the log
164 log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
166 # list of block devices used may be updated
167 vars["INSTALL_BLOCK_DEVICES"]= used_devices
173 def single_partition_device( device, vars, log ):
175 initialize a disk by removing the old partition tables,
176 and creating a new single partition that fills the disk.
178 return 1 if sucessful, 0 otherwise
183 lvm_flag= parted.partition_flag_get_by_name('lvm')
186 # wipe the old partition table
187 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
190 dev= parted.PedDevice.get(device)
192 # create a new partition table
193 disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
195 # create one big partition on each block device
196 constraint= dev.constraint_any()
198 new_part= disk.partition_new(
199 parted.PARTITION_PRIMARY,
200 parted.file_system_type_get("ext2"),
203 # make it an lvm partition
204 new_part.set_flag(lvm_flag,1)
206 # actually add the partition to the disk
207 disk.add_partition(new_part, constraint)
209 disk.maximize_partition(new_part,constraint)
214 except BootManagerException, e:
215 log.write( "BootManagerException while running: %s\n" % str(e) )
218 except parted.error, e:
219 log.write( "parted exception while running: %s\n" % str(e) )
226 def create_lvm_physical_volume( part_path, vars, log ):
228 make the specificed partition a lvm physical volume.
230 return 1 if successful, 0 otherwise.
234 # again, wipe any old data, this time on the partition
235 utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
236 ### patch Thierry Parmentelat, required on some hardware
239 utils.sysexec( "pvcreate -ffy %s" % part_path, log )
240 except BootManagerException, e:
241 log.write( "create_lvm_physical_volume failed.\n" )
248 def get_partition_path_from_device( device, vars, log ):
250 given a device, return the path of the first partition on the device
253 # those who wrote the cciss driver just had to make it difficult
254 cciss_test= "/dev/cciss"
255 if device[:len(cciss_test)] == cciss_test:
256 part_path= device + "p1"
258 part_path= device + "1"
264 def get_remaining_extents_on_vg( vars, log ):
266 return the free amount of extents on the planetlab volume group
269 c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
270 result= string.strip(c_stdout.readline())
273 remaining_extents= string.split(result,":")[15]
275 return remaining_extents