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

Diff of /trunk/src/GLIStorageDevice.py

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

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

Legend:
Removed from v.313  
changed lines
  Added in v.509

  ViewVC Help
Powered by ViewVC 1.1.20