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

Contents of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (hide annotations) (download) (as text)
Sun Nov 28 02:46:53 2004 UTC (15 years, 11 months ago) by agaffney
File MIME type: text/x-python
File size: 19514 byte(s)
changed the way GLIStorageDevice.py gets ext2/3 free space

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

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20