/[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 494
1import commands, string, re, os, parted 1import commands, string, re, os, parted
2from decimal import Decimal
3
4MEGABYTE = 1024 * 1024
2 5
3class Device: 6class Device:
4 "Class representing a partitionable device." 7 "Class representing a partitionable device."
5 8
6 _device = None 9 _device = None
10 _total_sectors = 0 13 _total_sectors = 0
11 _cylinder_bytes = 0 14 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 15 _sectors_in_cylinder = 0
13 _parted_dev = None 16 _parted_dev = None
14 _parted_disk = None 17 _parted_disk = None
15 _fdiskcall = "fdisk -l " 18 _sector_bytes = 0
19 _total_mb = 0
20 _arch = None
21
22 archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
23 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
24 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
25 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
26 }
16 27
17 def __init__(self, device): 28 def __init__(self, device, arch="x86"):
18 self._device = device 29 self._device = device
19 self._partitions = {} 30 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 31 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
21 self._total_bytes = 0 32 self._total_bytes = 0
22 self._cylinder_bytes = 0 33 self._cylinder_bytes = 0
34 self._arch = arch
23 self._parted_dev = parted.PedDevice.get(self._device) 35 self._parted_dev = parted.PedDevice.get(self._device)
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 36 self._parted_disk = parted.PedDisk.new(self._parted_dev)
25 self.set_disk_geometry_from_disk() 37 self.set_disk_geometry_from_disk()
26 38
27 def set_disk_geometry_from_disk(self): 39 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 40 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 41 self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = self._parted_dev.heads, self._parted_dev.sectors, self._parted_dev.cylinders
42 self._sector_bytes = self._parted_dev.sector_size
39 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._parted_dev.sector_size 43 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'] 44 self._total_sectors = self._parted_dev.length
41 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 45 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
46 self._total_mb = int(self._total_bytes / MEGABYTE)
42 47
43 def set_partitions_from_disk(self): 48 def set_partitions_from_disk(self):
49 last_part = 0
50 last_log_part = 4
44 parted_part = self._parted_disk.next_partition() 51 parted_part = self._parted_disk.next_partition()
45 while parted_part != None: 52 while parted_part:
53 part_mb = int((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
46 if parted_part.num < 1: 54 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 = "" 55 fs_type = ""
64 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 56 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
65 if parted_part.type == 2: fs_type = "extended" 57 if parted_part.type == 2: fs_type = "extended"
58 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)
59 elif parted_part.type_name == "free":
60 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
61 if parent_part:
62 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)
63 last_log_part += 1
64 else:
65 self._partitions[Decimal(str(float(last_log_part+0.1)))] = Partition(self, Decimal(str(float(last_log_part+0.1))), part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
66 last_part += 1
67 parted_part = self._parted_disk.next_partition(parted_part)
68
69 def set_partitions_from_install_profile_structure(self, ips):
70 pass
71# for part in ips:
72# tmppart = ips[part]
73# existing = False
74# parted_part = self._parted_disk.get_partition(part)
75# if parted_part != None:
76# start = parted_part.geom.start / self._sectors_in_cylinder
77# end = parted_part.geom.end / self._sectors_in_cylinder
78# fs_type = ""
79# if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
80# 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: 81# if int(tmppart['start']) == int(start) and int(tmppart['end']) == int(end) and tmppart['type'] == fs_type and tmppart['format'] == False:
67 existing = True 82# 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'])) 83# 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 84
70 def get_device(self): 85 def get_device(self):
71 return self._device 86 return self._device
72 87
73 def clear_partitions(self): 88 def tidy_partitions(self):
74 self._partitions = {} 89 last_minor = 0
90 last_log_minor = 4
91 last_free = 0
92 last_log_free = 0
93 parts = self._partitions.keys()
94 parts.sort()
95 for part in parts:
96 if self.archinfo[self._arch]['extended'] and part > 4: break
97 tmppart = self._partitions[part]
98 if tmppart.get_type() == "extended":
99 for part_log in parts:
100 if part_log < Decimal("4.9"): continue
101 tmppart_log = self._partitions[part_log]
102 if tmppart_log.get_type() == "free":
103 if not last_log_free:
104 tmppart_log.set_minor(Decimal(str(last_log_minor + 0.9)))
105 else:
106 if last_log_minor < last_log_free:
107 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
108 del self._partitions[part_log]
109 last_log_free = part_log
110 else:
111 if part_log > (last_log_minor + 1):
112 tmppart_log.set_minor(last_log_minor + 1)
113 last_log_minor = part_log
114 if tmppart.get_type() == "free":
115 if not last_free:
116 tmppart.set_minor(Decimal(str(last_minor + 0.1)))
117 else:
118 if last_minor < last_free:
119 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
120 del self_partitions[part]
121 last_free = part
122 else:
123 if part > (last_minor + 1):
124 tmppart.set_minor(last_minor + 1)
125 last_minor = part
75 126
76 def add_partition(self, minor, start, end, type): 127 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts=''):
77 free_start, free_end = self.get_free_space(start) 128 free_minor = Decimal(str(free_minor))
78 minor = int(minor) 129 new_minor = int(free_minor) + 1
79 if not free_end: 130# print "add_partition(): free_minor=" + str(free_minor) + ", new_minor=" + str(new_minor)
80 return False
81 if self._partitions.has_key(minor): 131 if self._partitions.has_key(new_minor):
82 parts = self._partitions.keys() 132 parts = self._partitions.keys()
83 parts.sort() 133 parts.sort()
84 parts.reverse() 134 parts.reverse()
85 hole_at = 0 135 hole_at = 0
86 for i in range(1, parts[0]+1): 136 for i in range(1, parts[0]+1):
87 if i <= minor: continue 137 if i <= new_minor: continue
88 if not self._partitions.has_key(int(i)): 138 if not self._partitions.has_key(i):
89 hole_at = i 139 hole_at = i
90 break 140 break
91 stopscooting = 0 141 stopscooting = 0
92 for i in parts: 142 for i in parts:
93 if stopscooting: break 143 if stopscooting: break
94 if (i >= hole_at) and (hole_at): continue 144 if i >= hole_at and hole_at: continue
95 if i >= minor: 145 if i >= new_minor:
96 self._partitions[i].set_minor(i+1) 146 self._partitions[i].set_minor(i+1)
97 self._partitions[i+1] = self._partitions[i] 147 self._partitions[i+1] = self._partitions[i]
98 if i == minor: stopscooting = 1 148 if i == new_minor: stopscooting = 1
99 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type) 149 if mb != self._partitions[free_minor].get_mb():
150 old_free_mb = self._partitions[free_minor].get_mb()
151 del self._partitions[free_minor]
152 if self.archinfo[self._arch]['extended'] and new_minor >= 5:
153 free_minor = Decimal(str(new_minor + 0.9))
154 else:
155 free_minor = Decimal(str(new_minor + 0.1))
156 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
157# print "add_partition(): new part doesn't use all freespace. new free part is: minor=" + str(free_minor)
158 else:
159 del self._partitions[free_minor]
160 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts)
161 if type == "extended":
162 self._partitions[Decimal("4.9")] = Partition(self, Decimal("4.9"), mb, 0, 0, "free")
163 self.tidy_partitions()
100 164
101 def remove_partition(self, minor): 165 def remove_partition(self, minor):
166 tmppart = self._partitions[int(minor)]
167 free_minor = 0
168 if tmppart.is_logical():
169 free_minor = Decimal(str(float(minor)-0.1))
170 else:
171 free_minor = Decimal(str(float(minor)-0.9))
172 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
102 del self._partitions[int(minor)] 173 del self._partitions[int(minor)]
174 self.tidy_partitions()
103 175
104 def get_free_space(self, start): 176 def get_free_space(self, start):
177 GAP_SIZE = 100
105 parts = self._partitions.keys() 178 parts = self._partitions.keys()
106 parts.sort() 179 parts.sort()
107 lastend_pri = 0 180 lastend_pri = 0
108 lastend_log = 0 181 lastend_log = 0
109 free_start = -1 182 free_start = -1
110 free_end = -1 183 free_end = -1
111 if start > self.get_num_cylinders(): return (-1, -1) 184 if start > self._total_sectors: return (-1, -1)
112 for part in parts: 185 for part in parts:
113 if part > 4: break 186 if part > 4: break
114 tmppart = self._partitions[part] 187 tmppart = self._partitions[part]
115 if (tmppart.get_start() > lastend_pri) and (lastend_pri >= start): 188 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
116 free_start = lastend_pri 189 free_start = lastend_pri
117 free_end = tmppart.get_start() - 1 190 free_end = tmppart.get_start() - 1
118 break 191 break
119 if tmppart.is_extended() and start < tmppart.get_end(): 192 if tmppart.is_extended() and start < tmppart.get_end():
120 lastend_log = tmppart.get_start() 193 lastend_log = tmppart.get_start()
121 for part_log in parts: 194 for part_log in parts:
122 if part_log < 5: continue 195 if part_log < 5: continue
123 tmppart_log = self._partitions[part_log] 196 tmppart_log = self._partitions[part_log]
124 if (tmppart_log.get_start() > lastend_log) and (lastend_log >= start): 197 if (tmppart_log.get_start() > (lastend_log + GAP_SIZE)) and (lastend_log >= start):
125 free_start = lastend_log 198 free_start = lastend_log
126 free_end = tmppart_log.get_start() - 1 199 free_end = tmppart_log.get_start() - 1
127 break 200 break
128 lastend_log = tmppart_log.get_end() + 1 201 lastend_log = tmppart_log.get_end() + 1
129 if free_start == -1 and lastend_log < tmppart.get_end(): 202 if free_start == -1 and lastend_log < tmppart.get_end():
130 free_start = lastend_log 203 free_start = lastend_log
131 free_end = tmppart.get_end() 204 free_end = tmppart.get_end()
132 break 205 break
133 lastend_pri = tmppart.get_end() + 1 206 lastend_pri = tmppart.get_end() + 1
134 if free_start == -1 and lastend_pri < self.get_num_cylinders(): 207 if free_start == -1 and lastend_pri < self._total_sectors:
135 free_start = lastend_pri 208 free_start = lastend_pri
136 free_end = self.get_num_cylinders() 209 free_end = self._total_sectors
137 return (free_start, free_end) 210 return (free_start, free_end)
138 211
139 def get_partition_at(self, cylinder, ignore_extended=1): 212 def get_partition_at(self, sector, ignore_extended=1):
140 parts = self._partitions.keys() 213 parts = self._partitions.keys()
141 parts.sort() 214 parts.sort()
142 for part in parts: 215 for part in parts:
143 tmppart = self._partitions[part] 216 tmppart = self._partitions[part]
144 if ignore_extended and tmppart.is_extended(): continue 217 if ignore_extended and tmppart.is_extended(): continue
145 if (cylinder >= tmppart.get_start()) and (cylinder <= tmppart.get_end()): 218 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
146 return part 219 return part
147 return 0 220 return 0
148 221
149 def get_free_minor_at(self, start, end): 222 def get_free_minor_at(self, start, end):
150 parts = self._partitions.keys() 223 parts = self._partitions.keys()
177 250
178 def get_ordered_partition_list(self): 251 def get_ordered_partition_list(self):
179 parts = self._partitions.keys() 252 parts = self._partitions.keys()
180 parts.sort() 253 parts.sort()
181 partlist = [] 254 partlist = []
182 free_start, free_end = self.get_free_space(0) 255 tmppart = None
183 tmppart = None
184 tmppart_log = None 256 tmppart_log = None
185 for part in parts: 257 for part in parts:
186 if part > 4: break 258 if self.archinfo[self._arch]['extended'] and part > Decimal("4.1"): break
187 tmppart = self._partitions[part] 259 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) 260 partlist.append(part)
194 if tmppart.is_extended(): 261 if tmppart.is_extended():
195 for part_log in parts: 262 for part_log in parts:
196 if part_log < 5: continue 263 if part_log < Decimal("4.9"): continue
197 tmppart_log = self._partitions[part_log] 264 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) 265 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 266 return partlist
209 267
210 def get_install_profile_structure(self): 268 def get_install_profile_structure(self):
211 devdic = {} 269 devdic = {}
212 for part in self._partitions: 270 for part in self._partitions:
213 tmppart = self._partitions[part] 271 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() } 272 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 273 return devdic
274
275 def get_extended_partition(self):
276 for part in self._partitions:
277 tmppart = self._partitions[part]
278 if tmppart.is_extended():
279 return part
280 return 0
216 281
217 def get_num_sectors(self): 282 def get_num_sectors(self):
218 return int(self._total_sectors) 283 return int(self._total_sectors)
219 284
220 def get_cylinder_size(self): 285 def get_cylinder_size(self):
221 return int(self._cylinder_bytes) 286 return int(self._cylinder_bytes)
222 287
288 def get_sector_size(self):
289 return int(self._sector_bytes)
290
223 def get_num_cylinders(self): 291 def get_num_cylinders(self):
224 return int(self._geometry['cylinders']) 292 return int(self._geometry['cylinders'])
225 293
226 def get_drive_bytes(self): 294 def get_drive_bytes(self):
227 return int(self._total_bytes) 295 return int(self._total_bytes)
228 296
297 def get_total_mb(self):
298 return self._total_mb
299
229 def get_partitions(self): 300 def get_partitions(self):
230 return self._partitions 301 return self._partitions
231 302
232 def print_partitions(self): 303# def print_partitions(self):
233 for part in self._partitions.keys(): 304# for part in self._partitions.keys():
234 print self._partitions[part].return_info() 305# print self._partitions[part].return_info()
235 306
236 def print_geometry(self): 307 def print_geometry(self):
237 print self._total_bytes, self._geometry 308 print self._total_bytes, self._geometry
238 309
239 def _error(self, message): 310 def _error(self, message):
266 337
267class Partition: 338class Partition:
268 "Class representing a single partition within a Device object" 339 "Class representing a single partition within a Device object"
269 340
270 _device = None 341 _device = None
271 _minor = None 342 _minor = 0
272 _bootflag = None 343 _orig_minor = 0
273 _start = None 344 _start = 0
274 _end = None 345 _end = 0
275 _blocks = None
276 _type = None 346 _type = None
277 _mountpoint = None 347 _mountpoint = None
278 _mountopts = None 348 _mountopts = None
279 _format = None 349 _format = None
280 _resizeable = None 350 _resizeable = None
281 _min_cylinders_for_resize = 0 351 _min_sectors_for_resize = 0
352 _mb = 0
282 353
283 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 354 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False):
284 self._device = device 355 self._device = device
285 self._minor = int(minor) 356 self._minor = float(minor)
286 self._bootflag = bootflag 357 if existing: self._orig_minor = int(minor)
287 self._start = int(start) 358 self._start = int(start)
288 self._end = int(end) 359 self._end = int(end)
289 self._blocks = int(blocks) 360 self._type = type or "unknown"
290 self._type = type
291 self._mountpoint = mountpoint 361 self._mountpoint = mountpoint
292 self._mountopts = mountopts 362 self._mountopts = mountopts
293 self._format = format 363 self._format = format
294 if blocks == 0: 364 self._mb = mb
295 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
296 if existing: 365 if existing:
366 self._orig_minor = self._minor
297 parted_part = device._parted_disk.get_partition(minor) 367 parted_part = device._parted_disk.get_partition(minor)
368 label_type = device._parted_disk.type.name
369 if label_type == "loop":
370 dev_node = device._device
371 else:
372 dev_node = device._device + str(minor)
298 if type == "ntfs": 373 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 .\+//'")) 374 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 375 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
301 self._resizeable == True 376 self._resizeable == True
302 elif type == "ext2" or type == "ext3": 377 elif type == "ext2" or type == "ext3":
303 commands.system("mkdir /mnt/freespace; mount " + device._device + str(minor) + " /mnt/freespace") 378 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 379 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") 380 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 381 free_sec = free_sec - 2000 # just to be safe
307 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 382 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
308 self._resizeable == True 383 self._resizeable == True
309 elif type == "fat16" or type == "fat32": 384 else:
310 parted_part = self._device._parted_disk.get_partition(self._minor) 385 parted_part = self._device._parted_disk.get_partition(int(self._minor))
386 try:
311 parted_fs = parted_part.geom.file_system_open() 387 parted_fs = parted_part.geom.file_system_open()
388 except:
389 self._resizeable = False
390 return
312 resize_constraint = parted_fs.get_resize_constraint() 391 resize_constraint = parted_fs.get_resize_constraint()
313 min_size = float(resize_constraint.min_size / self._device._sectors_in_cylinder) 392 min_size = resize_constraint.min_size
314 if int(min_size) != min_size: min_size = int(min_size) + 1 393 if int(min_size) != min_size: min_size = int(min_size) + 1
315 self._min_cylinders_for_resize = min_size 394 self._min_sectors_for_resize = min_size
316 self._resizeable = True 395 self._resizeable = True
317 elif type == "":
318 self._min_cylinders_for_resize = 1
319 self._resizeable = True
320 else:
321 self._resizeable = True
322 396
323 def is_extended(self): 397 def is_extended(self):
324 if self._type == "extended": 398 if self._type == "extended":
325 return True 399 return True
326 else: 400 else:
327 return False 401 return False
328 402
329 def is_logical(self): 403 def is_logical(self):
330 part = self._device.get_partition_at(self._start, ignore_extended=0) 404 if self._type == "free":
331 if part and self._device._partitions[part].is_extended() and not part == self._minor: 405 if int(self._minor) + Decimal("0.9") == Decimal(str(self._minor)):
406 return True
407 else:
408 return False
409 elif self._device.archinfo[self._device._arch]['extended'] and self._minor > 4:
332 return True 410 return True
411 else:
333 return False 412 return False
334 413
335 def get_logicals(self): 414 def get_logicals(self):
415 if not self.is_extended():
416 return None
336 logicals = [] 417 logicals = []
337 start = self._start 418 start = self._start
338 while not start > self._end:
339 part = self._device.get_partition_at(start) 419 parts = self._device._partitions.keys()
340 if not part: break 420 parts.sort()
421 for part in parts:
422 if part < 5: continue
341 logicals.append(part) 423 logicals.append(part)
342 start = self._device._partitions[part].get_end() + 1
343 logicals.sort() 424 logicals.sort()
344 return logicals 425 return logicals
345 426
346 def get_extended_parent(self): 427 def get_extended_parent(self):
428 if not self.is_logical():
429 return None
430 else:
347 return self._device.get_partition_at(self._start, ignore_extended=0) 431 return self._device.get_partition_at(self._start, ignore_extended=0)
348 432
349 def set_start(self, start): 433 def set_start(self, start):
350 self._start = int(start) 434 self._start = int(start)
351 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 435 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
352 436
358 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 442 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
359 443
360 def get_end(self): 444 def get_end(self):
361 return int(self._end) 445 return int(self._end)
362 446
447 def get_mb(self):
448 return int(self._mb)
449
450 def set_mb(self, mb):
451 self._mb = int(mb)
452
363 def set_type(self, type): 453 def set_type(self, type):
364 self._type = type 454 self._type = type
365 455
366 def get_type(self): 456 def get_type(self):
367 return self._type 457 return self._type
368 458
369 def get_device(self): 459 def get_device(self):
370 return self._device 460 return self._device
371 461
372 def set_minor(self, minor): 462 def set_minor(self, minor):
373 self._minor = int(minor) 463 self._minor = float(minor)
374 464
375 def get_minor(self): 465 def get_minor(self):
376 return int(self._minor) 466 return float(self._minor)
467
468 def set_orig_minor(self, orig_minor):
469 self._orig_minor = int(orig_minor)
470
471 def get_orig_minor(self):
472 return self._orig_minor
377 473
378 def set_mountpoint(self, mountpoint): 474 def set_mountpoint(self, mountpoint):
379 self._mountpoint = mountpoint 475 self._mountpoint = mountpoint
380 476
381 def get_mountpoint(self): 477 def get_mountpoint(self):
394 return self._format 490 return self._format
395 491
396 def get_blocks(self): 492 def get_blocks(self):
397 return int(self._blocks) 493 return int(self._blocks)
398 494
399 def get_min_cylinders_for_resize(self): 495 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(): 496 if self.is_extended():
410 min_size = self._start 497 min_size = self._start
411 for part in self._device._partitions: 498 for part in self._device._partitions:
412 if part < 5: continue 499 if part < 5: continue
413 min_size = part.get_end() 500 if part.get_end > min_size: min_size = part.get_end()
501 return min_size
414 else: 502 else:
415 return self._min_cylinders_for_resize 503 return self._min_sectors_for_resize
416 504
417 def get_max_cylinders_for_resize(self): 505 def get_max_sectors_for_resize(self):
418 free_start, free_end = self._device.get_free_space(self._end) 506 free_start, free_end = self._device.get_free_space(self._end)
419 if free_end == -1: return self._end 507 if free_end == -1: return self._end
420 if free_start - 1 == self._end: 508 if free_start - 1 == self._end:
421 if self.is_logical(): 509 if self.is_logical():
422 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 510 if free_end <= self._device._partitions[self.get_extended_parent()]._end:
423 return free_end 511 return free_end - self._start
424 else: 512 else:
425 return self._end 513 return self._end - self._start
426 else: 514 else:
427 return free_end 515 return free_end - self._start
428 516
429 def resize(self, start, end): 517 def resize(self, start, end):
430 part_at_start = self._device.get_partition_at(int(start)) 518 part_at_start = self._device.get_partition_at(int(start))
431 part_at_end = self._device.get_partition_at(int(end)) 519 part_at_end = self._device.get_partition_at(int(end))
432 logicals = None 520 logicals = None

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

  ViewVC Help
Powered by ViewVC 1.1.20