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

Diff of /trunk/src/GLIStorageDevice.py

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

Revision 386 Revision 514
1import commands, string, re, os, parted 1import commands, string, re, os, parted
2from decimal import Decimal
2 3
4MEGABYTE = 1024 * 1024
5
6archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
7 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
8 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
9 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
10 }
11
12##
13# This class provides a partitioning abstraction for the frontends
3class Device: 14class Device:
4 "Class representing a partitionable device." 15 "Class representing a partitionable device."
5 16
6 _device = None 17 _device = None
7 _partitions = None 18 _partitions = None
11 _cylinder_bytes = 0 22 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 23 _sectors_in_cylinder = 0
13 _parted_dev = None 24 _parted_dev = None
14 _parted_disk = None 25 _parted_disk = None
15 _sector_bytes = 0 26 _sector_bytes = 0
16 27 _total_mb = 0
28 _arch = None
29
30 ##
31 # Initialization function for GLIStorageDevice class
32 # @param self Parameter description
33 # @param device Device node (e.g. /dev/hda) of device being represented
34 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
17 def __init__(self, device): 35 def __init__(self, device, arch="x86"):
18 self._device = device 36 self._device = device
19 self._partitions = {} 37 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 38 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
21 self._total_bytes = 0 39 self._total_bytes = 0
22 self._cylinder_bytes = 0 40 self._cylinder_bytes = 0
41 self._arch = arch
23 self._parted_dev = parted.PedDevice.get(self._device) 42 self._parted_dev = parted.PedDevice.get(self._device)
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 43 self._parted_disk = parted.PedDisk.new(self._parted_dev)
25 self.set_disk_geometry_from_disk() 44 self.set_disk_geometry_from_disk()
26 45
46 ##
47 # Sets disk geometry info from disk. This function is used internally by __init__()
48 # @param self Parameter description
27 def set_disk_geometry_from_disk(self): 49 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 50 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 51 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 52 self._sector_bytes = self._parted_dev.sector_size
41 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes 53 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 54 self._total_sectors = self._parted_dev.length
44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 55 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
56 self._total_mb = int(self._total_bytes / MEGABYTE)
45 57
58 ##
59 # Sets partition info from disk.
60 # @param self Parameter description
46 def set_partitions_from_disk(self): 61 def set_partitions_from_disk(self):
62 last_part = 0
63 last_log_part = 4
47 parted_part = self._parted_disk.next_partition() 64 parted_part = self._parted_disk.next_partition()
48 while parted_part != None: 65 while parted_part:
66 part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
49 if parted_part.num < 1: 67 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 = "" 68 fs_type = ""
67 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 69 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
68 if parted_part.type == 2: fs_type = "extended" 70 if parted_part.type == 2: fs_type = "extended"
71 if archinfo[self._arch]['extended'] and parted_part.num > 4:
72 last_log_part = parted_part.num
73 else:
74 last_part = parted_part.num
75 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)
76 elif parted_part.type_name == "free":
77 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
78 if parent_part:
79 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)
80 last_log_part += 1
81 else:
82 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)
83 last_part += 1
84 parted_part = self._parted_disk.next_partition(parted_part)
85
86 ##
87 # Imports partition info from the install profile partition structure (currently does nothing)
88 # @param self Parameter description
89 # @param ips Parameter structure returned from install_profile.get_partition_tables()
90 def set_partitions_from_install_profile_structure(self, ips):
91 pass
92# for part in ips:
93# tmppart = ips[part]
94# existing = False
95# parted_part = self._parted_disk.get_partition(part)
96# if parted_part != None:
97# start = parted_part.geom.start / self._sectors_in_cylinder
98# end = parted_part.geom.end / self._sectors_in_cylinder
99# fs_type = ""
100# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
101# 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: 102# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
70 existing = True 103# 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'])) 104# 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 105
106 ##
107 # Returns name of device (e.g. /dev/hda) being represented
108 # @param self Parameter description
73 def get_device(self): 109 def get_device(self):
74 return self._device 110 return self._device
75 111
112 ##
113 # Combines free space and closes gaps in minor numbers. This is used internally
114 # @param self Parameter description
76 def clear_partitions(self): 115 def tidy_partitions(self):
77 self._partitions = {} 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 < Decimal("4.9"): 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 = Decimal(str(last_log_minor + 0.9))
136 tmppart_log.set_minor(last_log_free)
137 self._partitions[last_log_free] = tmppart_log
138 if part_log != last_log_free: del self._partitions[part_log]
139 continue
140 last_log_free = 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 continue
148 last_log_minor = part_log
149 if 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 = Decimal(str(last_minor + 0.1))
156 tmppart.set_minor(last_free)
157 self._partitions[last_free] = tmppart
158 if part != last_free: del self._partitions[part]
159 continue
160 last_free = 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 continue
168 last_minor = part
78 169
170 ##
171 # Adds a new partition to the partition info
172 # @param self Parameter description
173 # @param free_minor minor of unallocated space partition is being created in
174 # @param mb size of partition in MB
175 # @param start Start sector (only used for existing partitions)
176 # @param end End sector (only used for existing partitions)
177 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
178 # @param mountpoint='' Partition mountpoint
179 # @param mountopts='' Partition mount options
79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''): 180 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
80 free_start, free_end = self.get_free_space(start) 181 free_minor = Decimal(str(free_minor))
81 minor = int(minor) 182 new_minor = int(free_minor) + 1
82 if not free_end: 183# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
83 return False
84 if self._partitions.has_key(minor): 184 if self._partitions.has_key(new_minor):
85 parts = self._partitions.keys() 185 parts = self._partitions.keys()
86 parts.sort() 186 parts.sort()
87 parts.reverse() 187 parts.reverse()
88 hole_at = 0 188 hole_at = 0
89 for i in range(1, parts[0]+1): 189 for i in range(1, parts[0]+1):
90 if i <= minor: continue 190 if i <= new_minor: continue
91 if not self._partitions.has_key(int(i)): 191 if not self._partitions.has_key(i):
92 hole_at = i 192 hole_at = i
93 break 193 break
94 stopscooting = 0 194 stopscooting = 0
95 for i in parts: 195 for i in parts:
96 if stopscooting: break 196 if stopscooting: break
97 if (i >= hole_at) and (hole_at): continue 197 if i >= hole_at and hole_at: continue
98 if i >= minor: 198 if i >= new_minor:
99 self._partitions[i].set_minor(i+1) 199 self._partitions[i].set_minor(i+1)
100 self._partitions[i+1] = self._partitions[i] 200 self._partitions[i+1] = self._partitions[i]
101 if i == minor: stopscooting = 1 201 if i == new_minor: stopscooting = 1
202 if mb != self._partitions[free_minor].get_mb():
203 old_free_mb = self._partitions[free_minor].get_mb()
204 del self._partitions[free_minor]
205 if archinfo[self._arch]['extended'] and new_minor >= 5:
206 free_minor = Decimal(str(new_minor + 0.9))
207 else:
208 free_minor = Decimal(str(new_minor + 0.1))
209 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
210# print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
211 else:
212 del self._partitions[free_minor]
102 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type, mountpoint=mountpoint, mountopts=mountopts) 213 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
214 if type == "extended":
215 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free")
216 self.tidy_partitions()
103 217
218 ##
219 # Removes partition from partition info
220 # @param self Parameter description
221 # @param minor Minor of partition to remove
104 def remove_partition(self, minor): 222 def remove_partition(self, minor):
223 tmppart = self._partitions[int(minor)]
224 free_minor = 0
225 if tmppart.is_logical():
226 free_minor = Decimal(str(float(minor)-0.1))
227 else:
228 free_minor = Decimal(str(float(minor)-0.9))
229 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
105 del self._partitions[int(minor)] 230 del self._partitions[int(minor)]
231 self.tidy_partitions()
106 232
233 ##
234 # Returns free space (no longer used)
235 # @param self Parameter description
236 # @param start Start sector for search
107 def get_free_space(self, start): 237 def get_free_space(self, start):
108 GAP_SIZE = 100 238 GAP_SIZE = 100
109 parts = self._partitions.keys() 239 parts = self._partitions.keys()
110 parts.sort() 240 parts.sort()
111 lastend_pri = 0 241 lastend_pri = 0
138 if free_start == -1 and lastend_pri < self._total_sectors: 268 if free_start == -1 and lastend_pri < self._total_sectors:
139 free_start = lastend_pri 269 free_start = lastend_pri
140 free_end = self._total_sectors 270 free_end = self._total_sectors
141 return (free_start, free_end) 271 return (free_start, free_end)
142 272
273 ##
274 # Gets partition containing a certain sector (no longer used)
275 # @param self Parameter description
276 # @param sector Sector to look at
277 # @param ignore_extended=1 Ignore extended partitions
143 def get_partition_at(self, sector, ignore_extended=1): 278 def get_partition_at(self, sector, ignore_extended=1):
144 parts = self._partitions.keys() 279 parts = self._partitions.keys()
145 parts.sort() 280 parts.sort()
146 for part in parts: 281 for part in parts:
147 tmppart = self._partitions[part] 282 tmppart = self._partitions[part]
148 if ignore_extended and tmppart.is_extended(): continue 283 if ignore_extended and tmppart.is_extended(): continue
149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()): 284 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
150 return part 285 return part
151 return 0 286 return 0
152 287
288 ##
289 # Returns free minor (no longer used)
290 # @param self Parameter description
291 # @param start Parameter description
292 # @param end Parameter description
153 def get_free_minor_at(self, start, end): 293 def get_free_minor_at(self, start, end):
154 parts = self._partitions.keys() 294 parts = self._partitions.keys()
155 parts.sort() 295 parts.sort()
156 minor = 1 296 minor = 1
157 lastpart = 0 297 lastpart = 0
177 break 317 break
178 minor = part + 1 318 minor = part + 1
179 lastpart = part 319 lastpart = part
180 return minor 320 return minor
181 321
322 ##
323 # Returns an ordered list (disk order) of partition minors
324 # @param self Parameter description
182 def get_ordered_partition_list(self): 325 def get_ordered_partition_list(self):
183 parts = self._partitions.keys() 326 parts = self._partitions.keys()
184 parts.sort() 327 parts.sort()
185 partlist = [] 328 partlist = []
186 free_start, free_end = self.get_free_space(0) 329 tmppart = None
187 tmppart = None
188 tmppart_log = None 330 tmppart_log = None
189 for part in parts: 331 for part in parts:
190 if part > 4: break 332 if archinfo[self._arch]['extended'] and part > Decimal("4.1"): break
191 tmppart = self._partitions[part] 333 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) 334 partlist.append(part)
198 if tmppart.is_extended(): 335 if tmppart.is_extended():
199 for part_log in parts: 336 for part_log in parts:
200 if part_log < 5: continue 337 if part_log < Decimal("4.9"): continue
201 tmppart_log = self._partitions[part_log] 338 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) 339 partlist.append(part_log)
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 340 return partlist
213 341
342 ##
343 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
344 # @param self Parameter description
214 def get_install_profile_structure(self): 345 def get_install_profile_structure(self):
215 devdic = {} 346 devdic = {}
216 for part in self._partitions: 347 for part in self._partitions:
217 tmppart = self._partitions[part] 348 tmppart = self._partitions[part]
218 devdic[part] = { 'mb': 0, 'minor': int(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() } 349 devdic[part] = { 'mb': tmppart.get_mb(), 'minor': float(part), 'origminor': tmppart.get_orig_minor(), '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 350 return devdic
220 351
352 ##
353 # Returns the minor of the extended partition, if any
354 # @param self Parameter description
221 def get_extended_partition(self): 355 def get_extended_partition(self):
222 for part in self._partitions: 356 for part in self._partitions:
223 tmppart = self._partitions[part] 357 tmppart = self._partitions[part]
224 if tmppart.is_extended(): 358 if tmppart.is_extended():
225 return part 359 return part
226 return 0 360 return 0
227 361
362 ##
363 # Returns the number of sectors on the device
364 # @param self Parameter description
228 def get_num_sectors(self): 365 def get_num_sectors(self):
229 return int(self._total_sectors) 366 return int(self._total_sectors)
230 367
368 ##
369 # Returns the size of a cylinder in bytes
370 # @param self Parameter description
231 def get_cylinder_size(self): 371 def get_cylinder_size(self):
232 return int(self._cylinder_bytes) 372 return int(self._cylinder_bytes)
233 373
374 ##
375 # Returns the size of a sector in bytes
376 # @param self Parameter description
234 def get_sector_size(self): 377 def get_sector_size(self):
235 return int(self._sector_bytes) 378 return int(self._sector_bytes)
236 379
380 ##
381 # Returns the number of cylinders
382 # @param self Parameter description
237 def get_num_cylinders(self): 383 def get_num_cylinders(self):
238 return int(self._geometry['cylinders']) 384 return int(self._geometry['cylinders'])
239 385
386 ##
387 # Returns the total number of bytes on the device
388 # @param self Parameter description
240 def get_drive_bytes(self): 389 def get_drive_bytes(self):
241 return int(self._total_bytes) 390 return int(self._total_bytes)
242 391
392 ##
393 # Returns the total number of MB on the device
394 # @param self Parameter description
395 def get_total_mb(self):
396 return self._total_mb
397
398 ##
399 # Returns partition info dictionary
400 # @param self Parameter description
243 def get_partitions(self): 401 def get_partitions(self):
244 return self._partitions 402 return self._partitions
245 403
246 def print_partitions(self): 404 ##
247 for part in self._partitions.keys(): 405 # Prints disk geometry to STDOUT (no longer used)
248 print self._partitions[part].return_info() 406 # @param self Parameter description
249
250 def print_geometry(self): 407 def print_geometry(self):
251 print self._total_bytes, self._geometry 408 print self._total_bytes, self._geometry
252 409
410 ##
411 # Utility function for raising an exception
412 # @param self Parameter description
413 # @param message Error message
253 def _error(self, message): 414 def _error(self, message):
254 "Raises an exception" 415 "Raises an exception"
255 raise "DeviceObjectError", message 416 raise "DeviceObjectError", message
256 417
418 ##
419 # Utility function for running a command and returning it's output as a list
420 # @param self Parameter description
421 # @param cmd Command to run
257 def _run(self, cmd): 422 def _run(self, cmd):
258 "Runs a command and returns the output" 423 "Runs a command and returns the output"
259 424
260 # Run command 425 # Run command
261 output_string = commands.getoutput(cmd) 426 output_string = commands.getoutput(cmd)
275 output_string = output_string[index:] 440 output_string = output_string[index:]
276 441
277 # return output 442 # return output
278 return output_list 443 return output_list
279 444
280 445##
446# This class represents a partition within a GLIStorageDevice object
281class Partition: 447class Partition:
282 "Class representing a single partition within a Device object" 448 "Class representing a single partition within a Device object"
283 449
284 _device = None 450 _device = None
285 _minor = None 451 _minor = 0
286 _orig_minor = "" 452 _orig_minor = 0
287 _bootflag = None
288 _start = None 453 _start = 0
289 _end = None 454 _end = 0
290 _blocks = None
291 _type = None 455 _type = None
292 _mountpoint = None 456 _mountpoint = None
293 _mountopts = None 457 _mountopts = None
294 _format = None 458 _format = None
295 _resizeable = None 459 _resizeable = None
296 _min_sectors_for_resize = 0 460 _min_sectors_for_resize = 0
461 _mb = 0
297 462
463 ##
464 # Initialization function for the Partition class
465 # @param self Parameter description
466 # @param device Parent GLIStorageDevice object
467 # @param minor Minor of partition
468 # @param mb Parameter Size of partition in MB
469 # @param start Parameter Start sector of partition
470 # @param end Parameter Start sector of partition
471 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
472 # @param mountpoint='' Mountpoint of partition
473 # @param mountopts='' Mount options of partition
474 # @param format=True Format partition
475 # @param existing=False This partition exists on disk
298 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 476 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False):
299 self._device = device 477 self._device = device
300 self._minor = int(minor) 478 self._minor = float(minor)
301 if existing: self._orig_minor = int(minor) 479 if existing: self._orig_minor = int(minor)
302 self._bootflag = bootflag
303 self._start = int(start) 480 self._start = int(start)
304 self._end = int(end) 481 self._end = int(end)
305 self._blocks = int(blocks)
306 if type == "": type = "unknown" 482 self._type = type or "unknown"
307 self._type = type
308 self._mountpoint = mountpoint 483 self._mountpoint = mountpoint
309 self._mountopts = mountopts 484 self._mountopts = mountopts
310 self._format = format 485 self._format = format
311 if blocks == 0: 486 self._mb = mb
312 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
313 if existing: 487 if existing:
488 self._orig_minor = self._minor
314 parted_part = device._parted_disk.get_partition(minor) 489 parted_part = device._parted_disk.get_partition(minor)
490 label_type = device._parted_disk.type.name
491 if label_type == "loop":
492 dev_node = device._device
493 else:
494 dev_node = device._device + str(minor)
315 if type == "ntfs": 495 if type == "ntfs":
316 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 .\+//'")) 496 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 .\+//'"))
317 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 497 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
318 self._resizeable == True 498 self._resizeable == True
319 elif type == "ext2" or type == "ext3": 499 elif type == "ext2" or type == "ext3":
320 block_size = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")) 500 block_size = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'"))
321 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")) 501 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'"))
322 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes) 502 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes)
323 free_sec = free_sec - 2000 # just to be safe 503 free_sec = free_sec - 2000 # just to be safe
324 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec 504 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
325 self._resizeable == True 505 self._resizeable == True
326 else: 506 else:
327 parted_part = self._device._parted_disk.get_partition(self._minor) 507 parted_part = self._device._parted_disk.get_partition(int(self._minor))
328 try: 508 try:
329 parted_fs = parted_part.geom.file_system_open() 509 parted_fs = parted_part.geom.file_system_open()
330 except: 510 except:
331 self._resizeable = False 511 self._resizeable = False
332 return 512 return
334 min_size = resize_constraint.min_size 514 min_size = resize_constraint.min_size
335 if int(min_size) != min_size: min_size = int(min_size) + 1 515 if int(min_size) != min_size: min_size = int(min_size) + 1
336 self._min_sectors_for_resize = min_size 516 self._min_sectors_for_resize = min_size
337 self._resizeable = True 517 self._resizeable = True
338 518
519 ##
520 # Returns whether or not the partition is extended
521 # @param self Parameter description
339 def is_extended(self): 522 def is_extended(self):
340 if self._type == "extended": 523 if self._type == "extended":
341 return True 524 return True
342 else: 525 else:
343 return False 526 return False
344 527
528 ##
529 # Returns whether or not the partition is logical
530 # @param self Parameter description
345 def is_logical(self): 531 def is_logical(self):
346 part = self._device.get_partition_at(self._start, ignore_extended=0) 532 if self._type == "free":
347 if part and self._device._partitions[part].is_extended() and not part == self._minor: 533 if int(self._minor) + Decimal("0.9") == Decimal(str(self._minor)):
534 return True
535 else:
536 return False
537 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
348 return True 538 return True
539 else:
349 return False 540 return False
350 541
542 ##
543 # Returns a list of logical partitions if this is an extended partition
544 # @param self Parameter description
351 def get_logicals(self): 545 def get_logicals(self):
352 if not self.is_extended(): 546 if not self.is_extended():
353 return None 547 return None
354 logicals = [] 548 logicals = []
355 start = self._start 549 start = self._start
359 if part < 5: continue 553 if part < 5: continue
360 logicals.append(part) 554 logicals.append(part)
361 logicals.sort() 555 logicals.sort()
362 return logicals 556 return logicals
363 557
558 ##
559 # Returns the extened parent partition if this is a logical partition (no longer used)
560 # @param self Parameter description
364 def get_extended_parent(self): 561 def get_extended_parent(self):
365 if not self.is_logical(): 562 if not self.is_logical():
366 return None 563 return None
367 else: 564 else:
368 return self._device.get_partition_at(self._start, ignore_extended=0) 565 return self._device.get_partition_at(self._start, ignore_extended=0)
369 566
567 ##
568 # Sets the start sector for the partition
569 # @param self Parameter description
570 # @param start Start sector
370 def set_start(self, start): 571 def set_start(self, start):
371 self._start = int(start) 572 self._start = int(start)
372 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 573 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
373 574
575 ##
576 # Returns the start sector for the partition
577 # @param self Parameter description
374 def get_start(self): 578 def get_start(self):
375 return int(self._start) 579 return int(self._start)
376 580
581 ##
582 # Sets the end sector of the partition
583 # @param self Parameter description
584 # @param end End sector
377 def set_end(self, end): 585 def set_end(self, end):
378 self._end = int(end) 586 self._end = int(end)
379 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 587 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
380 588
589 ##
590 # Returns end sector for the partition
591 # @param self Parameter description
381 def get_end(self): 592 def get_end(self):
382 return int(self._end) 593 return int(self._end)
383 594
595 ##
596 # Returns size of partition in MB
597 # @param self Parameter description
598 def get_mb(self):
599 return int(self._mb)
600
601 ##
602 # Sets size of partition in MB
603 # @param self Parameter description
604 # @param mb Parameter description
605 def set_mb(self, mb):
606 self._mb = int(mb)
607
608 ##
609 # Sets type of partition
610 # @param self Parameter description
611 # @param type Parameter description
384 def set_type(self, type): 612 def set_type(self, type):
385 self._type = type 613 self._type = type
386 614
615 ##
616 # Returns type of partition
617 # @param self Parameter description
387 def get_type(self): 618 def get_type(self):
388 return self._type 619 return self._type
389 620
621 ##
622 # Returns parent GLIStorageDevice object
623 # @param self Parameter description
390 def get_device(self): 624 def get_device(self):
391 return self._device 625 return self._device
392 626
627 ##
628 # Sets minor of partition
629 # @param self Parameter description
630 # @param minor New minor
393 def set_minor(self, minor): 631 def set_minor(self, minor):
394 self._minor = int(minor) 632 self._minor = float(minor)
395 633
634 ##
635 # Returns minor of partition
636 # @param self Parameter description
396 def get_minor(self): 637 def get_minor(self):
397 return int(self._minor) 638 return float(self._minor)
398 639
640 ##
641 # Sets the original minor of the partition
642 # @param self Parameter description
643 # @param orig_minor Parameter description
399 def set_orig_minor(self, orig_minor): 644 def set_orig_minor(self, orig_minor):
400 self._orig_minor = int(orig_minor) 645 self._orig_minor = int(orig_minor)
401 646
647 ##
648 # Returns the original minor of the partition
649 # @param self Parameter description
402 def get_orig_minor(self): 650 def get_orig_minor(self):
403 return self._orig_minor 651 return self._orig_minor
404 652
653 ##
654 # Sets the mountpoint for the partition
655 # @param self Parameter description
656 # @param mountpoint Mountpoint
405 def set_mountpoint(self, mountpoint): 657 def set_mountpoint(self, mountpoint):
406 self._mountpoint = mountpoint 658 self._mountpoint = mountpoint
407 659
660 ##
661 # Returns the mountpoint for the partition
662 # @param self Parameter description
408 def get_mountpoint(self): 663 def get_mountpoint(self):
409 return self._mountpoint 664 return self._mountpoint
410 665
666 ##
667 # Sets the mount options for the partition
668 # @param self Parameter description
669 # @param mountopts Mount options
411 def set_mountopts(self, mountopts): 670 def set_mountopts(self, mountopts):
412 self._mountopts = mountopts 671 self._mountopts = mountopts
413 672
673 ##
674 # Returns the mount options for the partition
675 # @param self Parameter description
414 def get_mountopts(self): 676 def get_mountopts(self):
415 return self._mountopts 677 return self._mountopts
416 678
679 ##
680 # Set whether to format the partition
681 # @param self Parameter description
682 # @param format Parameter description
417 def set_format(self, format): 683 def set_format(self, format):
418 self._format = format 684 self._format = format
419 685
686 ##
687 # Returns whether to format the partition
688 # @param self Parameter description
420 def get_format(self): 689 def get_format(self):
421 return self._format 690 return self._format
422 691
423 def get_blocks(self): 692 ##
424 return int(self._blocks) 693 # Returns minimum sectors for resize
425 694 # @param self Parameter description
426 def get_min_sectors_for_resize(self): 695 def get_min_sectors_for_resize(self):
427 if self.is_extended(): 696 if self.is_extended():
428 min_size = self._start 697 min_size = self._start
429 for part in self._device._partitions: 698 for part in self._device._partitions:
430 if part < 5: continue 699 if part < 5: continue
431 if part.get_end > min_size: min_size = part.get_end() 700 if part.get_end > min_size: min_size = part.get_end()
432 return min_size 701 return min_size
433 else: 702 else:
434 return self._min_sectors_for_resize 703 return self._min_sectors_for_resize
435 704
705 ##
706 # Returns maximum sectors for resize
707 # @param self Parameter description
436 def get_max_sectors_for_resize(self): 708 def get_max_sectors_for_resize(self):
437 free_start, free_end = self._device.get_free_space(self._end) 709 free_start, free_end = self._device.get_free_space(self._end)
438 if free_end == -1: return self._end 710 if free_end == -1: return self._end
439 if free_start - 1 == self._end: 711 if free_start - 1 == self._end:
440 if self.is_logical(): 712 if self.is_logical():
443 else: 715 else:
444 return self._end - self._start 716 return self._end - self._start
445 else: 717 else:
446 return free_end - self._start 718 return free_end - self._start
447 719
720 ##
721 # Resizes the partition
722 # @param self Parameter description
723 # @param start New start sector
724 # @param end New end sector
448 def resize(self, start, end): 725 def resize(self, start, end):
449 part_at_start = self._device.get_partition_at(int(start)) 726 part_at_start = self._device.get_partition_at(int(start))
450 part_at_end = self._device.get_partition_at(int(end)) 727 part_at_end = self._device.get_partition_at(int(end))
451 logicals = None 728 logicals = None
452 if self.is_logical(): 729 if self.is_logical():
466 return 0 743 return 0
467 self.set_start(start) 744 self.set_start(start)
468 self.set_end(end) 745 self.set_end(end)
469 return 1 746 return 1
470 747
748 ##
749 # Utility function to raise an exception
750 # @param self Parameter description
751 # @param message Error message
471 def _error(self, message): 752 def _error(self, message):
472 "Raises an exception" 753 "Raises an exception"
473 raise "PartitionObjectError", message 754 raise "PartitionObjectError", message
474
475 755
756##
757# Returns a list of detected partitionable devices
476def detect_devices(): 758def detect_devices():
477 "Returns a list of partitionable devices on the system" 759 "Returns a list of partitionable devices on the system"
478 760
479 devices = [] 761 devices = []
480 762

Legend:
Removed from v.386  
changed lines
  Added in v.514

  ViewVC Help
Powered by ViewVC 1.1.20