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

Diff of /trunk/src/GLIStorageDevice.py

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

Revision 107 Revision 386
10 _total_sectors = 0 10 _total_sectors = 0
11 _cylinder_bytes = 0 11 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 12 _sectors_in_cylinder = 0
13 _parted_dev = None 13 _parted_dev = None
14 _parted_disk = None 14 _parted_disk = None
15 _fdiskcall = "fdisk -l " 15 _sector_bytes = 0
16 16
17 def __init__(self, device): 17 def __init__(self, device):
18 self._device = device 18 self._device = device
19 self._partitions = {} 19 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 24 self._parted_disk = parted.PedDisk.new(self._parted_dev)
25 self.set_disk_geometry_from_disk() 25 self.set_disk_geometry_from_disk()
26 26
27 def set_disk_geometry_from_disk(self): 27 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 28 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 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") 30# proc_dir = "/proc/ide/" + commands.getoutput("echo " + self._device + " | cut -d '/' -f 3")
31 proc_dir = string.strip(proc_dir) 31# proc_dir = string.strip(proc_dir)
32 heads = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 2") 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") 33# sectors = commands.getoutput("cat " + proc_dir + "/geometry | grep logical | cut -d '/' -f 3")
34 total_sectors = commands.getoutput("cat " + proc_dir + "/capacity") 34# total_sectors = commands.getoutput("cat " + proc_dir + "/capacity")
35 cylinders = int(total_sectors) / (int(heads) * int(sectors)) 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) 36# self._geometry['heads'], self._geometry['sectors'], self._geometry['cylinders'] = int(heads), int(sectors), int(cylinders)
37 else: #SCSI 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 38 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
39 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._parted_dev.sector_size 41 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'] 42# self._total_sectors = self._geometry['cylinders'] * self._geometry['heads'] * self._geometry['sectors']
43 self._total_sectors = self._parted_dev.length
41 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
42 45
43 def set_partitions_from_disk(self): 46 def set_partitions_from_disk(self):
44 parted_part = self._parted_disk.next_partition() 47 parted_part = self._parted_disk.next_partition()
45 while parted_part != None: 48 while parted_part != None:
47 parted_part = self._parted_disk.next_partition(parted_part) 50 parted_part = self._parted_disk.next_partition(parted_part)
48 continue 51 continue
49 fs_type = "" 52 fs_type = ""
50 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 53 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
51 if parted_part.type == 2: fs_type = "extended" 54 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) 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)
53 parted_part = self._parted_disk.next_partition(parted_part) 56 parted_part = self._parted_disk.next_partition(parted_part)
54 57
55 def set_partitions_from_install_profile_structure(self, ips): 58 def set_partitions_from_install_profile_structure(self, ips):
56 for part in ips: 59 for part in ips:
57 tmppart = ips[part] 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 = ""
67 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
68 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:
70 existing = True
58 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'])) 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']))
59 72
60 def get_device(self): 73 def get_device(self):
61 return self._device 74 return self._device
62 75
63 def clear_partitions(self): 76 def clear_partitions(self):
64 self._partitions = {} 77 self._partitions = {}
65 78
66 def add_partition(self, minor, start, end, type): 79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''):
67 free_start, free_end = self.get_free_space(start) 80 free_start, free_end = self.get_free_space(start)
68 minor = int(minor) 81 minor = int(minor)
69 if not free_end: 82 if not free_end:
70 return False 83 return False
71 if self._partitions.has_key(minor): 84 if self._partitions.has_key(minor):
84 if (i >= hole_at) and (hole_at): continue 97 if (i >= hole_at) and (hole_at): continue
85 if i >= minor: 98 if i >= minor:
86 self._partitions[i].set_minor(i+1) 99 self._partitions[i].set_minor(i+1)
87 self._partitions[i+1] = self._partitions[i] 100 self._partitions[i+1] = self._partitions[i]
88 if i == minor: stopscooting = 1 101 if i == minor: stopscooting = 1
89 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type) 102 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type, mountpoint=mountpoint, mountopts=mountopts)
90 103
91 def remove_partition(self, minor): 104 def remove_partition(self, minor):
92 del self._partitions[int(minor)] 105 del self._partitions[int(minor)]
93 106
94 def get_free_space(self, start): 107 def get_free_space(self, start):
108 GAP_SIZE = 100
95 parts = self._partitions.keys() 109 parts = self._partitions.keys()
96 parts.sort() 110 parts.sort()
97 lastend_pri = 0 111 lastend_pri = 0
98 lastend_log = 0 112 lastend_log = 0
99 free_start = -1 113 free_start = -1
100 free_end = -1 114 free_end = -1
101 if start > self.get_num_cylinders(): return (-1, -1) 115 if start > self._total_sectors: return (-1, -1)
102 for part in parts: 116 for part in parts:
103 if part > 4: break 117 if part > 4: break
104 tmppart = self._partitions[part] 118 tmppart = self._partitions[part]
105 if (tmppart.get_start() > lastend_pri) and (lastend_pri >= start): 119 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
106 free_start = lastend_pri 120 free_start = lastend_pri
107 free_end = tmppart.get_start() - 1 121 free_end = tmppart.get_start() - 1
108 break 122 break
109 if tmppart.is_extended() and start < tmppart.get_end(): 123 if tmppart.is_extended() and start < tmppart.get_end():
110 lastend_log = tmppart.get_start() 124 lastend_log = tmppart.get_start()
111 for part_log in parts: 125 for part_log in parts:
112 if part_log < 5: continue 126 if part_log < 5: continue
113 tmppart_log = self._partitions[part_log] 127 tmppart_log = self._partitions[part_log]
114 if (tmppart_log.get_start() > lastend_log) and (lastend_log >= start): 128 if (tmppart_log.get_start() > (lastend_log + GAP_SIZE)) and (lastend_log >= start):
115 free_start = lastend_log 129 free_start = lastend_log
116 free_end = tmppart_log.get_start() - 1 130 free_end = tmppart_log.get_start() - 1
117 break 131 break
118 lastend_log = tmppart_log.get_end() + 1 132 lastend_log = tmppart_log.get_end() + 1
119 if free_start == -1 and lastend_log < tmppart.get_end(): 133 if free_start == -1 and lastend_log < tmppart.get_end():
120 free_start = lastend_log 134 free_start = lastend_log
121 free_end = tmppart.get_end() 135 free_end = tmppart.get_end()
122 break 136 break
123 lastend_pri = tmppart.get_end() + 1 137 lastend_pri = tmppart.get_end() + 1
124 if free_start == -1 and lastend_pri < self.get_num_cylinders(): 138 if free_start == -1 and lastend_pri < self._total_sectors:
125 free_start = lastend_pri 139 free_start = lastend_pri
126 free_end = self.get_num_cylinders() 140 free_end = self._total_sectors
127 return (free_start, free_end) 141 return (free_start, free_end)
128 142
129 def get_partition_at(self, cylinder, ignore_extended=1): 143 def get_partition_at(self, sector, ignore_extended=1):
130 parts = self._partitions.keys() 144 parts = self._partitions.keys()
131 parts.sort() 145 parts.sort()
132 for part in parts: 146 for part in parts:
133 tmppart = self._partitions[part] 147 tmppart = self._partitions[part]
134 if ignore_extended and tmppart.is_extended(): continue 148 if ignore_extended and tmppart.is_extended(): continue
135 if (cylinder >= tmppart.get_start()) and (cylinder <= tmppart.get_end()): 149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
136 return part 150 return part
137 return 0 151 return 0
138 152
139 def get_free_minor_at(self, start, end): 153 def get_free_minor_at(self, start, end):
140 parts = self._partitions.keys() 154 parts = self._partitions.keys()
199 213
200 def get_install_profile_structure(self): 214 def get_install_profile_structure(self):
201 devdic = {} 215 devdic = {}
202 for part in self._partitions: 216 for part in self._partitions:
203 tmppart = self._partitions[part] 217 tmppart = self._partitions[part]
204 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() } 218 devdic[part] = { 'mb': 0, 'minor': int(part), 'origminor': tmppart.get_orig_minor(), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() }
205 return devdic 219 return devdic
220
221 def get_extended_partition(self):
222 for part in self._partitions:
223 tmppart = self._partitions[part]
224 if tmppart.is_extended():
225 return part
226 return 0
206 227
207 def get_num_sectors(self): 228 def get_num_sectors(self):
208 return int(self._total_sectors) 229 return int(self._total_sectors)
209 230
210 def get_cylinder_size(self): 231 def get_cylinder_size(self):
211 return int(self._cylinder_bytes) 232 return int(self._cylinder_bytes)
233
234 def get_sector_size(self):
235 return int(self._sector_bytes)
212 236
213 def get_num_cylinders(self): 237 def get_num_cylinders(self):
214 return int(self._geometry['cylinders']) 238 return int(self._geometry['cylinders'])
215 239
216 def get_drive_bytes(self): 240 def get_drive_bytes(self):
257class Partition: 281class Partition:
258 "Class representing a single partition within a Device object" 282 "Class representing a single partition within a Device object"
259 283
260 _device = None 284 _device = None
261 _minor = None 285 _minor = None
286 _orig_minor = ""
262 _bootflag = None 287 _bootflag = None
263 _start = None 288 _start = None
264 _end = None 289 _end = None
265 _blocks = None 290 _blocks = None
266 _type = None 291 _type = None
267 _mountpoint = None 292 _mountpoint = None
268 _mountopts = None 293 _mountopts = None
269 _format = None 294 _format = None
270 _resizeable = None 295 _resizeable = None
271 _min_cylinders_for_resize = 0 296 _min_sectors_for_resize = 0
272 297
273 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 298 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False):
274 self._device = device 299 self._device = device
275 self._minor = int(minor) 300 self._minor = int(minor)
301 if existing: self._orig_minor = int(minor)
276 self._bootflag = bootflag 302 self._bootflag = bootflag
277 self._start = int(start) 303 self._start = int(start)
278 self._end = int(end) 304 self._end = int(end)
279 self._blocks = int(blocks) 305 self._blocks = int(blocks)
306 if type == "": type = "unknown"
280 self._type = type 307 self._type = type
281 self._mountpoint = mountpoint 308 self._mountpoint = mountpoint
282 self._mountopts = mountopts 309 self._mountopts = mountopts
283 self._format = format 310 self._format = format
284 if blocks == 0: 311 if blocks == 0:
285 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 312 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
286 if existing: 313 if existing:
287 parted_part = device._parted_disk.get_partition(minor) 314 parted_part = device._parted_disk.get_partition(minor)
288 if type == "ntfs": 315 if type == "ntfs":
289 ntfsresize_output = commands.getoutput("ntfsresize --info " + device._device + str(minor)) 316 min_bytes = int(commands.getoutput("ntfsresize -f --info " + device._device + str(minor) + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
317 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
290 self._resizeable == True 318 self._resizeable == True
291# elsif type == "ext2" or type == "ext3": 319 elif type == "ext2" or type == "ext3":
292# self._min_cylinders_for_resize = -1 320 block_size = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'"))
321 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'"))
322 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes)
323 free_sec = free_sec - 2000 # just to be safe
324 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
293# self._resizeable == True 325 self._resizeable == True
294# elsif type == "": 326 else:
295# self._min_cylinders_for_resize = self._start + 1
296# self._resizeable = True
297# elsif type == "fat16" or type == "fat32":
298# parted_part = self._device._parted_disk.get_partition(self._minor) 327 parted_part = self._device._parted_disk.get_partition(self._minor)
328 try:
299# parted_fs = parted_part.geom.file_system_open() 329 parted_fs = parted_part.geom.file_system_open()
330 except:
331 self._resizeable = False
332 return
300# resize_constraint = parted_fs.get_resize_constraint() 333 resize_constraint = parted_fs.get_resize_constraint()
301# min_size = float(resize_constraint.min_size / self._device._sectors_in_cylinder) 334 min_size = resize_constraint.min_size
302# if int(min_size) != min_size: min_size = int(min_size) + 1 335 if int(min_size) != min_size: min_size = int(min_size) + 1
303# self._min_cylinders_for_resize = min_size 336 self._min_sectors_for_resize = min_size
304 else:
305 self._resizeable = True 337 self._resizeable = True
306 338
307 def is_extended(self): 339 def is_extended(self):
308 if self._type == "extended": 340 if self._type == "extended":
309 return True 341 return True
310 else: 342 else:
315 if part and self._device._partitions[part].is_extended() and not part == self._minor: 347 if part and self._device._partitions[part].is_extended() and not part == self._minor:
316 return True 348 return True
317 return False 349 return False
318 350
319 def get_logicals(self): 351 def get_logicals(self):
352 if not self.is_extended():
353 return None
320 logicals = [] 354 logicals = []
321 start = self._start 355 start = self._start
322 while not start > self._end:
323 part = self._device.get_partition_at(start) 356 parts = self._device._partitions.keys()
324 if not part: break 357 parts.sort()
358 for part in parts:
359 if part < 5: continue
325 logicals.append(part) 360 logicals.append(part)
326 start = self._device._partitions[part].get_end() + 1
327 logicals.sort() 361 logicals.sort()
328 return logicals 362 return logicals
329 363
330 def get_extended_parent(self): 364 def get_extended_parent(self):
365 if not self.is_logical():
366 return None
367 else:
331 return self._device.get_partition_at(self._start, ignore_extended=0) 368 return self._device.get_partition_at(self._start, ignore_extended=0)
332 369
333 def set_start(self, start): 370 def set_start(self, start):
334 self._start = int(start) 371 self._start = int(start)
335 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 372 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
336 373
357 self._minor = int(minor) 394 self._minor = int(minor)
358 395
359 def get_minor(self): 396 def get_minor(self):
360 return int(self._minor) 397 return int(self._minor)
361 398
399 def set_orig_minor(self, orig_minor):
400 self._orig_minor = int(orig_minor)
401
402 def get_orig_minor(self):
403 return self._orig_minor
404
362 def set_mountpoint(self, mountpoint): 405 def set_mountpoint(self, mountpoint):
363 self._mountpoint = mountpoint 406 self._mountpoint = mountpoint
364 407
365 def get_mountpoint(self): 408 def get_mountpoint(self):
366 return self._mountpoint 409 return self._mountpoint
378 return self._format 421 return self._format
379 422
380 def get_blocks(self): 423 def get_blocks(self):
381 return int(self._blocks) 424 return int(self._blocks)
382 425
383 def get_min_cylinders_for_resize(self): 426 def get_min_sectors_for_resize(self):
384# min_size = self._start + 1
385# if not self._format:
386# parted_part = self._device._parted_disk.get_partition(self._minor)
387# parted_fs = parted_part.geom.file_system_open()
388# resize_constraint = parted_fs.get_resize_constraint()
389# min_size = float(resize_constraint.min_size / self._device._sectors_in_cylinder)
390# if int(min_size) != min_size: min_size = int(min_size) + 1
391# min_size = min_size + self._start
392#################################################
393 if self.is_extended(): 427 if self.is_extended():
394 min_size = self._start 428 min_size = self._start
395 for part in self._device._partitions: 429 for part in self._device._partitions:
396 if part < 5: continue 430 if part < 5: continue
397 min_size = part.get_end() 431 if part.get_end > min_size: min_size = part.get_end()
432 return min_size
398 else: 433 else:
399 return self._min_cylinders_for_resize 434 return self._min_sectors_for_resize
400 435
401 def get_max_cylinders_for_resize(self): 436 def get_max_sectors_for_resize(self):
402 free_start, free_end = self._device.get_free_space(self._end) 437 free_start, free_end = self._device.get_free_space(self._end)
403 if free_end == -1: return self._end 438 if free_end == -1: return self._end
404 if free_start - 1 == self._end: 439 if free_start - 1 == self._end:
405 if self.is_logical(): 440 if self.is_logical():
406 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 441 if free_end <= self._device._partitions[self.get_extended_parent()]._end:
407 return free_end 442 return free_end - self._start
408 else: 443 else:
409 return self._end 444 return self._end - self._start
410 else: 445 else:
411 return free_end 446 return free_end - self._start
412 447
413 def resize(self, start, end): 448 def resize(self, start, end):
414 part_at_start = self._device.get_partition_at(int(start)) 449 part_at_start = self._device.get_partition_at(int(start))
415 part_at_end = self._device.get_partition_at(int(end)) 450 part_at_end = self._device.get_partition_at(int(end))
416 logicals = None 451 logicals = None

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

  ViewVC Help
Powered by ViewVC 1.1.20