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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 265 - (show annotations) (download) (as text)
Wed Jan 19 03:00:53 2005 UTC (15 years, 9 months ago) by agaffney
File MIME type: text/x-python
File size: 18989 byte(s)
partition data fix

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20