* improve availability - reliability : start a fallback sshd very early in the bm...
[bootmanager.git] / source / steps / InstallPartitionDisks.py
1 #!/usr/bin/python
2
3 # Copyright (c) 2003 Intel Corporation
4 # All rights reserved.
5 #
6 # Copyright (c) 2004-2006 The Trustees of Princeton University
7 # All rights reserved.
8 # expected /proc/partitions format
9
10 import os, sys
11 import string
12 import popen2
13
14
15 from Exceptions import *
16 import utils
17 import BootServerRequest
18
19 import ModelOptions
20
21 def Run( vars, log ):
22     """
23     Setup the block devices for install, partition them w/ LVM
24     
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
30     """
31
32     log.write( "\n\nStep: Install: partitioning disks.\n" )
33         
34     # make sure we have the variables we need
35     try:
36         TEMP_PATH= vars["TEMP_PATH"]
37         if TEMP_PATH == "":
38             raise ValueError, "TEMP_PATH"
39
40         INSTALL_BLOCK_DEVICES= vars["INSTALL_BLOCK_DEVICES"]
41         if( len(INSTALL_BLOCK_DEVICES) == 0 ):
42             raise ValueError, "INSTALL_BLOCK_DEVICES is empty"
43
44         ROOT_SIZE= vars["ROOT_SIZE"]
45         if ROOT_SIZE == "" or ROOT_SIZE == 0:
46             raise ValueError, "ROOT_SIZE invalid"
47
48         SWAP_SIZE= vars["SWAP_SIZE"]
49         if SWAP_SIZE == "" or SWAP_SIZE == 0:
50             raise ValueError, "SWAP_SIZE invalid"
51
52         NODE_MODEL_OPTIONS= vars["NODE_MODEL_OPTIONS"]
53
54         PARTITIONS= vars["PARTITIONS"]
55         if PARTITIONS == None:
56             raise ValueError, "PARTITIONS"
57
58     except KeyError, var:
59         raise BootManagerException, "Missing variable in vars: %s\n" % var
60     except ValueError, var:
61         raise BootManagerException, "Variable in vars, shouldn't be: %s\n" % var
62
63     bs_request= BootServerRequest.BootServerRequest()
64
65     
66     # disable swap if its on
67     utils.sysexec_noerr( "swapoff %s" % PARTITIONS["swap"], log )
68
69     # shutdown and remove any lvm groups/volumes
70     utils.sysexec_noerr( "vgscan", log )
71     utils.sysexec_noerr( "vgchange -ay", log )        
72     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["root"], log )
73     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["swap"], log )
74     utils.sysexec_noerr( "lvremove -f %s" % PARTITIONS["vservers"], log )
75     utils.sysexec_noerr( "vgchange -an", log )
76     utils.sysexec_noerr( "vgremove planetlab", log )
77
78     log.write( "Running vgscan for devices\n" )
79     utils.sysexec_noerr( "vgscan", log )
80     
81     used_devices= []
82
83     for device in INSTALL_BLOCK_DEVICES:
84
85         if single_partition_device( device, vars, log ):
86             used_devices.append( device )
87             log.write( "Successfully initialized %s\n" % device )
88         else:
89             log.write( "Unable to partition %s, not using it.\n" % device )
90             continue
91
92     # list of devices to be used with vgcreate
93     vg_device_list= ""
94
95     # initialize the physical volumes
96     for device in used_devices:
97
98         part_path= get_partition_path_from_device( device, vars, log )
99         
100         if not create_lvm_physical_volume( part_path, vars, log ):
101             raise BootManagerException, "Could not create lvm physical volume " \
102                   "on partition %s" % part_path
103         
104         vg_device_list = vg_device_list + " " + part_path
105
106     # create an lvm volume group
107     utils.sysexec( "vgcreate -s32M planetlab %s" % vg_device_list, log)
108
109     # create swap logical volume
110     utils.sysexec( "lvcreate -L%s -nswap planetlab" % SWAP_SIZE, log )
111
112     # create root logical volume
113     utils.sysexec( "lvcreate -L%s -nroot planetlab" % ROOT_SIZE, log )
114
115     # create vservers logical volume with all remaining space
116     # first, we need to get the number of remaining extents we can use
117     remaining_extents= get_remaining_extents_on_vg( vars, log )
118     
119     utils.sysexec( "lvcreate -l%s -nvservers planetlab" % remaining_extents, log )
120
121     # activate volume group (should already be active)
122     #utils.sysexec( TEMP_PATH + "vgchange -ay planetlab", log )
123
124     # make swap
125     utils.sysexec( "mkswap %s" % PARTITIONS["swap"], log )
126
127     # check if badhd option has been set
128     option = ''
129     txt = ''
130     if NODE_MODEL_OPTIONS & ModelOptions.BADHD:
131         option = '-c'
132         txt = " with bad block search enabled, which may take a while"
133     
134     # filesystems partitions names and their corresponding
135     # reserved-blocks-percentages
136     filesystems = {"root":5,"vservers":0}
137
138     # make the file systems
139     for fs in filesystems.keys():
140         # get the reserved blocks percentage
141         rbp = filesystems[fs]
142         devname = PARTITIONS[fs]
143         log.write("formatting %s partition (%s)%s.\n" % (fs,devname,txt))
144         utils.sysexec( "mkfs.ext2 -q %s -m %d -j %s" % (option,rbp,devname), log )
145
146     # save the list of block devices in the log
147     log.write( "Block devices used (in lvm): %s\n" % repr(used_devices))
148
149     # list of block devices used may be updated
150     vars["INSTALL_BLOCK_DEVICES"]= used_devices
151
152     return 1
153
154
155
156 def single_partition_device( device, vars, log ):
157     """
158     initialize a disk by removing the old partition tables,
159     and creating a new single partition that fills the disk.
160
161     return 1 if sucessful, 0 otherwise
162     """
163
164     import parted
165     
166     lvm_flag= parted.partition_flag_get_by_name('lvm')
167     
168     try:
169         # wipe the old partition table
170         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % device, log )
171
172         # get the device
173         dev= parted.PedDevice.get(device)
174
175         # create a new partition table
176         disk= dev.disk_new_fresh(parted.disk_type_get("msdos"))
177
178         # create one big partition on each block device
179         constraint= dev.constraint_any()
180
181         new_part= disk.partition_new(
182             parted.PARTITION_PRIMARY,
183             parted.file_system_type_get("ext2"),
184             0, 1 )
185
186         # make it an lvm partition
187         new_part.set_flag(lvm_flag,1)
188
189         # actually add the partition to the disk
190         disk.add_partition(new_part, constraint)
191
192         disk.maximize_partition(new_part,constraint)
193
194         disk.commit()
195         del disk
196             
197     except BootManagerException, e:
198         log.write( "BootManagerException while running: %s\n" % str(e) )
199         return 0
200
201     except parted.error, e:
202         log.write( "parted exception while running: %s\n" % str(e) )
203         return 0
204                    
205     return 1
206
207
208
209 def create_lvm_physical_volume( part_path, vars, log ):
210     """
211     make the specificed partition a lvm physical volume.
212
213     return 1 if successful, 0 otherwise.
214     """
215
216     try:
217         # again, wipe any old data, this time on the partition
218         utils.sysexec( "dd if=/dev/zero of=%s bs=512 count=1" % part_path, log )
219         ### patch Thierry Parmentelat, required on some hardware
220         import time
221         time.sleep(1)
222         utils.sysexec( "pvcreate -ffy %s" % part_path, log )
223     except BootManagerException, e:
224         log.write( "create_lvm_physical_volume failed.\n" )
225         return 0
226
227     return 1
228
229
230
231 def get_partition_path_from_device( device, vars, log ):
232     """
233     given a device, return the path of the first partition on the device
234     """
235
236     # those who wrote the cciss driver just had to make it difficult
237     cciss_test= "/dev/cciss"
238     if device[:len(cciss_test)] == cciss_test:
239         part_path= device + "p1"
240     else:
241         part_path= device + "1"
242
243     return part_path
244
245
246
247 def get_remaining_extents_on_vg( vars, log ):
248     """
249     return the free amount of extents on the planetlab volume group
250     """
251     
252     c_stdout, c_stdin = popen2.popen2("vgdisplay -c planetlab")
253     result= string.strip(c_stdout.readline())
254     c_stdout.close()
255     c_stdin.close()
256     remaining_extents= string.split(result,":")[15]
257     
258     return remaining_extents