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

Diff of /trunk/src/GLIStorageDevice.py

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

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

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

  ViewVC Help
Powered by ViewVC 1.1.20