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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 826 - (show annotations) (download) (as text)
Sat Aug 20 16:14:46 2005 UTC (15 years, 2 months ago) by agaffney
File MIME type: text/x-python
File size: 23389 byte(s)
global try/except block around fs resizable detection code

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20