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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 731 - (show annotations) (download) (as text)
Wed Jul 6 04:51:47 2005 UTC (15 years, 3 months ago) by agaffney
File MIME type: text/x-python
File size: 23380 byte(s)
Added stage3 generation 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 parted_part = device._parted_disk.get_partition(self._orig_minor)
373 label_type = device._parted_disk.type.name
374 if label_type == "loop":
375 dev_node = device._device
376 else:
377 dev_node = device._device + str(self._orig_minor)
378 # print "dev_node = " + dev_node
379 if type == "ntfs":
380 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 .\+//'"))
381 self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
382 self._resizeable = True
383 elif type == "ext2" or type == "ext3":
384 block_size = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")))
385 free_blocks = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
386 free_bytes = long(block_size * free_blocks)
387 # can't hurt to pad (the +50) it a bit since this is really just a guess
388 self._min_mb_for_resize = self._mb - long(free_bytes / MEGABYTE) + 50
389 self._resizeable = True
390 else:
391 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
392 try:
393 parted_fs = parted_part.geom.file_system_open()
394 except:
395 self._resizeable = False
396 return
397 resize_constraint = parted_fs.get_resize_constraint()
398 min_bytes = resize_constraint.min_size * self._device._sector_bytes
399 self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
400 self._resizeable = True
401
402 ##
403 # Returns whether or not the partition is extended
404 def is_extended(self):
405 if self._type == "extended":
406 return True
407 else:
408 return False
409
410 ##
411 # Returns whether or not the partition is logical
412 def is_logical(self):
413 if self._type == "free":
414 if int(self._minor) + FREE_MINOR_FRAC_LOG == self._minor:
415 return True
416 else:
417 return False
418 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
419 return True
420 else:
421 return False
422
423 ##
424 # Returns a list of logical partitions if this is an extended partition
425 def get_logicals(self):
426 if not self.is_extended():
427 return None
428 logicals = []
429 parts = self._device._partitions.keys()
430 parts.sort()
431 for part in parts:
432 if part < 5: continue
433 logicals.append(part)
434 logicals.sort()
435 return logicals
436
437 ##
438 # Returns the extened parent partition if this is a logical partition (no longer used)
439 def get_extended_parent(self):
440 if not self.is_logical():
441 return None
442 else:
443 return self._device.get_partition_at(self._start, ignore_extended=0)
444
445 ##
446 # Sets the options passed to mkfs
447 # @param mkfsopts Options passed to mkfs
448 def set_mkfsopts(self, mkfsopts):
449 self._mkfsopts = mkfsopts
450
451 ##
452 # Returns the options passes to mkfs
453 def get_mkfsopts(self):
454 return self._mkfsopts
455
456 ##
457 # Sets the start sector for the partition
458 # @param start Start sector
459 def set_start(self, start):
460 self._start = long(start)
461
462 ##
463 # Returns the start sector for the partition
464 def get_start(self):
465 return long(self._start)
466
467 ##
468 # Sets the end sector of the partition
469 # @param end End sector
470 def set_end(self, end):
471 self._end = long(end)
472
473 ##
474 # Returns end sector for the partition
475 def get_end(self):
476 return long(self._end)
477
478 ##
479 # Returns size of partition in MB
480 def get_mb(self):
481 return long(self._mb)
482
483 ##
484 # Sets size of partition in MB
485 # @param mb Parameter description
486 def set_mb(self, mb):
487 self._mb = long(mb)
488
489 ##
490 # Sets type of partition
491 # @param type Parameter description
492 def set_type(self, type):
493 self._type = type
494
495 ##
496 # Returns type of partition
497 def get_type(self):
498 return self._type
499
500 ##
501 # Returns parent GLIStorageDevice object
502 def get_device(self):
503 return self._device
504
505 ##
506 # Sets minor of partition
507 # @param minor New minor
508 def set_minor(self, minor):
509 self._minor = float(minor)
510
511 ##
512 # Returns minor of partition
513 def get_minor(self):
514 return float(self._minor)
515
516 ##
517 # Sets the original minor of the partition
518 # @param orig_minor Parameter description
519 def set_orig_minor(self, orig_minor):
520 self._orig_minor = int(orig_minor)
521
522 ##
523 # Returns the original minor of the partition
524 def get_orig_minor(self):
525 return self._orig_minor
526
527 ##
528 # Sets the mountpoint for the partition
529 # @param mountpoint Mountpoint
530 def set_mountpoint(self, mountpoint):
531 self._mountpoint = mountpoint
532
533 ##
534 # Returns the mountpoint for the partition
535 def get_mountpoint(self):
536 return self._mountpoint
537
538 ##
539 # Sets the mount options for the partition
540 # @param mountopts Mount options
541 def set_mountopts(self, mountopts):
542 self._mountopts = mountopts
543
544 ##
545 # Returns the mount options for the partition
546 def get_mountopts(self):
547 return self._mountopts
548
549 ##
550 # Set whether to format the partition
551 # @param format Format this partition (True/False)
552 def set_format(self, format):
553 self._format = format
554
555 ##
556 # Returns whether to format the partition
557 def get_format(self):
558 return self._format
559
560 ##
561 # Returns whether the partition is resizeable
562 def is_resizeable(self):
563 return self._resizeable
564
565 ##
566 # Returns minimum MB for resize
567 def get_min_mb_for_resize(self):
568 # if self.is_extended():
569 # min_size = self._start
570 # for part in self._device._partitions:
571 # if part < 5: continue
572 # if part.get_end > min_size: min_size = part.get_end()
573 # return min_size
574 # else:
575 if self._resizeable:
576 return self._min_mb_for_resize
577 else:
578 return -1
579
580 ##
581 # Returns maximum MB for resize
582 def get_max_mb_for_resize(self):
583 if self._resizeable:
584 free_minor = 0
585 if self.is_logical():
586 free_minor = self._minor + FREE_MINOR_FRAC_LOG
587 else:
588 free_minor = self._minor + FREE_MINOR_FRAC_PRI
589 if free_minor in self._device._partitions:
590 return self._mb + self._device._partitions[free_minor]._mb
591 else:
592 return self._mb
593 else:
594 return -1
595
596 ##
597 # Resizes the partition
598 # @param mb New size in MB
599 def resize(self, mb):
600 free_minor = self._minor
601 if self.is_logical():
602 free_minor += FREE_MINOR_FRAC_LOG
603 else:
604 free_minor += FREE_MINOR_FRAC_PRI
605 if mb < self._mb:
606 # Shrinking
607 if not free_minor in self._device._partitions:
608 self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
609 self._device._partitions[free_minor]._mb += self._mb - mb
610 self._mb = mb
611 elif mb == self._mb + self._device._partitions[free_minor]._mb:
612 # Using all available unallocated space
613 del self._device._partitions[free_minor]
614 self._mb = mb
615 elif mb > self._mb:
616 # Growing
617 self._device._partitions[free_minor]._mb = mb - self._mb
618 self._mb = mb
619
620 ##
621 # Returns a list of detected partitionable devices
622 def detect_devices():
623 devices = []
624
625 # Make sure sysfs exists
626 # TODO: rewrite for 2.4 support
627 if not os.path.exists("/sys/bus"):
628 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
629 # Make sure /proc/partitions exists
630 if not os.path.exists("/proc/partitions"):
631 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
632
633 # Load /proc/partitions into the variable 'partitions'
634 partitions = []
635 for line in open("/proc/partitions"):
636 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
637 continue
638
639 # Get the major, minor and device name
640 major = line.split()[0]
641 minor = line.split()[1]
642 device = "/dev/" + line.split()[3]
643
644 if not major.isdigit() or not minor.isdigit():
645 continue
646
647 major = int(major)
648 minor = int(minor)
649
650 # If there is no /dev/'device_name', then scan
651 # all the devices in /dev to try and find a
652 # devices with the same major and minor
653 if not os.path.exists(device):
654 device = None
655 for path, dirs, files in os.walk("/dev"):
656 for d_file in files:
657 full_file = os.path.join(path, d_file)
658 if not os.path.exists(full_file):
659 continue
660 statres = os.stat(full_file)
661 fmaj = os.major(statres.st_rdev)
662 fmin = os.minor(statres.st_rdev)
663 if fmaj == major and fmin == minor:
664 device = full_file
665 break
666 if not device:
667 continue
668
669 partitions.append(( major, minor, device ))
670
671 # Scan sysfs for the devices of type 'x'
672 # 'x' being a member of the list below:
673 # TODO: rewrite for 2.4 support
674 for dev_type in [ "ide", "scsi" ]: # Other device types? usb? fw?
675 if os.path.exists("/sys/bus/" + dev_type):
676 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
677
678 # For each device in the devices on that bus
679 for sysfs_device in sysfs_devices:
680 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
681
682 # If the file is not a block device, loop
683 if not os.path.exists(dev_file):
684 continue
685
686 # Get the major and minor info
687 try:
688 major, minor = open(dev_file).read().split(":")
689 major = int(major)
690 minor = int(minor)
691 except:
692 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
693
694 # Find a device listed in /proc/partitions
695 # that has the same minor and major as our
696 # current block device.
697 for record in partitions:
698 if major == record[0] and minor == record[1]:
699 devices.append(record[2])
700
701 # For testing the partitioning code
702 if GLIUtility.is_file("/tmp/disk.img"):
703 devices.append("/tmp/disk.img")
704
705 # We have assembled the list of devices, so return it
706 return devices

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20