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

Diff of /trunk/src/GLIStorageDevice.py

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

Revision 512 Revision 513
7 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False }, 7 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
8 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True }, 8 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
9 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False } 9 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
10 } 10 }
11 11
12##
13# This class provides a partitioning abstraction for the frontends
12class Device: 14class Device:
13 "Class representing a partitionable device." 15 "Class representing a partitionable device."
14 16
15 _device = None 17 _device = None
16 _partitions = None 18 _partitions = None
24 _sector_bytes = 0 26 _sector_bytes = 0
25 _total_mb = 0 27 _total_mb = 0
26 _arch = None 28 _arch = None
27 29
28 ## 30 ##
29 # Brief description of function 31 # Initialization function for GLIStorageDevice class
30 # @param self Parameter description
31 # @param device Parameter description 32 # @param self Parameter description
32 # @param arch="x86" Parameter description 33 # @param device Device node (e.g. /dev/hda) of device being represented
34 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
33 def __init__(self, device, arch="x86"): 35 def __init__(self, device, arch="x86"):
34 self._device = device 36 self._device = device
35 self._partitions = {} 37 self._partitions = {}
36 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 38 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
37 self._total_bytes = 0 39 self._total_bytes = 0
40 self._parted_dev = parted.PedDevice.get(self._device) 42 self._parted_dev = parted.PedDevice.get(self._device)
41 self._parted_disk = parted.PedDisk.new(self._parted_dev) 43 self._parted_disk = parted.PedDisk.new(self._parted_dev)
42 self.set_disk_geometry_from_disk() 44 self.set_disk_geometry_from_disk()
43 45
44 ## 46 ##
45 # Brief description of function 47 # Sets disk geometry info from disk. This function is used internally by __init__()
46 # @param self Parameter description 48 # @param self Parameter description
47 def set_disk_geometry_from_disk(self): 49 def set_disk_geometry_from_disk(self):
48 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 50 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
49 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders 51 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
50 self._sector_bytes = self._parted_dev.sector_size 52 self._sector_bytes = self._parted_dev.sector_size
52 self._total_sectors = self._parted_dev.length 54 self._total_sectors = self._parted_dev.length
53 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 55 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
54 self._total_mb = int(self._total_bytes / MEGABYTE) 56 self._total_mb = int(self._total_bytes / MEGABYTE)
55 57
56 ## 58 ##
57 # Brief description of function 59 # Sets partition info from disk.
58 # @param self Parameter description 60 # @param self Parameter description
59 def set_partitions_from_disk(self): 61 def set_partitions_from_disk(self):
60 last_part = 0 62 last_part = 0
61 last_log_part = 4 63 last_log_part = 4
62 parted_part = self._parted_disk.next_partition() 64 parted_part = self._parted_disk.next_partition()
80 self._partitions[Decimal(str(float(last_part+0.1)))] = Partition(self, Decimal(str(float(last_part+0.1))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False) 82 self._partitions[Decimal(str(float(last_part+0.1)))] = Partition(self, Decimal(str(float(last_part+0.1))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
81 last_part += 1 83 last_part += 1
82 parted_part = self._parted_disk.next_partition(parted_part) 84 parted_part = self._parted_disk.next_partition(parted_part)
83 85
84 ## 86 ##
85 # Brief description of function 87 # Imports partition info from the install profile partition structure (currently does nothing)
86 # @param self Parameter description
87 # @param ips Parameter description 88 # @param self Parameter description
89 # @param ips Parameter structure returned from install_profile.get_partition_tables()
88 def set_partitions_from_install_profile_structure(self, ips): 90 def set_partitions_from_install_profile_structure(self, ips):
89 pass 91 pass
90# for part in ips: 92# for part in ips:
91# tmppart = ips[part] 93# tmppart = ips[part]
92# existing = False 94# existing = False
100# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False: 102# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
101# existing = True 103# existing = True
102# 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'])) 104# 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']))
103 105
104 ## 106 ##
105 # Brief description of function 107 # Returns name of device (e.g. /dev/hda) being represented
106 # @param self Parameter description 108 # @param self Parameter description
107 def get_device(self): 109 def get_device(self):
108 return self._device 110 return self._device
109 111
110 ## 112 ##
111 # Brief description of function 113 # Combines free space and closes gaps in minor numbers. This is used internally
112 # @param self Parameter description 114 # @param self Parameter description
113 def tidy_partitions(self): 115 def tidy_partitions(self):
114 last_minor = 0 116 last_minor = 0
115 last_log_minor = 4 117 last_log_minor = 4
116 last_free = 0 118 last_free = 0
164 del self._partitions[part] 166 del self._partitions[part]
165 continue 167 continue
166 last_minor = part 168 last_minor = part
167 169
168 ## 170 ##
169 # Brief description of function 171 # Adds a new partition to the partition info
170 # @param self Parameter description
171 # @param free_minor Parameter description
172 # @param mb Parameter description
173 # @param start Parameter description
174 # @param end Parameter description 172 # @param self Parameter description
175 # @param type Parameter description 173 # @param free_minor minor of unallocated space partition is being created in
176 # @param mountpoint='' Parameter description 174 # @param mb size of partition in MB
177 # @param mountopts='' Parameter description 175 # @param start Start sector (only used for existing partitions)
176 # @param end End sector (only used for existing partitions)
177 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
178 # @param mountpoint='' Partition mountpoint
179 # @param mountopts='' Partition mount options
178 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''): 180 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
179 free_minor = Decimal(str(free_minor)) 181 free_minor = Decimal(str(free_minor))
180 new_minor = int(free_minor) + 1 182 new_minor = int(free_minor) + 1
181# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor) 183# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
182 if self._partitions.has_key(new_minor): 184 if self._partitions.has_key(new_minor):
212 if type == "extended": 214 if type == "extended":
213 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free") 215 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free")
214 self.tidy_partitions() 216 self.tidy_partitions()
215 217
216 ## 218 ##
217 # Brief description of function 219 # Removes partition from partition info
218 # @param self Parameter description
219 # @param minor Parameter description 220 # @param self Parameter description
221 # @param minor Minor of partition to remove
220 def remove_partition(self, minor): 222 def remove_partition(self, minor):
221 tmppart = self._partitions[int(minor)] 223 tmppart = self._partitions[int(minor)]
222 free_minor = 0 224 free_minor = 0
223 if tmppart.is_logical(): 225 if tmppart.is_logical():
224 free_minor = Decimal(str(float(minor)-0.1)) 226 free_minor = Decimal(str(float(minor)-0.1))
227 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False) 229 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
228 del self._partitions[int(minor)] 230 del self._partitions[int(minor)]
229 self.tidy_partitions() 231 self.tidy_partitions()
230 232
231 ## 233 ##
232 # Brief description of function 234 # Returns free space (no longer used)
233 # @param self Parameter description
234 # @param start Parameter description 235 # @param self Parameter description
236 # @param start Start sector for search
235 def get_free_space(self, start): 237 def get_free_space(self, start):
236 GAP_SIZE = 100 238 GAP_SIZE = 100
237 parts = self._partitions.keys() 239 parts = self._partitions.keys()
238 parts.sort() 240 parts.sort()
239 lastend_pri = 0 241 lastend_pri = 0
267 free_start = lastend_pri 269 free_start = lastend_pri
268 free_end = self._total_sectors 270 free_end = self._total_sectors
269 return (free_start, free_end) 271 return (free_start, free_end)
270 272
271 ## 273 ##
272 # Brief description of function 274 # Gets partition containing a certain sector (no longer used)
273 # @param self Parameter description
274 # @param sector Parameter description 275 # @param self Parameter description
275 # @param ignore_extended=1 Parameter description 276 # @param sector Sector to look at
277 # @param ignore_extended=1 Ignore extended partitions
276 def get_partition_at(self, sector, ignore_extended=1): 278 def get_partition_at(self, sector, ignore_extended=1):
277 parts = self._partitions.keys() 279 parts = self._partitions.keys()
278 parts.sort() 280 parts.sort()
279 for part in parts: 281 for part in parts:
280 tmppart = self._partitions[part] 282 tmppart = self._partitions[part]
282 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()): 284 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
283 return part 285 return part
284 return 0 286 return 0
285 287
286 ## 288 ##
287 # Brief description of function 289 # Returns free minor (no longer used)
288 # @param self Parameter description 290 # @param self Parameter description
289 # @param start Parameter description 291 # @param start Parameter description
290 # @param end Parameter description 292 # @param end Parameter description
291 def get_free_minor_at(self, start, end): 293 def get_free_minor_at(self, start, end):
292 parts = self._partitions.keys() 294 parts = self._partitions.keys()
316 minor = part + 1 318 minor = part + 1
317 lastpart = part 319 lastpart = part
318 return minor 320 return minor
319 321
320 ## 322 ##
321 # Brief description of function 323 # Returns an ordered list (disk order) of partition minors
322 # @param self Parameter description 324 # @param self Parameter description
323 def get_ordered_partition_list(self): 325 def get_ordered_partition_list(self):
324 parts = self._partitions.keys() 326 parts = self._partitions.keys()
325 parts.sort() 327 parts.sort()
326 partlist = [] 328 partlist = []
336 tmppart_log = self._partitions[part_log] 338 tmppart_log = self._partitions[part_log]
337 partlist.append(part_log) 339 partlist.append(part_log)
338 return partlist 340 return partlist
339 341
340 ## 342 ##
341 # Brief description of function 343 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
342 # @param self Parameter description 344 # @param self Parameter description
343 def get_install_profile_structure(self): 345 def get_install_profile_structure(self):
344 devdic = {} 346 devdic = {}
345 for part in self._partitions: 347 for part in self._partitions:
346 tmppart = self._partitions[part] 348 tmppart = self._partitions[part]
347 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() } 349 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() }
348 return devdic 350 return devdic
349 351
350 ## 352 ##
351 # Brief description of function 353 # Returns the minor of the extended partition, if any
352 # @param self Parameter description 354 # @param self Parameter description
353 def get_extended_partition(self): 355 def get_extended_partition(self):
354 for part in self._partitions: 356 for part in self._partitions:
355 tmppart = self._partitions[part] 357 tmppart = self._partitions[part]
356 if tmppart.is_extended(): 358 if tmppart.is_extended():
357 return part 359 return part
358 return 0 360 return 0
359 361
360 ## 362 ##
361 # Brief description of function 363 # Returns the number of sectors on the device
362 # @param self Parameter description 364 # @param self Parameter description
363 def get_num_sectors(self): 365 def get_num_sectors(self):
364 return int(self._total_sectors) 366 return int(self._total_sectors)
365 367
366 ## 368 ##
367 # Brief description of function 369 # Returns the size of a cylinder in bytes
368 # @param self Parameter description 370 # @param self Parameter description
369 def get_cylinder_size(self): 371 def get_cylinder_size(self):
370 return int(self._cylinder_bytes) 372 return int(self._cylinder_bytes)
371 373
372 ## 374 ##
373 # Brief description of function 375 # Returns the size of a sector in bytes
374 # @param self Parameter description 376 # @param self Parameter description
375 def get_sector_size(self): 377 def get_sector_size(self):
376 return int(self._sector_bytes) 378 return int(self._sector_bytes)
377 379
378 ## 380 ##
379 # Brief description of function 381 # Returns the number of cylinders
380 # @param self Parameter description 382 # @param self Parameter description
381 def get_num_cylinders(self): 383 def get_num_cylinders(self):
382 return int(self._geometry['cylinders']) 384 return int(self._geometry['cylinders'])
383 385
384 ## 386 ##
385 # Brief description of function 387 # Returns the total number of bytes on the device
386 # @param self Parameter description 388 # @param self Parameter description
387 def get_drive_bytes(self): 389 def get_drive_bytes(self):
388 return int(self._total_bytes) 390 return int(self._total_bytes)
389 391
390 ## 392 ##
391 # Brief description of function 393 # Returns the total number of MB on the device
392 # @param self Parameter description 394 # @param self Parameter description
393 def get_total_mb(self): 395 def get_total_mb(self):
394 return self._total_mb 396 return self._total_mb
395 397
396 ## 398 ##
397 # Brief description of function 399 # Returns partition info dictionary
398 # @param self Parameter description 400 # @param self Parameter description
399 def get_partitions(self): 401 def get_partitions(self):
400 return self._partitions 402 return self._partitions
401 403
402# def print_partitions(self):
403# for part in self._partitions.keys():
404# print self._partitions[part].return_info()
405
406 ## 404 ##
407 # Brief description of function 405 # Prints disk geometry to STDOUT (no longer used)
408 # @param self Parameter description 406 # @param self Parameter description
409 def print_geometry(self): 407 def print_geometry(self):
410 print self._total_bytes, self._geometry 408 print self._total_bytes, self._geometry
411 409
412 ## 410 ##
413 # Brief description of function 411 # Utility function for raising an exception
414 # @param self Parameter description
415 # @param message Parameter description 412 # @param self Parameter description
413 # @param message Error message
416 def _error(self, message): 414 def _error(self, message):
417 "Raises an exception" 415 "Raises an exception"
418 raise "DeviceObjectError", message 416 raise "DeviceObjectError", message
419 417
420 ## 418 ##
421 # Brief description of function 419 # Utility function for running a command and returning it's output as a list
422 # @param self Parameter description
423 # @param cmd Parameter description 420 # @param self Parameter description
421 # @param cmd Command to run
424 def _run(self, cmd): 422 def _run(self, cmd):
425 "Runs a command and returns the output" 423 "Runs a command and returns the output"
426 424
427 # Run command 425 # Run command
428 output_string = commands.getoutput(cmd) 426 output_string = commands.getoutput(cmd)
442 output_string = output_string[index:] 440 output_string = output_string[index:]
443 441
444 # return output 442 # return output
445 return output_list 443 return output_list
446 444
447 445##
446# This class represents a partition within a GLIStorageDevice object
448class Partition: 447class Partition:
449 "Class representing a single partition within a Device object" 448 "Class representing a single partition within a Device object"
450 449
451 _device = None 450 _device = None
452 _minor = 0 451 _minor = 0

Legend:
Removed from v.512  
changed lines
  Added in v.513

  ViewVC Help
Powered by ViewVC 1.1.20