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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 495 - (show annotations) (download) (as text)
Sun Apr 10 07:11:59 2005 UTC (15 years, 6 months ago) by agaffney
File MIME type: text/x-python
File size: 21540 byte(s)
tweaks to tidy code

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20