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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 494 - (show annotations) (download) (as text)
Sun Apr 10 05:05:45 2005 UTC (15 years, 6 months ago) by agaffney
File MIME type: text/x-python
File size: 20955 byte(s)
tidy_partitions() function in GLIStorageDevice and remove debug statements (gtkfe also)

1 import commands, string, re, os, parted
2 from decimal import Decimal
3
4 MEGABYTE = 1024 * 1024
5
6 class Device:
7 "Class representing a partitionable device."
8
9 _device = None
10 _partitions = None
11 _geometry = None
12 _total_bytes = 0
13 _total_sectors = 0
14 _cylinder_bytes = 0
15 _sectors_in_cylinder = 0
16 _parted_dev = None
17 _parted_disk = None
18 _sector_bytes = 0
19 _total_mb = 0
20 _arch = None
21
22 archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
23 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
24 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
25 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
26 }
27
28 def __init__(self, device, arch="x86"):
29 self._device = device
30 self._partitions = {}
31 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
32 self._total_bytes = 0
33 self._cylinder_bytes = 0
34 self._arch = arch
35 self._parted_dev = parted.PedDevice.get(self._device)
36 self._parted_disk = parted.PedDisk.new(self._parted_dev)
37 self.set_disk_geometry_from_disk()
38
39 def set_disk_geometry_from_disk(self):
40 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
41 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
42 self._sector_bytes = self._parted_dev.sector_size
43 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes
44 self._total_sectors = self._parted_dev.length
45 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
46 self._total_mb = int(self._total_bytes / MEGABYTE)
47
48 def set_partitions_from_disk(self):
49 last_part = 0
50 last_log_part = 4
51 parted_part = self._parted_disk.next_partition()
52 while parted_part:
53 part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
54 if parted_part.num >= 1:
55 fs_type = ""
56 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
57 if parted_part.type == 2: fs_type = "extended"
58 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)
59 elif parted_part.type_name == "free":
60 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
61 if parent_part:
62 self._partitions[Decimal(str(float(last_log_part+0.9)))] = Partition(self, Decimal(str(float(last_log_part+0.9))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
63 last_log_part += 1
64 else:
65 self._partitions[Decimal(str(float(last_log_part+0.1)))] = Partition(self, Decimal(str(float(last_log_part+0.1))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
66 last_part += 1
67 parted_part = self._parted_disk.next_partition(parted_part)
68
69 def set_partitions_from_install_profile_structure(self, ips):
70 pass
71 # for part in ips:
72 # tmppart = ips[part]
73 # existing = False
74 # parted_part = self._parted_disk.get_partition(part)
75 # if parted_part != None:
76 # start = parted_part.geom.start / self._sectors_in_cylinder
77 # end = parted_part.geom.end / self._sectors_in_cylinder
78 # fs_type = ""
79 # if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
80 # if parted_part.type == 2: fs_type = "extended"
81 # if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
82 # existing = True
83 # 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']))
84
85 def get_device(self):
86 return self._device
87
88 def tidy_partitions(self):
89 last_minor = 0
90 last_log_minor = 4
91 last_free = 0
92 last_log_free = 0
93 parts = self._partitions.keys()
94 parts.sort()
95 for part in parts:
96 if self.archinfo[self._arch]['extended'] and part > 4: break
97 tmppart = self._partitions[part]
98 if tmppart.get_type() == "extended":
99 for part_log in parts:
100 if part_log < Decimal("4.9"): continue
101 tmppart_log = self._partitions[part_log]
102 if tmppart_log.get_type() == "free":
103 if not last_log_free:
104 tmppart_log.set_minor(Decimal(str(last_log_minor + 0.9)))
105 else:
106 if last_log_minor < last_log_free:
107 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
108 del self._partitions[part_log]
109 last_log_free = part_log
110 else:
111 if part_log > (last_log_minor + 1):
112 tmppart_log.set_minor(last_log_minor + 1)
113 last_log_minor = part_log
114 if tmppart.get_type() == "free":
115 if not last_free:
116 tmppart.set_minor(Decimal(str(last_minor + 0.1)))
117 else:
118 if last_minor < last_free:
119 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
120 del self_partitions[part]
121 last_free = part
122 else:
123 if part > (last_minor + 1):
124 tmppart.set_minor(last_minor + 1)
125 last_minor = part
126
127 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
128 free_minor = Decimal(str(free_minor))
129 new_minor = int(free_minor) + 1
130 # print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
131 if self._partitions.has_key(new_minor):
132 parts = self._partitions.keys()
133 parts.sort()
134 parts.reverse()
135 hole_at = 0
136 for i in range(1, parts[0]+1):
137 if i <= new_minor: continue
138 if not self._partitions.has_key(i):
139 hole_at = i
140 break
141 stopscooting = 0
142 for i in parts:
143 if stopscooting: break
144 if i >= hole_at and hole_at: continue
145 if i >= new_minor:
146 self._partitions[i].set_minor(i+1)
147 self._partitions[i+1] = self._partitions[i]
148 if i == new_minor: stopscooting = 1
149 if mb != self._partitions[free_minor].get_mb():
150 old_free_mb = self._partitions[free_minor].get_mb()
151 del self._partitions[free_minor]
152 if self.archinfo[self._arch]['extended'] and new_minor >= 5:
153 free_minor = Decimal(str(new_minor + 0.9))
154 else:
155 free_minor = Decimal(str(new_minor + 0.1))
156 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
157 # print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
158 else:
159 del self._partitions[free_minor]
160 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
161 if type == "extended":
162 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free")
163 self.tidy_partitions()
164
165 def remove_partition(self, minor):
166 tmppart = self._partitions[int(minor)]
167 free_minor = 0
168 if tmppart.is_logical():
169 free_minor = Decimal(str(float(minor)-0.1))
170 else:
171 free_minor = Decimal(str(float(minor)-0.9))
172 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
173 del self._partitions[int(minor)]
174 self.tidy_partitions()
175
176 def get_free_space(self, start):
177 GAP_SIZE = 100
178 parts = self._partitions.keys()
179 parts.sort()
180 lastend_pri = 0
181 lastend_log = 0
182 free_start = -1
183 free_end = -1
184 if start > self._total_sectors: return (-1, -1)
185 for part in parts:
186 if part > 4: break
187 tmppart = self._partitions[part]
188 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
189 free_start = lastend_pri
190 free_end = tmppart.get_start() - 1
191 break
192 if tmppart.is_extended() and start < tmppart.get_end():
193 lastend_log = tmppart.get_start()
194 for part_log in parts:
195 if part_log < 5: continue
196 tmppart_log = self._partitions[part_log]
197 if (tmppart_log.get_start() > (lastend_log + GAP_SIZE)) and (lastend_log >= start):
198 free_start = lastend_log
199 free_end = tmppart_log.get_start() - 1
200 break
201 lastend_log = tmppart_log.get_end() + 1
202 if free_start == -1 and lastend_log < tmppart.get_end():
203 free_start = lastend_log
204 free_end = tmppart.get_end()
205 break
206 lastend_pri = tmppart.get_end() + 1
207 if free_start == -1 and lastend_pri < self._total_sectors:
208 free_start = lastend_pri
209 free_end = self._total_sectors
210 return (free_start, free_end)
211
212 def get_partition_at(self, sector, ignore_extended=1):
213 parts = self._partitions.keys()
214 parts.sort()
215 for part in parts:
216 tmppart = self._partitions[part]
217 if ignore_extended and tmppart.is_extended(): continue
218 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
219 return part
220 return 0
221
222 def get_free_minor_at(self, start, end):
223 parts = self._partitions.keys()
224 parts.sort()
225 minor = 1
226 lastpart = 0
227 for part in parts:
228 if part > 4: break
229 tmppart = self._partitions[part]
230 if end < tmppart.get_start():
231 minor = part
232 if (minor - 1) > lastpart: minor = lastpart + 1
233 break
234 if tmppart.is_extended() and start < tmppart.get_end():
235 minor = 5
236 lastpart = 4
237 for part_log in parts:
238 if part_log < 5: continue
239 tmppart_log = self._partitions[part_log]
240 if end < tmppart_log.get_start():
241 minor = part_log
242 if (minor - 1) > lastpart: minor = lastpart + 1
243 break
244 minor = part_log + 1
245 lastpart = part_log
246 break
247 minor = part + 1
248 lastpart = part
249 return minor
250
251 def get_ordered_partition_list(self):
252 parts = self._partitions.keys()
253 parts.sort()
254 partlist = []
255 tmppart = None
256 tmppart_log = None
257 for part in parts:
258 if self.archinfo[self._arch]['extended'] and part > Decimal("4.1"): break
259 tmppart = self._partitions[part]
260 partlist.append(part)
261 if tmppart.is_extended():
262 for part_log in parts:
263 if part_log < Decimal("4.9"): continue
264 tmppart_log = self._partitions[part_log]
265 partlist.append(part_log)
266 return partlist
267
268 def get_install_profile_structure(self):
269 devdic = {}
270 for part in self._partitions:
271 tmppart = self._partitions[part]
272 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() }
273 return devdic
274
275 def get_extended_partition(self):
276 for part in self._partitions:
277 tmppart = self._partitions[part]
278 if tmppart.is_extended():
279 return part
280 return 0
281
282 def get_num_sectors(self):
283 return int(self._total_sectors)
284
285 def get_cylinder_size(self):
286 return int(self._cylinder_bytes)
287
288 def get_sector_size(self):
289 return int(self._sector_bytes)
290
291 def get_num_cylinders(self):
292 return int(self._geometry['cylinders'])
293
294 def get_drive_bytes(self):
295 return int(self._total_bytes)
296
297 def get_total_mb(self):
298 return self._total_mb
299
300 def get_partitions(self):
301 return self._partitions
302
303 # def print_partitions(self):
304 # for part in self._partitions.keys():
305 # print self._partitions[part].return_info()
306
307 def print_geometry(self):
308 print self._total_bytes, self._geometry
309
310 def _error(self, message):
311 "Raises an exception"
312 raise "DeviceObjectError", message
313
314 def _run(self, cmd):
315 "Runs a command and returns the output"
316
317 # Run command
318 output_string = commands.getoutput(cmd)
319
320 # What we will return
321 output_list = []
322
323 # As long as there is a new line in the output_string
324 while output_string.find("\n") != -1:
325
326 # Find the \n in the string
327 index = output_string.find("\n") + 1
328
329 # Add the line to the output and remove it from
330 # the output_string
331 output_list.append(output_string[:index])
332 output_string = output_string[index:]
333
334 # return output
335 return output_list
336
337
338 class Partition:
339 "Class representing a single partition within a Device object"
340
341 _device = None
342 _minor = 0
343 _orig_minor = 0
344 _start = 0
345 _end = 0
346 _type = None
347 _mountpoint = None
348 _mountopts = None
349 _format = None
350 _resizeable = None
351 _min_sectors_for_resize = 0
352 _mb = 0
353
354 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False):
355 self._device = device
356 self._minor = float(minor)
357 if existing: self._orig_minor = int(minor)
358 self._start = int(start)
359 self._end = int(end)
360 self._type = type or "unknown"
361 self._mountpoint = mountpoint
362 self._mountopts = mountopts
363 self._format = format
364 self._mb = mb
365 if existing:
366 self._orig_minor = self._minor
367 parted_part = device._parted_disk.get_partition(minor)
368 label_type = device._parted_disk.type.name
369 if label_type == "loop":
370 dev_node = device._device
371 else:
372 dev_node = device._device + str(minor)
373 if type == "ntfs":
374 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 .\+//'"))
375 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
376 self._resizeable == True
377 elif type == "ext2" or type == "ext3":
378 block_size = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'"))
379 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'"))
380 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes)
381 free_sec = free_sec - 2000 # just to be safe
382 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
383 self._resizeable == True
384 else:
385 parted_part = self._device._parted_disk.get_partition(int(self._minor))
386 try:
387 parted_fs = parted_part.geom.file_system_open()
388 except:
389 self._resizeable = False
390 return
391 resize_constraint = parted_fs.get_resize_constraint()
392 min_size = resize_constraint.min_size
393 if int(min_size) != min_size: min_size = int(min_size) + 1
394 self._min_sectors_for_resize = min_size
395 self._resizeable = True
396
397 def is_extended(self):
398 if self._type == "extended":
399 return True
400 else:
401 return False
402
403 def is_logical(self):
404 if self._type == "free":
405 if int(self._minor) + Decimal("0.9") == Decimal(str(self._minor)):
406 return True
407 else:
408 return False
409 elif self._device.archinfo[self._device._arch]['extended'] and self._minor > 4:
410 return True
411 else:
412 return False
413
414 def get_logicals(self):
415 if not self.is_extended():
416 return None
417 logicals = []
418 start = self._start
419 parts = self._device._partitions.keys()
420 parts.sort()
421 for part in parts:
422 if part < 5: continue
423 logicals.append(part)
424 logicals.sort()
425 return logicals
426
427 def get_extended_parent(self):
428 if not self.is_logical():
429 return None
430 else:
431 return self._device.get_partition_at(self._start, ignore_extended=0)
432
433 def set_start(self, start):
434 self._start = int(start)
435 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
436
437 def get_start(self):
438 return int(self._start)
439
440 def set_end(self, end):
441 self._end = int(end)
442 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
443
444 def get_end(self):
445 return int(self._end)
446
447 def get_mb(self):
448 return int(self._mb)
449
450 def set_mb(self, mb):
451 self._mb = int(mb)
452
453 def set_type(self, type):
454 self._type = type
455
456 def get_type(self):
457 return self._type
458
459 def get_device(self):
460 return self._device
461
462 def set_minor(self, minor):
463 self._minor = float(minor)
464
465 def get_minor(self):
466 return float(self._minor)
467
468 def set_orig_minor(self, orig_minor):
469 self._orig_minor = int(orig_minor)
470
471 def get_orig_minor(self):
472 return self._orig_minor
473
474 def set_mountpoint(self, mountpoint):
475 self._mountpoint = mountpoint
476
477 def get_mountpoint(self):
478 return self._mountpoint
479
480 def set_mountopts(self, mountopts):
481 self._mountopts = mountopts
482
483 def get_mountopts(self):
484 return self._mountopts
485
486 def set_format(self, format):
487 self._format = format
488
489 def get_format(self):
490 return self._format
491
492 def get_blocks(self):
493 return int(self._blocks)
494
495 def get_min_sectors_for_resize(self):
496 if self.is_extended():
497 min_size = self._start
498 for part in self._device._partitions:
499 if part < 5: continue
500 if part.get_end > min_size: min_size = part.get_end()
501 return min_size
502 else:
503 return self._min_sectors_for_resize
504
505 def get_max_sectors_for_resize(self):
506 free_start, free_end = self._device.get_free_space(self._end)
507 if free_end == -1: return self._end
508 if free_start - 1 == self._end:
509 if self.is_logical():
510 if free_end <= self._device._partitions[self.get_extended_parent()]._end:
511 return free_end - self._start
512 else:
513 return self._end - self._start
514 else:
515 return free_end - self._start
516
517 def resize(self, start, end):
518 part_at_start = self._device.get_partition_at(int(start))
519 part_at_end = self._device.get_partition_at(int(end))
520 logicals = None
521 if self.is_logical():
522 parent = self.get_extended_parent()
523 parentstart = int(self._device._partitions[parent].get_start())
524 parentend = int(self._device._partitions[parent].get_end())
525 if (start < parentstart) or (end > parentend): return 0
526 if self.is_extended():
527 logicals = self.get_logicals()
528 if len(logicals):
529 logicals_start = self._device._partitions[logicals[0]].get_start()
530 logicals_end = self._device._partitions[logicals[len(logicals)-1]].get_end()
531 if (start > logicals_start) or (end < logicals_end): return 0
532 if part_at_start in logicals: part_at_start = 0
533 if part_at_end in logicals: part_at_end = 0
534 if ((not part_at_start == 0) and (part_at_start != self._minor)) or ((not part_at_end == 0) and (part_at_end != self._minor)):
535 return 0
536 self.set_start(start)
537 self.set_end(end)
538 return 1
539
540 def _error(self, message):
541 "Raises an exception"
542 raise "PartitionObjectError", message
543
544
545 def detect_devices():
546 "Returns a list of partitionable devices on the system"
547
548 devices = []
549
550 # Make sure sysfs exists
551 if not os.path.exists("/sys/bus"):
552 raise Exception, "no sysfs found (you MUST use a kernel >2.6)"
553 # Make sure /proc/partitions exists
554 if not os.path.exists("/proc/partitions"):
555 raise Exception, "/proc/partitions does not exist!"
556
557 # Load /proc/partitions into the variable 'partitions'
558 partitions = []
559 for line in open("/proc/partitions"):
560 if len(line.split()) < 4 or not line.split()[0].isdigit() or \
561 not line.split()[1].isdigit():
562 continue
563
564 # Get the major, minor and device name
565 major = line.split()[0]
566 minor = line.split()[1]
567 device = "/dev/" + line.split()[3]
568
569 if not major.isdigit() or not minor.isdigit():
570 continue
571
572 major = int(major)
573 minor = int(minor)
574
575 # If there is no /dev/'device_name', then scan
576 # all the devices in /dev to try and find a
577 # devices with the same major and minor
578 if not os.path.exists(device):
579 device = None
580 for path, dirs, files in os.walk("/dev"):
581 for file in files:
582 full_file = os.path.join(path, file)
583 if not os.path.exists(full_file):
584 continue
585 statres = os.stat(full_file)
586 fmaj = os.major(statres.st_rdev)
587 fmin = os.minor(statres.st_rdev)
588 if fmaj == major and fmin == minor:
589 device = full_file
590 break
591 if not device:
592 continue
593
594 partitions.append(( major, minor, device ))
595
596 # Scan sysfs for the devices of type 'x'
597 # 'x' being a member of the list below:
598 for dev_type in [ "ide", "scsi" ]: # Other device types? usb? fw?
599 if os.path.exists("/sys/bus/" + dev_type):
600 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
601
602 # For each device in the devices on that bus
603 for sysfs_device in sysfs_devices:
604 dev_file = "/sys/bus/" + dev_type + "/devices/"\
605 + sysfs_device + "/block/dev"
606
607 # If the file is not a block device, loop
608 if not os.path.exists(dev_file):
609 continue
610
611 # Get the major and minor info
612 try:
613 major, minor = open(dev_file).read().split(":")
614 major = int(major)
615 minor = int(minor)
616 except:
617 raise Exception, "invalid major minor in "\
618 + dev_file
619
620 # Find a device listed in /proc/partitions
621 # that has the same minor and major as our
622 # current block device.
623 for record in partitions:
624 if major == record[0] and minor == record[1]:
625 devices.append(record[2])
626
627 # We have assembled the list of devices, so return it
628 return devices

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20