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

Diff of /trunk/src/GLIStorageDevice.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 529 Revision 596
1import commands, string, re, os, parted 1import commands, string, os, parted
2from decimal import Decimal 2from GLIException import *
3import GLIUtility
3 4
4MEGABYTE = 1024 * 1024 5MEGABYTE = 1024 * 1024
5 6
6archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False }, 7archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
7 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False }, 8 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
8 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True }, 9 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
10 'amd64': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
9 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False } 11 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
10 } 12 }
11 13
12## 14##
13# This class provides a partitioning abstraction for the frontends 15# This class provides a partitioning abstraction for the frontends
24 _parted_dev = None 26 _parted_dev = None
25 _parted_disk = None 27 _parted_disk = None
26 _sector_bytes = 0 28 _sector_bytes = 0
27 _total_mb = 0 29 _total_mb = 0
28 _arch = None 30 _arch = None
31 _disklabel = None
29 32
30 ## 33 ##
31 # Initialization function for GLIStorageDevice class 34 # Initialization function for GLIStorageDevice class
32 # @param device Device node (e.g. /dev/hda) of device being represented 35 # @param device Device node (e.g. /dev/hda) of device being represented
33 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now) 36 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
34 def __init__(self, device, arch="x86"): 37 def __init__(self, device, arch="x86", set_geometry=True):
35 self._device = device 38 self._device = device
36 self._partitions = {} 39 self._partitions = {}
37 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 40 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
38 self._total_bytes = 0 41 self._total_bytes = 0
39 self._cylinder_bytes = 0 42 self._cylinder_bytes = 0
40 self._arch = arch 43 self._arch = arch
41 self._parted_dev = parted.PedDevice.get(self._device) 44 self._parted_dev = parted.PedDevice.get(self._device)
45 try:
42 self._parted_disk = parted.PedDisk.new(self._parted_dev) 46 self._parted_disk = parted.PedDisk.new(self._parted_dev)
47 except:
48 self._parted_disk = self._parted_dev.disk_new_fresh(parted.disk_type_get(archinfo[self._arch]['disklabel']))
49 self._disklabel = self._parted_disk.type.name
50 if set_geometry:
43 self.set_disk_geometry_from_disk() 51 self.set_disk_geometry_from_disk()
44 52
45 ## 53 ##
46 # Sets disk geometry info from disk. This function is used internally by __init__() 54 # Sets disk geometry info from disk. This function is used internally by __init__()
47 def set_disk_geometry_from_disk(self): 55 def set_disk_geometry_from_disk(self):
48 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 56 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size
71 last_part = parted_part.num 79 last_part = parted_part.num
72 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) 80 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)
73 elif parted_part.type_name == "free": 81 elif parted_part.type_name == "free":
74 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0) 82 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
75 if parent_part: 83 if parent_part:
76 self._partitions[Decimal(str(float(last_log_part+0.9)))] = Partition(self, Decimal(str(float(last_log_part+0.9))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False) 84 self._partitions[last_log_part+0.9] = Partition(self, last_log_part+0.9, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
77 last_log_part += 1 85 last_log_part += 1
78 else: 86 else:
79 self._partitions[Decimal(str(float(last_part+0.1)))] = Partition(self, Decimal(str(float(last_part+0.1))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False) 87 self._partitions[last_part+0.1] = Partition(self, last_part+0.1, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
80 last_part += 1 88 last_part += 1
81 parted_part = self._parted_disk.next_partition(parted_part) 89 parted_part = self._parted_disk.next_partition(parted_part)
82 90
83 ## 91 ##
84 # Imports partition info from the install profile partition structure (currently does nothing) 92 # Imports partition info from the install profile partition structure (currently does nothing)
85 # @param ips Parameter structure returned from install_profile.get_partition_tables() 93 # @param ips Parameter structure returned from install_profile.get_partition_tables()
86 def set_partitions_from_install_profile_structure(self, ips): 94 def set_partitions_from_install_profile_structure(self, ips):
87 pass
88# for part in ips: 95 for part in ips:
89# tmppart = ips[part] 96 tmppart = ips[part]
90# existing = False 97 existing = False
91# parted_part = self._parted_disk.get_partition(part) 98 if tmppart['origminor'] and not tmppart['format']:
92# if parted_part != None:
93# start = parted_part.geom.start / self._sectors_in_cylinder
94# end = parted_part.geom.end / self._sectors_in_cylinder
95# fs_type = ""
96# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
97# if parted_part.type == 2: fs_type = "extended"
98# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
99# existing = True 99 existing = True
100# 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'])) 100 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=['mountopts'])
101 101
102 ## 102 ##
103 # Returns name of device (e.g. /dev/hda) being represented 103 # Returns name of device (e.g. /dev/hda) being represented
104 def get_device(self): 104 def get_device(self):
105 return self._device 105 return self._device
116 for part in parts: 116 for part in parts:
117 if archinfo[self._arch]['extended'] and part > 4: break 117 if archinfo[self._arch]['extended'] and part > 4: break
118 tmppart = self._partitions[part] 118 tmppart = self._partitions[part]
119 if tmppart.get_type() == "extended": 119 if tmppart.get_type() == "extended":
120 for part_log in parts: 120 for part_log in parts:
121 if part_log < Decimal("4.9"): continue 121 if part_log < 4.9: continue
122 tmppart_log = self._partitions[part_log] 122 tmppart_log = self._partitions[part_log]
123 if tmppart_log.get_type() == "free": 123 if tmppart_log.get_type() == "free":
124 if last_log_minor < last_log_free: 124 if last_log_minor < last_log_free:
125 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb()) 125 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
126 del self._partitions[part_log] 126 del self._partitions[part_log]
127 else: 127 else:
128 if not last_log_free: 128 if not last_log_free:
129 last_log_free = Decimal(str(last_log_minor + 0.9)) 129 last_log_free = last_log_minor + 0.9
130 else:
131 lost_log_free = part_log
130 tmppart_log.set_minor(last_log_free) 132 tmppart_log.set_minor(last_log_free)
131 self._partitions[last_log_free] = tmppart_log 133 self._partitions[last_log_free] = tmppart_log
132 if part_log != last_log_free: del self._partitions[part_log] 134 if part_log != last_log_free: del self._partitions[part_log]
133 continue 135 continue
134 last_log_free = part_log
135 else: 136 else:
136 if part_log > (last_log_minor + 1): 137 if part_log > (last_log_minor + 1):
137 tmppart_log.set_minor(last_log_minor + 1) 138 tmppart_log.set_minor(last_log_minor + 1)
138 last_log_minor = last_log_minor + 1 139 last_log_minor = last_log_minor + 1
139 self._partitions[last_log_minor] = tmppart_log 140 self._partitions[last_log_minor] = tmppart_log
140 del self._partitions[part_log] 141 del self._partitions[part_log]
141 continue 142 continue
143 else:
142 last_log_minor = part_log 144 last_log_minor = part_log
143 if tmppart.get_type() == "free": 145 if tmppart.get_type() == "free":
144 if last_minor < last_free: 146 if last_minor < last_free:
145 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb()) 147 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
146 del self._partitions[part] 148 del self._partitions[part]
147 else: 149 else:
148 if not last_free: 150 if not last_free:
149 last_free = Decimal(str(last_minor + 0.1)) 151 last_free = last_minor + 0.1
152 else:
153 last_free = part
150 tmppart.set_minor(last_free) 154 tmppart.set_minor(last_free)
151 self._partitions[last_free] = tmppart 155 self._partitions[last_free] = tmppart
152 if part != last_free: del self._partitions[part] 156 if part != last_free: del self._partitions[part]
153 continue 157 continue
154 last_free = part
155 else: 158 else:
156 if part > (last_minor + 1): 159 if part > (last_minor + 1):
157 tmppart.set_minor(last_minor + 1) 160 tmppart.set_minor(last_minor + 1)
158 last_minor = last_minor + 1 161 last_minor = last_minor + 1
159 self._partitions[last_minor] = tmppart 162 self._partitions[last_minor] = tmppart
160 del self._partitions[part] 163 del self._partitions[part]
161 continue 164 continue
165 else:
162 last_minor = part 166 last_minor = part
163 167
164 ## 168 ##
165 # Adds a new partition to the partition info 169 # Adds a new partition to the partition info
166 # @param free_minor minor of unallocated space partition is being created in 170 # @param free_minor minor of unallocated space partition is being created in
167 # @param mb size of partition in MB 171 # @param mb size of partition in MB
169 # @param end End sector (only used for existing partitions) 173 # @param end End sector (only used for existing partitions)
170 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.) 174 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
171 # @param mountpoint='' Partition mountpoint 175 # @param mountpoint='' Partition mountpoint
172 # @param mountopts='' Partition mount options 176 # @param mountopts='' Partition mount options
173 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''): 177 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
174 free_minor = Decimal(str(free_minor)) 178 free_minor = free_minor
175 new_minor = int(free_minor) + 1 179 new_minor = int(free_minor) + 1
176# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor) 180# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
177 if self._partitions.has_key(new_minor): 181 if self._partitions.has_key(new_minor):
178 parts = self._partitions.keys() 182 parts = self._partitions.keys()
179 parts.sort() 183 parts.sort()
194 if i == new_minor: stopscooting = 1 198 if i == new_minor: stopscooting = 1
195 if mb != self._partitions[free_minor].get_mb(): 199 if mb != self._partitions[free_minor].get_mb():
196 old_free_mb = self._partitions[free_minor].get_mb() 200 old_free_mb = self._partitions[free_minor].get_mb()
197 del self._partitions[free_minor] 201 del self._partitions[free_minor]
198 if archinfo[self._arch]['extended'] and new_minor >= 5: 202 if archinfo[self._arch]['extended'] and new_minor >= 5:
199 free_minor = Decimal(str(new_minor + 0.9)) 203 free_minor = new_minor + 0.9
200 else: 204 else:
201 free_minor = Decimal(str(new_minor + 0.1)) 205 free_minor = new_minor + 0.1
202 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free") 206 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
203# print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor) 207# print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
204 else: 208 else:
205 del self._partitions[free_minor] 209 del self._partitions[free_minor]
206 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts) 210 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
207 if type == "extended": 211 if type == "extended":
208 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free") 212 self._partitions[4.9] = Partition(self, 4.9, mb, 0, 0, "free")
209 self.tidy_partitions() 213 self.tidy_partitions()
210 214
211 ## 215 ##
212 # Removes partition from partition info 216 # Removes partition from partition info
213 # @param minor Minor of partition to remove 217 # @param minor Minor of partition to remove
214 def remove_partition(self, minor): 218 def remove_partition(self, minor):
215 tmppart = self._partitions[int(minor)] 219 tmppart = self._partitions[int(minor)]
216 free_minor = 0 220 free_minor = 0
217 if tmppart.is_logical(): 221 if tmppart.is_logical():
218 free_minor = Decimal(str(float(minor)-0.1)) 222 free_minor = minor-0.1
219 else: 223 else:
220 free_minor = Decimal(str(float(minor)-0.9)) 224 free_minor = minor-0.9
221 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False) 225 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
222 del self._partitions[int(minor)] 226 del self._partitions[int(minor)]
223 self.tidy_partitions() 227 self.tidy_partitions()
224 228
225 ## 229 ##
313 def get_ordered_partition_list(self): 317 def get_ordered_partition_list(self):
314 parts = self._partitions.keys() 318 parts = self._partitions.keys()
315 parts.sort() 319 parts.sort()
316 partlist = [] 320 partlist = []
317 tmppart = None 321 tmppart = None
318 tmppart_log = None
319 for part in parts: 322 for part in parts:
320 if archinfo[self._arch]['extended'] and part > Decimal("4.1"): break 323 if archinfo[self._arch]['extended'] and part > 4.1: break
321 tmppart = self._partitions[part] 324 tmppart = self._partitions[part]
322 partlist.append(part) 325 partlist.append(part)
323 if tmppart.is_extended(): 326 if tmppart.is_extended():
324 for part_log in parts: 327 for part_log in parts:
325 if part_log < Decimal("4.9"): continue 328 if part_log < 4.9: continue
326 tmppart_log = self._partitions[part_log]
327 partlist.append(part_log) 329 partlist.append(part_log)
328 return partlist 330 return partlist
329 331
330 ## 332 ##
331 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables() 333 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
344 if tmppart.is_extended(): 346 if tmppart.is_extended():
345 return part 347 return part
346 return 0 348 return 0
347 349
348 ## 350 ##
351 # Sets the disklabel type
352 def set_disklabel(self, disklabel):
353 self._disklabel = disklabel
354
355 ##
356 # Returns the disklabel type
357 def get_disklabel(self):
358 return self._disklabel
359
360 ##
349 # Returns the number of sectors on the device 361 # Returns the number of sectors on the device
350 def get_num_sectors(self): 362 def get_num_sectors(self):
351 return int(self._total_sectors) 363 return int(self._total_sectors)
352 364
353 ## 365 ##
383 ## 395 ##
384 # Prints disk geometry to STDOUT (no longer used) 396 # Prints disk geometry to STDOUT (no longer used)
385 def print_geometry(self): 397 def print_geometry(self):
386 print self._total_bytes, self._geometry 398 print self._total_bytes, self._geometry
387 399
388 ##
389 # Utility function for raising an exception
390 # @param message Error message
391 def _error(self, message):
392 "Raises an exception"
393 raise "DeviceObjectError", message
394
395 ## 400 ##
396 # Utility function for running a command and returning it's output as a list 401 # Utility function for running a command and returning it's output as a list
397 # @param cmd Command to run 402 # @param cmd Command to run
398 def _run(self, cmd): 403 def _run(self, cmd):
399 "Runs a command and returns the output" 404 "Runs a command and returns the output"
446 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.) 451 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
447 # @param mountpoint='' Mountpoint of partition 452 # @param mountpoint='' Mountpoint of partition
448 # @param mountopts='' Mount options of partition 453 # @param mountopts='' Mount options of partition
449 # @param format=True Format partition 454 # @param format=True Format partition
450 # @param existing=False This partition exists on disk 455 # @param existing=False This partition exists on disk
451 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False): 456 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False, origminor=0):
452 self._device = device 457 self._device = device
453 self._minor = float(minor) 458 self._minor = float(minor)
454 if existing: self._orig_minor = int(minor)
455 self._start = int(start) 459 self._start = int(start)
456 self._end = int(end) 460 self._end = int(end)
457 self._type = type or "unknown" 461 self._type = type or "unknown"
458 self._mountpoint = mountpoint 462 self._mountpoint = mountpoint
459 self._mountopts = mountopts 463 self._mountopts = mountopts
460 self._format = format 464 self._format = format
461 self._mb = mb 465 self._mb = mb
466 self._orig_minor = origminor
467 if type != "free":
468 if existing and not origminor:
469 self._orig_minor = self._minor
470 self._minor = int(self._minor)
471 self._orig_minor = int(self._orig_minor)
462 if existing: 472 if existing:
463 self._orig_minor = self._minor
464 parted_part = device._parted_disk.get_partition(minor) 473 parted_part = device._parted_disk.get_partition(self._orig_minor)
465 label_type = device._parted_disk.type.name 474 label_type = device._parted_disk.type.name
466 if label_type == "loop": 475 if label_type == "loop":
467 dev_node = device._device 476 dev_node = device._device
468 else: 477 else:
469 dev_node = device._device + str(minor) 478 dev_node = device._device + str(self._orig_minor)
479 print "dev_node = " + dev_node
470 if type == "ntfs": 480 if type == "ntfs":
471 min_bytes = int(commands.getoutput("ntfsresize -f --info " + dev_node + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'")) 481 min_bytes = int(commands.getoutput("ntfsresize -f --info " + dev_node + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
472 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1 482 self._min_mb_for_resize = int(min_bytes / MEGABYTE) + 1
473 self._resizeable = True 483 self._resizeable = True
474 elif type == "ext2" or type == "ext3": 484 elif type == "ext2" or type == "ext3":
475 block_size = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")) 485 block_size = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")))
476 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")) 486 free_blocks = int(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
477 free_bytes = int(block_size * free_blocks) 487 free_bytes = int(block_size * free_blocks)
478 # can't hurt to pad (the +50) it a bit since this is really just a guess 488 # can't hurt to pad (the +50) it a bit since this is really just a guess
479 self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50 489 self._min_mb_for_resize = self._mb - int(free_bytes / MEGABYTE) + 50
480 self._resizeable = True 490 self._resizeable = True
481 else: 491 else:
482 parted_part = self._device._parted_disk.get_partition(int(self._minor)) 492 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
483 try: 493 try:
484 parted_fs = parted_part.geom.file_system_open() 494 parted_fs = parted_part.geom.file_system_open()
485 except: 495 except:
486 self._resizeable = False 496 self._resizeable = False
487 return 497 return
500 510
501 ## 511 ##
502 # Returns whether or not the partition is logical 512 # Returns whether or not the partition is logical
503 def is_logical(self): 513 def is_logical(self):
504 if self._type == "free": 514 if self._type == "free":
505 if int(self._minor) + Decimal("0.9") == Decimal(str(self._minor)): 515 if int(self._minor) + 0.9 == self._minor:
506 return True 516 return True
507 else: 517 else:
508 return False 518 return False
509 elif archinfo[self._device._arch]['extended'] and self._minor > 4: 519 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
510 return True 520 return True
515 # Returns a list of logical partitions if this is an extended partition 525 # Returns a list of logical partitions if this is an extended partition
516 def get_logicals(self): 526 def get_logicals(self):
517 if not self.is_extended(): 527 if not self.is_extended():
518 return None 528 return None
519 logicals = [] 529 logicals = []
520 start = self._start
521 parts = self._device._partitions.keys() 530 parts = self._device._partitions.keys()
522 parts.sort() 531 parts.sort()
523 for part in parts: 532 for part in parts:
524 if part < 5: continue 533 if part < 5: continue
525 logicals.append(part) 534 logicals.append(part)
659 return -1 668 return -1
660 669
661 ## 670 ##
662 # Returns maximum MB for resize 671 # Returns maximum MB for resize
663 def get_max_mb_for_resize(self): 672 def get_max_mb_for_resize(self):
673 if self._resizeable:
664 free_minor = 0 674 free_minor = 0
675 if self.is_logical():
676 free_minor = self._minor + 0.9
677 else:
678 free_minor = self._minor + 0.1
679 if free_minor in self._device._partitions:
680 return self._mb + self._device._partitions[free_minor]._mb
681 else:
682 return self._mb
683 else:
684 return -1
685
686 ##
687 # Resizes the partition
688 # @param mb New size in MB
689 def resize(self, mb):
690 free_minor = self._minor
665 if self.is_logical(): 691 if self.is_logical():
666 free_minor = Decimal(str(self._minor + 0.9)) 692 free_minor += 0.9
667 else: 693 else:
668 free_minor = Decimal(str(self._minor + 0.1)) 694 free_minor += 0.1
695 if mb < self._mb:
696 # Shrinking
669 if free_minor in self._device._partitions: 697 if not free_minor in self._device._partitions:
698 self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
699 self._device._partitions[free_minor]._mb += self._mb - mb
700 self._mb = mb
670 return self._mb + self._device._partitions[free_minor]._mb 701 elif mb == self._mb + self._device._partitions[free_minor]._mb:
671 else: 702 # Using all available unallocated space
672 return self._mb 703 del self._device._partitions[free_minor]
673 704 self._mb = mb
674 ## 705 elif mb > self._mb:
675 # Resizes the partition (ignore) 706 # Growing
676 # @param start New start sector 707 self._device._partitions[free_minor]._mb = mb - self._mb
677 # @param end New end sector 708 self._mb = mb
678 def resize(self, start, end):
679 part_at_start = self._device.get_partition_at(int(start))
680 part_at_end = self._device.get_partition_at(int(end))
681 logicals = None
682 if self.is_logical():
683 parent = self.get_extended_parent()
684 parentstart = int(self._device._partitions[parent].get_start())
685 parentend = int(self._device._partitions[parent].get_end())
686 if (start < parentstart) or (end > parentend): return 0
687 if self.is_extended():
688 logicals = self.get_logicals()
689 if len(logicals):
690 logicals_start = self._device._partitions[logicals[0]].get_start()
691 logicals_end = self._device._partitions[logicals[len(logicals)-1]].get_end()
692 if (start > logicals_start) or (end < logicals_end): return 0
693 if part_at_start in logicals: part_at_start = 0
694 if part_at_end in logicals: part_at_end = 0
695 if ((not part_at_start == 0) and (part_at_start != self._minor)) or ((not part_at_end == 0) and (part_at_end != self._minor)):
696 return 0
697 self.set_start(start)
698 self.set_end(end)
699 return 1
700
701 ##
702 # Utility function to raise an exception
703 # @param message Error message
704 def _error(self, message):
705 raise "PartitionObjectError", message
706 709
707## 710##
708# Returns a list of detected partitionable devices 711# Returns a list of detected partitionable devices
709def detect_devices(): 712def detect_devices():
710 devices = [] 713 devices = []
711 714
712 # Make sure sysfs exists 715 # Make sure sysfs exists
713 if not os.path.exists("/sys/bus"): 716 if not os.path.exists("/sys/bus"):
714 raise Exception, "no sysfs found (you MUST use a kernel >2.6)" 717 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
715 # Make sure /proc/partitions exists 718 # Make sure /proc/partitions exists
716 if not os.path.exists("/proc/partitions"): 719 if not os.path.exists("/proc/partitions"):
717 raise Exception, "/proc/partitions does not exist!" 720 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
718 721
719 # Load /proc/partitions into the variable 'partitions' 722 # Load /proc/partitions into the variable 'partitions'
720 partitions = [] 723 partitions = []
721 for line in open("/proc/partitions"): 724 for line in open("/proc/partitions"):
722 if len(line.split()) < 4 or not line.split()[0].isdigit() or \ 725 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
723 not line.split()[1].isdigit():
724 continue 726 continue
725 727
726 # Get the major, minor and device name 728 # Get the major, minor and device name
727 major = line.split()[0] 729 major = line.split()[0]
728 minor = line.split()[1] 730 minor = line.split()[1]
738 # all the devices in /dev to try and find a 740 # all the devices in /dev to try and find a
739 # devices with the same major and minor 741 # devices with the same major and minor
740 if not os.path.exists(device): 742 if not os.path.exists(device):
741 device = None 743 device = None
742 for path, dirs, files in os.walk("/dev"): 744 for path, dirs, files in os.walk("/dev"):
743 for file in files: 745 for d_file in files:
744 full_file = os.path.join(path, file) 746 full_file = os.path.join(path, d_file)
745 if not os.path.exists(full_file): 747 if not os.path.exists(full_file):
746 continue 748 continue
747 statres = os.stat(full_file) 749 statres = os.stat(full_file)
748 fmaj = os.major(statres.st_rdev) 750 fmaj = os.major(statres.st_rdev)
749 fmin = os.minor(statres.st_rdev) 751 fmin = os.minor(statres.st_rdev)
761 if os.path.exists("/sys/bus/" + dev_type): 763 if os.path.exists("/sys/bus/" + dev_type):
762 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices") 764 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
763 765
764 # For each device in the devices on that bus 766 # For each device in the devices on that bus
765 for sysfs_device in sysfs_devices: 767 for sysfs_device in sysfs_devices:
766 dev_file = "/sys/bus/" + dev_type + "/devices/"\ 768 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
767 + sysfs_device + "/block/dev"
768 769
769 # If the file is not a block device, loop 770 # If the file is not a block device, loop
770 if not os.path.exists(dev_file): 771 if not os.path.exists(dev_file):
771 continue 772 continue
772 773
774 try: 775 try:
775 major, minor = open(dev_file).read().split(":") 776 major, minor = open(dev_file).read().split(":")
776 major = int(major) 777 major = int(major)
777 minor = int(minor) 778 minor = int(minor)
778 except: 779 except:
779 raise Exception, "invalid major minor in "\ 780 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
780 + dev_file
781 781
782 # Find a device listed in /proc/partitions 782 # Find a device listed in /proc/partitions
783 # that has the same minor and major as our 783 # that has the same minor and major as our
784 # current block device. 784 # current block device.
785 for record in partitions: 785 for record in partitions:
786 if major == record[0] and minor == record[1]: 786 if major == record[0] and minor == record[1]:
787 devices.append(record[2]) 787 devices.append(record[2])
788 788
789 # For testing the partitioning code
790 if GLIUtility.is_file("/tmp/disk.img"):
791 devices.append("/tmp/disk.img")
792
789 # We have assembled the list of devices, so return it 793 # We have assembled the list of devices, so return it
790 return devices 794 return devices

Legend:
Removed from v.529  
changed lines
  Added in v.596

  ViewVC Help
Powered by ViewVC 1.1.20