/[gli]/trunk/src/GLIStorageDevice.py
Gentoo

Diff of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 386 Revision 544
1import commands, string, re, os, parted 1import commands, string, os, parted
2 2
3MEGABYTE = 1024 * 1024
4
5archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
6 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
7 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
8 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
9 }
10
11##
12# This class provides a partitioning abstraction for the frontends
3class Device: 13class Device:
4 "Class representing a partitionable device." 14 "Class representing a partitionable device."
5 15
6 _device = None 16 _device = None
7 _partitions = None 17 _partitions = None
11 _cylinder_bytes = 0 21 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 22 _sectors_in_cylinder = 0
13 _parted_dev = None 23 _parted_dev = None
14 _parted_disk = None 24 _parted_disk = None
15 _sector_bytes = 0 25 _sector_bytes = 0
16 26 _total_mb = 0
27 _arch = None
28
29 ##
30 # Initialization function for GLIStorageDevice class
31 # @param device Device node (e.g. /dev/hda) of device being represented
32 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
17 def __init__(self, device): 33 def __init__(self, device, arch="x86"):
18 self._device = device 34 self._device = device
19 self._partitions = {} 35 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 36 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
21 self._total_bytes = 0 37 self._total_bytes = 0
22 self._cylinder_bytes = 0 38 self._cylinder_bytes = 0
39 self._arch = arch
23 self._parted_dev = parted.PedDevice.get(self._device) 40 self._parted_dev = parted.PedDevice.get(self._device)
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 41 self._parted_disk = parted.PedDisk.new(self._parted_dev)
25 self.set_disk_geometry_from_disk() 42 self.set_disk_geometry_from_disk()
26 43
44 ##
45 # Sets disk geometry info from disk. This function is used internally by __init__()
27 def set_disk_geometry_from_disk(self): 46 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 47 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
29# if string.strip(commands.getoutput("echo " + self._device + " | grep '/hd'")) == self._device: # IDE
30# proc_dir = "/proc/ide/" + commands.getoutput("echo " + self._device + " | cut -d '/' -f 3")
31# proc_dir = string.strip(proc_dir)
32# heads = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 2")
33# sectors = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 3")
34# total_sectors = commands.getoutput("cat " + proc_dir + "/capacity")
35# cylinders = int(total_sectors) / (int(heads) * int(sectors))
36# self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = int(heads), int(sectors), int(cylinders)
37# else: #SCSI
38 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders 48 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
39
40 self._sector_bytes = self._parted_dev.sector_size 49 self._sector_bytes = self._parted_dev.sector_size
41 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes 50 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes
42# self._total_sectors = self._geometry['cylinders'] * self._geometry['heads'] * self._geometry['sectors']
43 self._total_sectors = self._parted_dev.length 51 self._total_sectors = self._parted_dev.length
44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 52 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
53 self._total_mb = int(self._total_bytes / MEGABYTE)
45 54
55 ##
56 # Sets partition info from disk.
46 def set_partitions_from_disk(self): 57 def set_partitions_from_disk(self):
58 last_part = 0
59 last_log_part = 4
47 parted_part = self._parted_disk.next_partition() 60 parted_part = self._parted_disk.next_partition()
48 while parted_part != None: 61 while parted_part:
62 part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
49 if parted_part.num < 1: 63 if parted_part.num >= 1:
50 parted_part = self._parted_disk.next_partition(parted_part)
51 continue
52 fs_type = ""
53 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
54 if parted_part.type == 2: fs_type = "extended"
55 self._partitions[int(parted_part.num)] = Partition(self, parted_part.num, '', parted_part.geom.start, parted_part.geom.end, (parted_part.geom.end - parted_part.geom.start), fs_type, format=False, existing=True)
56 parted_part = self._parted_disk.next_partition(parted_part)
57
58 def set_partitions_from_install_profile_structure(self, ips):
59 for part in ips:
60 tmppart = ips[part]
61 existing = False
62 parted_part = self._parted_disk.get_partition(part)
63 if parted_part != None:
64 start = parted_part.geom.start / self._sectors_in_cylinder
65 end = parted_part.geom.end / self._sectors_in_cylinder
66 fs_type = "" 64 fs_type = ""
67 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 65 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
68 if parted_part.type == 2: fs_type = "extended" 66 if parted_part.type == 2: fs_type = "extended"
67 if archinfo[self._arch]['extended'] and parted_part.num > 4:
68 last_log_part = parted_part.num
69 else:
70 last_part = parted_part.num
71 self._partitions[int(parted_part.num)] = Partition(self, parted_part.num, part_mb, parted_part.geom.start, parted_part.geom.end, fs_type, format=False, existing=True)
72 elif parted_part.type_name == "free":
73 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
74 if parent_part:
75 self._partitions[last_log_part+0.9] = Partition(self, last_log_part+0.9, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
76 last_log_part += 1
77 else:
78 self._partitions[last_part+0.1] = Partition(self, last_part+0.1, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
79 last_part += 1
80 parted_part = self._parted_disk.next_partition(parted_part)
81
82 ##
83 # Imports partition info from the install profile partition structure (currently does nothing)
84 # @param ips Parameter structure returned from install_profile.get_partition_tables()
85 def set_partitions_from_install_profile_structure(self, ips):
86 pass
87# for part in ips:
88# tmppart = ips[part]
89# existing = False
90# parted_part = self._parted_disk.get_partition(part)
91# if parted_part != None:
92# start = parted_part.geom.start / self._sectors_in_cylinder
93# end = parted_part.geom.end / self._sectors_in_cylinder
94# fs_type = ""
95# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
96# if parted_part.type == 2: fs_type = "extended"
69 if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False: 97# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
70 existing = True 98# existing = True
71 self._partitions[int(part)] = Partition(self, part, '', tmppart['start'], tmppart['end'], 0, tmppart['type'], mountopts=tmppart['mountopts'], mountpoint=tmppart['mountpoint'], format=tmppart['format'], existing=(not tmppart['format'])) 99# self._partitions[int(part)] = Partition(self, part, '', tmppart['start'], tmppart['end'], 0, tmppart['type'], mountopts=tmppart['mountopts'], mountpoint=tmppart['mountpoint'], format=tmppart['format'], existing=(not tmppart['format']))
72 100
101 ##
102 # Returns name of device (e.g. /dev/hda) being represented
73 def get_device(self): 103 def get_device(self):
74 return self._device 104 return self._device
75 105
106 ##
107 # Combines free space and closes gaps in minor numbers. This is used internally
76 def clear_partitions(self): 108 def tidy_partitions(self):
77 self._partitions = {} 109 last_minor = 0
110 last_log_minor = 4
111 last_free = 0
112 last_log_free = 0
113 parts = self._partitions.keys()
114 parts.sort()
115 for part in parts:
116 if archinfo[self._arch]['extended'] and part > 4: break
117 tmppart = self._partitions[part]
118 if tmppart.get_type() == "extended":
119 for part_log in parts:
120 if part_log < 4.9: continue
121 tmppart_log = self._partitions[part_log]
122 if tmppart_log.get_type() == "free":
123 if last_log_minor < last_log_free:
124 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
125 del self._partitions[part_log]
126 else:
127 if not last_log_free:
128 last_log_free = last_log_minor + 0.9
129 else:
130 lost_log_free = part_log
131 tmppart_log.set_minor(last_log_free)
132 self._partitions[last_log_free] = tmppart_log
133 if part_log != last_log_free: del self._partitions[part_log]
134 continue
135 else:
136 if part_log > (last_log_minor + 1):
137 tmppart_log.set_minor(last_log_minor + 1)
138 last_log_minor = last_log_minor + 1
139 self._partitions[last_log_minor] = tmppart_log
140 del self._partitions[part_log]
141 continue
142 else:
143 last_log_minor = part_log
144 if tmppart.get_type() == "free":
145 if last_minor < last_free:
146 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
147 del self._partitions[part]
148 else:
149 if not last_free:
150 last_free = last_minor + 0.1
151 else:
152 last_free = part
153 tmppart.set_minor(last_free)
154 self._partitions[last_free] = tmppart
155 if part != last_free: del self._partitions[part]
156 continue
157 else:
158 if part > (last_minor + 1):
159 tmppart.set_minor(last_minor + 1)
160 last_minor = last_minor + 1
161 self._partitions[last_minor] = tmppart
162 del self._partitions[part]
163 continue
164 else:
165 last_minor = part
78 166
167 ##
168 # Adds a new partition to the partition info
169 # @param free_minor minor of unallocated space partition is being created in
170 # @param mb size of partition in MB
171 # @param start Start sector (only used for existing partitions)
172 # @param end End sector (only used for existing partitions)
173 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
174 # @param mountpoint='' Partition mountpoint
175 # @param mountopts='' Partition mount options
79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''): 176 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
80 free_start, free_end = self.get_free_space(start) 177 free_minor = free_minor
81 minor = int(minor) 178 new_minor = int(free_minor) + 1
82 if not free_end: 179# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
83 return False
84 if self._partitions.has_key(minor): 180 if self._partitions.has_key(new_minor):
85 parts = self._partitions.keys() 181 parts = self._partitions.keys()
86 parts.sort() 182 parts.sort()
87 parts.reverse() 183 parts.reverse()
88 hole_at = 0 184 hole_at = 0
89 for i in range(1, parts[0]+1): 185 for i in range(1, parts[0]+1):
90 if i <= minor: continue 186 if i <= new_minor: continue
91 if not self._partitions.has_key(int(i)): 187 if not self._partitions.has_key(i):
92 hole_at = i 188 hole_at = i
93 break 189 break
94 stopscooting = 0 190 stopscooting = 0
95 for i in parts: 191 for i in parts:
96 if stopscooting: break 192 if stopscooting: break
97 if (i >= hole_at) and (hole_at): continue 193 if i >= hole_at and hole_at: continue
98 if i >= minor: 194 if i >= new_minor:
99 self._partitions[i].set_minor(i+1) 195 self._partitions[i].set_minor(i+1)
100 self._partitions[i+1] = self._partitions[i] 196 self._partitions[i+1] = self._partitions[i]
101 if i == minor: stopscooting = 1 197 if i == new_minor: stopscooting = 1
198 if mb != self._partitions[free_minor].get_mb():
199 old_free_mb = self._partitions[free_minor].get_mb()
200 del self._partitions[free_minor]
201 if archinfo[self._arch]['extended'] and new_minor >= 5:
202 free_minor = new_minor + 0.9
203 else:
204 free_minor = new_minor + 0.1
205 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
206# print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
207 else:
208 del self._partitions[free_minor]
102 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type, mountpoint=mountpoint, mountopts=mountopts) 209 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
210 if type == "extended":
211 self._partitions[4.9] = Partition(self, 4.9, mb, 0, 0, "free")
212 self.tidy_partitions()
103 213
214 ##
215 # Removes partition from partition info
216 # @param minor Minor of partition to remove
104 def remove_partition(self, minor): 217 def remove_partition(self, minor):
218 tmppart = self._partitions[int(minor)]
219 free_minor = 0
220 if tmppart.is_logical():
221 free_minor = minor-0.1
222 else:
223 free_minor = minor-0.9
224 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
105 del self._partitions[int(minor)] 225 del self._partitions[int(minor)]
226 self.tidy_partitions()
106 227
228 ##
229 # Returns free space (no longer used)
230 # @param start Start sector for search
107 def get_free_space(self, start): 231 def get_free_space(self, start):
108 GAP_SIZE = 100 232 GAP_SIZE = 100
109 parts = self._partitions.keys() 233 parts = self._partitions.keys()
110 parts.sort() 234 parts.sort()
111 lastend_pri = 0 235 lastend_pri = 0
138 if free_start == -1 and lastend_pri < self._total_sectors: 262 if free_start == -1 and lastend_pri < self._total_sectors:
139 free_start = lastend_pri 263 free_start = lastend_pri
140 free_end = self._total_sectors 264 free_end = self._total_sectors
141 return (free_start, free_end) 265 return (free_start, free_end)
142 266
267 ##
268 # Gets partition containing a certain sector (no longer used)
269 # @param sector Sector to look at
270 # @param ignore_extended=1 Ignore extended partitions
143 def get_partition_at(self, sector, ignore_extended=1): 271 def get_partition_at(self, sector, ignore_extended=1):
144 parts = self._partitions.keys() 272 parts = self._partitions.keys()
145 parts.sort() 273 parts.sort()
146 for part in parts: 274 for part in parts:
147 tmppart = self._partitions[part] 275 tmppart = self._partitions[part]
148 if ignore_extended and tmppart.is_extended(): continue 276 if ignore_extended and tmppart.is_extended(): continue
149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()): 277 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
150 return part 278 return part
151 return 0 279 return 0
152 280
281 ##
282 # Returns free minor (no longer used)
283 # @param start Parameter description
284 # @param end Parameter description
153 def get_free_minor_at(self, start, end): 285 def get_free_minor_at(self, start, end):
154 parts = self._partitions.keys() 286 parts = self._partitions.keys()
155 parts.sort() 287 parts.sort()
156 minor = 1 288 minor = 1
157 lastpart = 0 289 lastpart = 0
177 break 309 break
178 minor = part + 1 310 minor = part + 1
179 lastpart = part 311 lastpart = part
180 return minor 312 return minor
181 313
314 ##
315 # Returns an ordered list (disk order) of partition minors
182 def get_ordered_partition_list(self): 316 def get_ordered_partition_list(self):
183 parts = self._partitions.keys() 317 parts = self._partitions.keys()
184 parts.sort() 318 parts.sort()
185 partlist = [] 319 partlist = []
186 free_start, free_end = self.get_free_space(0)
187 tmppart = None
188 tmppart_log = None 320 tmppart = None
189 for part in parts: 321 for part in parts:
190 if part > 4: break 322 if archinfo[self._arch]['extended'] and part > 4.1: break
191 tmppart = self._partitions[part] 323 tmppart = self._partitions[part]
192 if free_end < tmppart.get_start() and not free_start == -1:
193 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
194 free_start, free_end = self.get_free_space(free_end)
195 newitem = self._device + str(part) + ": " + str(tmppart.get_start()) + "-" + str(tmppart.get_end())
196 if tmppart.is_extended(): newitem = newitem + " extended"
197 partlist.append(newitem) 324 partlist.append(part)
198 if tmppart.is_extended(): 325 if tmppart.is_extended():
199 for part_log in parts: 326 for part_log in parts:
200 if part_log < 5: continue 327 if part_log < 4.9: continue
201 tmppart_log = self._partitions[part_log]
202 if free_end < tmppart_log.get_start() and free_end <= tmppart.get_end() and not free_start == -1:
203 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
204 free_start, free_end = self.get_free_space(free_end)
205 newitem = self._device + str(part_log) + ": " + str(tmppart_log.get_start()) + "-" + str(tmppart_log.get_end()) + " logical"
206 partlist.append(newitem) 328 partlist.append(part_log)
207 if ((tmppart_log == None) or (free_start > tmppart_log.get_end())) and free_start < tmppart.get_end():
208 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ") logical")
209 free_start, free_end = self.get_free_space(free_end)
210 if (tmppart == None) or (free_start > tmppart.get_end()):
211 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
212 return partlist 329 return partlist
213 330
331 ##
332 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
214 def get_install_profile_structure(self): 333 def get_install_profile_structure(self):
215 devdic = {} 334 devdic = {}
216 for part in self._partitions: 335 for part in self._partitions:
217 tmppart = self._partitions[part] 336 tmppart = self._partitions[part]
218 devdic[part] = { 'mb': 0, 'minor': int(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() } 337 devdic[part] = { 'mb': tmppart.get_mb(), 'minor': float(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() }
219 return devdic 338 return devdic
220 339
340 ##
341 # Returns the minor of the extended partition, if any
221 def get_extended_partition(self): 342 def get_extended_partition(self):
222 for part in self._partitions: 343 for part in self._partitions:
223 tmppart = self._partitions[part] 344 tmppart = self._partitions[part]
224 if tmppart.is_extended(): 345 if tmppart.is_extended():
225 return part 346 return part
226 return 0 347 return 0
227 348
349 ##
350 # Returns the number of sectors on the device
228 def get_num_sectors(self): 351 def get_num_sectors(self):
229 return int(self._total_sectors) 352 return int(self._total_sectors)
230 353
354 ##
355 # Returns the size of a cylinder in bytes
231 def get_cylinder_size(self): 356 def get_cylinder_size(self):
232 return int(self._cylinder_bytes) 357 return int(self._cylinder_bytes)
233 358
359 ##
360 # Returns the size of a sector in bytes
234 def get_sector_size(self): 361 def get_sector_size(self):
235 return int(self._sector_bytes) 362 return int(self._sector_bytes)
236 363
364 ##
365 # Returns the number of cylinders
237 def get_num_cylinders(self): 366 def get_num_cylinders(self):
238 return int(self._geometry['cylinders']) 367 return int(self._geometry['cylinders'])
239 368
369 ##
370 # Returns the total number of bytes on the device
240 def get_drive_bytes(self): 371 def get_drive_bytes(self):
241 return int(self._total_bytes) 372 return int(self._total_bytes)
242 373
374 ##
375 # Returns the total number of MB on the device
376 def get_total_mb(self):
377 return self._total_mb
378
379 ##
380 # Returns partition info dictionary
243 def get_partitions(self): 381 def get_partitions(self):
244 return self._partitions 382 return self._partitions
245 383
246 def print_partitions(self): 384 ##
247 for part in self._partitions.keys(): 385 # Prints disk geometry to STDOUT (no longer used)
248 print self._partitions[part].return_info()
249
250 def print_geometry(self): 386 def print_geometry(self):
251 print self._total_bytes, self._geometry 387 print self._total_bytes, self._geometry
252 388
389 ##
390 # Utility function for raising an exception
391 # @param message Error message
253 def _error(self, message): 392 def _error(self, message):
254 "Raises an exception" 393 "Raises an exception"
255 raise "DeviceObjectError", message 394 raise GLIException("DeviceObjectError", 'fatal', 'Device', message)
256 395
396 ##
397 # Utility function for running a command and returning it's output as a list
398 # @param cmd Command to run
257 def _run(self, cmd): 399 def _run(self, cmd):
258 "Runs a command and returns the output" 400 "Runs a command and returns the output"
259 401
260 # Run command 402 # Run command
261 output_string = commands.getoutput(cmd) 403 output_string = commands.getoutput(cmd)
275 output_string = output_string[index:] 417 output_string = output_string[index:]
276 418
277 # return output 419 # return output
278 return output_list 420 return output_list
279 421
280 422##
423# This class represents a partition within a GLIStorageDevice object
281class Partition: 424class Partition:
282 "Class representing a single partition within a Device object" 425 "Class representing a single partition within a Device object"
283 426
284 _device = None 427 _device = None
285 _minor = None 428 _minor = 0
286 _orig_minor = "" 429 _orig_minor = 0
287 _bootflag = None
288 _start = None 430 _start = 0
289 _end = None 431 _end = 0
290 _blocks = None
291 _type = None 432 _type = None
292 _mountpoint = None 433 _mountpoint = None
293 _mountopts = None 434 _mountopts = None
294 _format = None 435 _format = None
295 _resizeable = None 436 _resizeable = None
296 _min_sectors_for_resize = 0 437 _min_mb_for_resize = 0
438 _mb = 0
297 439
440 ##
441 # Initialization function for the Partition class
442 # @param device Parent GLIStorageDevice object
443 # @param minor Minor of partition
444 # @param mb Parameter Size of partition in MB
445 # @param start Parameter Start sector of partition
446 # @param end Parameter Start sector of partition
447 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
448 # @param mountpoint='' Mountpoint of partition
449 # @param mountopts='' Mount options of partition
450 # @param format=True Format partition
451 # @param existing=False This partition exists on disk
298 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 452 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False):
299 self._device = device 453 self._device = device
300 self._minor = int(minor) 454 self._minor = float(minor)
301 if existing: self._orig_minor = int(minor) 455 if existing: self._orig_minor = int(minor)
302 self._bootflag = bootflag
303 self._start = int(start) 456 self._start = int(start)
304 self._end = int(end) 457 self._end = int(end)
305 self._blocks = int(blocks)
306 if type == "": type = "unknown" 458 self._type = type or "unknown"
307 self._type = type
308 self._mountpoint = mountpoint 459 self._mountpoint = mountpoint
309 self._mountopts = mountopts 460 self._mountopts = mountopts
310 self._format = format 461 self._format = format
311 if blocks == 0: 462 self._mb = mb
312 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
313 if existing: 463 if existing:
464 self._orig_minor = self._minor
314 parted_part = device._parted_disk.get_partition(minor) 465 parted_part = device._parted_disk.get_partition(minor)
466 label_type = device._parted_disk.type.name
467 if label_type == "loop":
468 dev_node = device._device
469 else:
470 dev_node = device._device + str(minor)
315 if type == "ntfs": 471 if type == "ntfs":
316 min_bytes = int(commands.getoutput("ntfsresize -f --info " + device._device + str(minor) + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'")) 472 min_bytes = int(commands.getoutput("ntfsresize -f --info " + dev_node + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
317 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 473 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
318 self._resizeable == True 474 self._resizeable = True
319 elif type == "ext2" or type == "ext3": 475 elif type == "ext2" or type == "ext3":
320 block_size = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")) 476 block_size = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")))
321 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")) 477 free_blocks = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
322 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes) 478 free_bytes = int(block_size * free_blocks)
323 free_sec = free_sec - 2000 # just to be safe 479 # can't hurt to pad (the +50) it a bit since this is really just a guess
324 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec 480 self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50
325 self._resizeable == True 481 self._resizeable = True
326 else: 482 else:
327 parted_part = self._device._parted_disk.get_partition(self._minor) 483 parted_part = self._device._parted_disk.get_partition(int(self._minor))
328 try: 484 try:
329 parted_fs = parted_part.geom.file_system_open() 485 parted_fs = parted_part.geom.file_system_open()
330 except: 486 except:
331 self._resizeable = False 487 self._resizeable = False
332 return 488 return
333 resize_constraint = parted_fs.get_resize_constraint() 489 resize_constraint = parted_fs.get_resize_constraint()
334 min_size = resize_constraint.min_size 490 min_bytes = resize_constraint.min_size * self._device._sector_bytes
335 if int(min_size) != min_size: min_size = int(min_size) + 1 491 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
336 self._min_sectors_for_resize = min_size
337 self._resizeable = True 492 self._resizeable = True
338 493
494 ##
495 # Returns whether or not the partition is extended
339 def is_extended(self): 496 def is_extended(self):
340 if self._type == "extended": 497 if self._type == "extended":
341 return True 498 return True
342 else: 499 else:
343 return False 500 return False
344 501
502 ##
503 # Returns whether or not the partition is logical
345 def is_logical(self): 504 def is_logical(self):
346 part = self._device.get_partition_at(self._start, ignore_extended=0) 505 if self._type == "free":
347 if part and self._device._partitions[part].is_extended() and not part == self._minor: 506 if int(self._minor) + 0.9 == self._minor:
507 return True
508 else:
509 return False
510 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
348 return True 511 return True
512 else:
349 return False 513 return False
350 514
515 ##
516 # Returns a list of logical partitions if this is an extended partition
351 def get_logicals(self): 517 def get_logicals(self):
352 if not self.is_extended(): 518 if not self.is_extended():
353 return None 519 return None
354 logicals = [] 520 logicals = []
355 start = self._start
356 parts = self._device._partitions.keys() 521 parts = self._device._partitions.keys()
357 parts.sort() 522 parts.sort()
358 for part in parts: 523 for part in parts:
359 if part < 5: continue 524 if part < 5: continue
360 logicals.append(part) 525 logicals.append(part)
361 logicals.sort() 526 logicals.sort()
362 return logicals 527 return logicals
363 528
529 ##
530 # Returns the extened parent partition if this is a logical partition (no longer used)
364 def get_extended_parent(self): 531 def get_extended_parent(self):
365 if not self.is_logical(): 532 if not self.is_logical():
366 return None 533 return None
367 else: 534 else:
368 return self._device.get_partition_at(self._start, ignore_extended=0) 535 return self._device.get_partition_at(self._start, ignore_extended=0)
369 536
537 ##
538 # Sets the start sector for the partition
539 # @param start Start sector
370 def set_start(self, start): 540 def set_start(self, start):
371 self._start = int(start) 541 self._start = int(start)
372 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
373 542
543 ##
544 # Returns the start sector for the partition
374 def get_start(self): 545 def get_start(self):
375 return int(self._start) 546 return int(self._start)
376 547
548 ##
549 # Sets the end sector of the partition
550 # @param end End sector
377 def set_end(self, end): 551 def set_end(self, end):
378 self._end = int(end) 552 self._end = int(end)
379 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
380 553
554 ##
555 # Returns end sector for the partition
381 def get_end(self): 556 def get_end(self):
382 return int(self._end) 557 return int(self._end)
383 558
559 ##
560 # Returns size of partition in MB
561 def get_mb(self):
562 return int(self._mb)
563
564 ##
565 # Sets size of partition in MB
566 # @param mb Parameter description
567 def set_mb(self, mb):
568 self._mb = int(mb)
569
570 ##
571 # Sets type of partition
572 # @param type Parameter description
384 def set_type(self, type): 573 def set_type(self, type):
385 self._type = type 574 self._type = type
386 575
576 ##
577 # Returns type of partition
387 def get_type(self): 578 def get_type(self):
388 return self._type 579 return self._type
389 580
581 ##
582 # Returns parent GLIStorageDevice object
390 def get_device(self): 583 def get_device(self):
391 return self._device 584 return self._device
392 585
586 ##
587 # Sets minor of partition
588 # @param minor New minor
393 def set_minor(self, minor): 589 def set_minor(self, minor):
394 self._minor = int(minor) 590 self._minor = float(minor)
395 591
592 ##
593 # Returns minor of partition
396 def get_minor(self): 594 def get_minor(self):
397 return int(self._minor) 595 return float(self._minor)
398 596
597 ##
598 # Sets the original minor of the partition
599 # @param orig_minor Parameter description
399 def set_orig_minor(self, orig_minor): 600 def set_orig_minor(self, orig_minor):
400 self._orig_minor = int(orig_minor) 601 self._orig_minor = int(orig_minor)
401 602
603 ##
604 # Returns the original minor of the partition
402 def get_orig_minor(self): 605 def get_orig_minor(self):
403 return self._orig_minor 606 return self._orig_minor
404 607
608 ##
609 # Sets the mountpoint for the partition
610 # @param mountpoint Mountpoint
405 def set_mountpoint(self, mountpoint): 611 def set_mountpoint(self, mountpoint):
406 self._mountpoint = mountpoint 612 self._mountpoint = mountpoint
407 613
614 ##
615 # Returns the mountpoint for the partition
408 def get_mountpoint(self): 616 def get_mountpoint(self):
409 return self._mountpoint 617 return self._mountpoint
410 618
619 ##
620 # Sets the mount options for the partition
621 # @param mountopts Mount options
411 def set_mountopts(self, mountopts): 622 def set_mountopts(self, mountopts):
412 self._mountopts = mountopts 623 self._mountopts = mountopts
413 624
625 ##
626 # Returns the mount options for the partition
414 def get_mountopts(self): 627 def get_mountopts(self):
415 return self._mountopts 628 return self._mountopts
416 629
630 ##
631 # Set whether to format the partition
632 # @param format Format this partition (True/False)
417 def set_format(self, format): 633 def set_format(self, format):
418 self._format = format 634 self._format = format
419 635
636 ##
637 # Returns whether to format the partition
420 def get_format(self): 638 def get_format(self):
421 return self._format 639 return self._format
422 640
423 def get_blocks(self): 641 ##
424 return int(self._blocks) 642 # Returns whether the partition is resizeable
643 def is_resizeable(self):
644 return self._resizeable
425 645
646 ##
647 # Returns minimum MB for resize
426 def get_min_sectors_for_resize(self): 648 def get_min_mb_for_resize(self):
427 if self.is_extended(): 649# if self.is_extended():
428 min_size = self._start 650# min_size = self._start
429 for part in self._device._partitions: 651# for part in self._device._partitions:
430 if part < 5: continue 652# if part < 5: continue
431 if part.get_end > min_size: min_size = part.get_end() 653# if part.get_end > min_size: min_size = part.get_end()
432 return min_size 654# return min_size
655# else:
656 if self._resizeable:
657 return self._min_mb_for_resize
433 else: 658 else:
434 return self._min_sectors_for_resize 659 return -1
435 660
661 ##
662 # Returns maximum MB for resize
436 def get_max_sectors_for_resize(self): 663 def get_max_mb_for_resize(self):
437 free_start, free_end = self._device.get_free_space(self._end) 664 if self._resizeable:
438 if free_end == -1: return self._end 665 free_minor = 0
439 if free_start - 1 == self._end:
440 if self.is_logical(): 666 if self.is_logical():
441 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 667 free_minor = self._minor + 0.9
442 return free_end - self._start
443 else:
444 return self._end - self._start
445 else: 668 else:
446 return free_end - self._start 669 free_minor = self._minor + 0.1
670 if free_minor in self._device._partitions:
671 return self._mb + self._device._partitions[free_minor]._mb
672 else:
673 return self._mb
674 else:
675 return -1
447 676
677 ##
678 # Resizes the partition
679 # @param mb New size in MB
448 def resize(self, start, end): 680 def resize(self, mb):
449 part_at_start = self._device.get_partition_at(int(start)) 681 free_minor = self._minor
450 part_at_end = self._device.get_partition_at(int(end))
451 logicals = None
452 if self.is_logical(): 682 if self.is_logical():
453 parent = self.get_extended_parent() 683 free_minor += 0.9
454 parentstart = int(self._device._partitions[parent].get_start()) 684 else:
455 parentend = int(self._device._partitions[parent].get_end()) 685 free_minor += 0.1
456 if (start < parentstart) or (end > parentend): return 0 686 if mb < self._mb:
457 if self.is_extended(): 687 # Shrinking
458 logicals = self.get_logicals() 688 if not free_minor in self._device._partitions:
459 if len(logicals): 689 self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
460 logicals_start = self._device._partitions[logicals[0]].get_start() 690 self._device._partitions[free_minor]._mb += self._mb - mb
461 logicals_end = self._device._partitions[logicals[len(logicals)-1]].get_end() 691 self._mb = mb
462 if (start > logicals_start) or (end < logicals_end): return 0 692 elif mb == self._mb + self._device._partitions[free_minor]._mb:
463 if part_at_start in logicals: part_at_start = 0 693 # Using all available unallocated space
464 if part_at_end in logicals: part_at_end = 0 694 del self._device._partitions[free_minor]
465 if ((not part_at_start == 0) and (part_at_start != self._minor)) or ((not part_at_end == 0) and (part_at_end != self._minor)): 695 self._mb = mb
466 return 0 696 elif mb > self._mb:
467 self.set_start(start) 697 # Growing
468 self.set_end(end) 698 self._device._partitions[free_minor]._mb = mb - self._mb
469 return 1 699 self._mb = mb
470 700
701 ##
702 # Utility function to raise an exception
703 # @param message Error message
471 def _error(self, message): 704 def _error(self, message):
472 "Raises an exception" 705 raise GLIException("PartitionObjectError", 'fatal', 'Partition', message)
473 raise "PartitionObjectError", message
474
475 706
707##
708# Returns a list of detected partitionable devices
476def detect_devices(): 709def detect_devices():
477 "Returns a list of partitionable devices on the system"
478
479 devices = [] 710 devices = []
480 711
481 # Make sure sysfs exists 712 # Make sure sysfs exists
482 if not os.path.exists("/sys/bus"): 713 if not os.path.exists("/sys/bus"):
483 raise Exception, "no sysfs found (you MUST use a kernel >2.6)" 714 raise Exception, "no sysfs found (you MUST use a kernel >2.6)"
507 # all the devices in /dev to try and find a 738 # all the devices in /dev to try and find a
508 # devices with the same major and minor 739 # devices with the same major and minor
509 if not os.path.exists(device): 740 if not os.path.exists(device):
510 device = None 741 device = None
511 for path, dirs, files in os.walk("/dev"): 742 for path, dirs, files in os.walk("/dev"):
512 for file in files: 743 for d_file in files:
513 full_file = os.path.join(path, file) 744 full_file = os.path.join(path, d_file)
514 if not os.path.exists(full_file): 745 if not os.path.exists(full_file):
515 continue 746 continue
516 statres = os.stat(full_file) 747 statres = os.stat(full_file)
517 fmaj = os.major(statres.st_rdev) 748 fmaj = os.major(statres.st_rdev)
518 fmin = os.minor(statres.st_rdev) 749 fmin = os.minor(statres.st_rdev)

Legend:
Removed from v.386  
changed lines
  Added in v.544

  ViewVC Help
Powered by ViewVC 1.1.20