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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 501 - (hide annotations) (download) (as text)
Tue Apr 12 04:07:39 2005 UTC (15 years, 6 months ago) by agaffney
File MIME type: text/x-python
File size: 21615 byte(s)
Fixed all bugs that stood in
  the way of partitioning from a blank disk.

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20