/[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 463
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]
71 return self._device 74 return self._device
72 75
73 def clear_partitions(self): 76 def clear_partitions(self):
74 self._partitions = {} 77 self._partitions = {}
75 78
76 def add_partition(self, minor, start, end, type): 79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''):
77 free_start, free_end = self.get_free_space(start) 80 free_start, free_end = self.get_free_space(start)
78 minor = int(minor) 81 minor = int(minor)
79 if not free_end: 82 if not free_end:
80 return False 83 return False
81 if self._partitions.has_key(minor): 84 if self._partitions.has_key(minor):
94 if (i >= hole_at) and (hole_at): continue 97 if (i >= hole_at) and (hole_at): continue
95 if i >= minor: 98 if i >= minor:
96 self._partitions[i].set_minor(i+1) 99 self._partitions[i].set_minor(i+1)
97 self._partitions[i+1] = self._partitions[i] 100 self._partitions[i+1] = self._partitions[i]
98 if i == minor: stopscooting = 1 101 if i == minor: stopscooting = 1
99 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)
100 103
101 def remove_partition(self, minor): 104 def remove_partition(self, minor):
102 del self._partitions[int(minor)] 105 del self._partitions[int(minor)]
103 106
104 def get_free_space(self, start): 107 def get_free_space(self, start):
108 GAP_SIZE = 100
105 parts = self._partitions.keys() 109 parts = self._partitions.keys()
106 parts.sort() 110 parts.sort()
107 lastend_pri = 0 111 lastend_pri = 0
108 lastend_log = 0 112 lastend_log = 0
109 free_start = -1 113 free_start = -1
110 free_end = -1 114 free_end = -1
111 if start > self.get_num_cylinders(): return (-1, -1) 115 if start > self._total_sectors: return (-1, -1)
112 for part in parts: 116 for part in parts:
113 if part > 4: break 117 if part > 4: break
114 tmppart = self._partitions[part] 118 tmppart = self._partitions[part]
115 if (tmppart.get_start() > lastend_pri) and (lastend_pri >= start): 119 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
116 free_start = lastend_pri 120 free_start = lastend_pri
117 free_end = tmppart.get_start() - 1 121 free_end = tmppart.get_start() - 1
118 break 122 break
119 if tmppart.is_extended() and start < tmppart.get_end(): 123 if tmppart.is_extended() and start < tmppart.get_end():
120 lastend_log = tmppart.get_start() 124 lastend_log = tmppart.get_start()
121 for part_log in parts: 125 for part_log in parts:
122 if part_log < 5: continue 126 if part_log < 5: continue
123 tmppart_log = self._partitions[part_log] 127 tmppart_log = self._partitions[part_log]
124 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):
125 free_start = lastend_log 129 free_start = lastend_log
126 free_end = tmppart_log.get_start() - 1 130 free_end = tmppart_log.get_start() - 1
127 break 131 break
128 lastend_log = tmppart_log.get_end() + 1 132 lastend_log = tmppart_log.get_end() + 1
129 if free_start == -1 and lastend_log < tmppart.get_end(): 133 if free_start == -1 and lastend_log < tmppart.get_end():
130 free_start = lastend_log 134 free_start = lastend_log
131 free_end = tmppart.get_end() 135 free_end = tmppart.get_end()
132 break 136 break
133 lastend_pri = tmppart.get_end() + 1 137 lastend_pri = tmppart.get_end() + 1
134 if free_start == -1 and lastend_pri < self.get_num_cylinders(): 138 if free_start == -1 and lastend_pri < self._total_sectors:
135 free_start = lastend_pri 139 free_start = lastend_pri
136 free_end = self.get_num_cylinders() 140 free_end = self._total_sectors
137 return (free_start, free_end) 141 return (free_start, free_end)
138 142
139 def get_partition_at(self, cylinder, ignore_extended=1): 143 def get_partition_at(self, sector, ignore_extended=1):
140 parts = self._partitions.keys() 144 parts = self._partitions.keys()
141 parts.sort() 145 parts.sort()
142 for part in parts: 146 for part in parts:
143 tmppart = self._partitions[part] 147 tmppart = self._partitions[part]
144 if ignore_extended and tmppart.is_extended(): continue 148 if ignore_extended and tmppart.is_extended(): continue
145 if (cylinder >= tmppart.get_start()) and (cylinder <= tmppart.get_end()): 149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
146 return part 150 return part
147 return 0 151 return 0
148 152
149 def get_free_minor_at(self, start, end): 153 def get_free_minor_at(self, start, end):
150 parts = self._partitions.keys() 154 parts = self._partitions.keys()
209 213
210 def get_install_profile_structure(self): 214 def get_install_profile_structure(self):
211 devdic = {} 215 devdic = {}
212 for part in self._partitions: 216 for part in self._partitions:
213 tmppart = self._partitions[part] 217 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() } 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() }
215 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
216 227
217 def get_num_sectors(self): 228 def get_num_sectors(self):
218 return int(self._total_sectors) 229 return int(self._total_sectors)
219 230
220 def get_cylinder_size(self): 231 def get_cylinder_size(self):
221 return int(self._cylinder_bytes) 232 return int(self._cylinder_bytes)
233
234 def get_sector_size(self):
235 return int(self._sector_bytes)
222 236
223 def get_num_cylinders(self): 237 def get_num_cylinders(self):
224 return int(self._geometry['cylinders']) 238 return int(self._geometry['cylinders'])
225 239
226 def get_drive_bytes(self): 240 def get_drive_bytes(self):
267class Partition: 281class Partition:
268 "Class representing a single partition within a Device object" 282 "Class representing a single partition within a Device object"
269 283
270 _device = None 284 _device = None
271 _minor = None 285 _minor = None
286 _orig_minor = ""
272 _bootflag = None 287 _bootflag = None
273 _start = None 288 _start = None
274 _end = None 289 _end = None
275 _blocks = None 290 _blocks = None
276 _type = None 291 _type = None
277 _mountpoint = None 292 _mountpoint = None
278 _mountopts = None 293 _mountopts = None
279 _format = None 294 _format = None
280 _resizeable = None 295 _resizeable = None
281 _min_cylinders_for_resize = 0 296 _min_sectors_for_resize = 0
282 297
283 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):
284 self._device = device 299 self._device = device
285 self._minor = int(minor) 300 self._minor = int(minor)
301 if existing: self._orig_minor = int(minor)
286 self._bootflag = bootflag 302 self._bootflag = bootflag
287 self._start = int(start) 303 self._start = int(start)
288 self._end = int(end) 304 self._end = int(end)
289 self._blocks = int(blocks) 305 self._blocks = int(blocks)
306 if type == "": type = "unknown"
290 self._type = type 307 self._type = type
291 self._mountpoint = mountpoint 308 self._mountpoint = mountpoint
292 self._mountopts = mountopts 309 self._mountopts = mountopts
293 self._format = format 310 self._format = format
294 if blocks == 0: 311 if blocks == 0:
295 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
296 if existing: 313 if existing:
297 parted_part = device._parted_disk.get_partition(minor) 314 parted_part = device._parted_disk.get_partition(minor)
315 label_type = device._parted_disk.type.name
316 if label_type == "loop":
317 dev_node = device._device + str(minor)
318 else:
319 dev_node = device._device
298 if type == "ntfs": 320 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 .\+//'")) 321 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 322 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
301 self._resizeable == True 323 self._resizeable == True
302 elif type == "ext2" or type == "ext3": 324 elif type == "ext2" or type == "ext3":
303 commands.system("mkdir /mnt/freespace; mount " + device._device + str(minor) + " /mnt/freespace") 325 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 326 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") 327 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 328 free_sec = free_sec - 2000 # just to be safe
307 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 329 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
308 self._resizeable == True 330 self._resizeable == True
309 elif type == "fat16" or type == "fat32": 331 else:
310 parted_part = self._device._parted_disk.get_partition(self._minor) 332 parted_part = self._device._parted_disk.get_partition(self._minor)
333 try:
311 parted_fs = parted_part.geom.file_system_open() 334 parted_fs = parted_part.geom.file_system_open()
335 except:
336 self._resizeable = False
337 return
312 resize_constraint = parted_fs.get_resize_constraint() 338 resize_constraint = parted_fs.get_resize_constraint()
313 min_size = float(resize_constraint.min_size / self._device._sectors_in_cylinder) 339 min_size = resize_constraint.min_size
314 if int(min_size) != min_size: min_size = int(min_size) + 1 340 if int(min_size) != min_size: min_size = int(min_size) + 1
315 self._min_cylinders_for_resize = min_size 341 self._min_sectors_for_resize = min_size
316 self._resizeable = True 342 self._resizeable = True
317 elif type == "":
318 self._min_cylinders_for_resize = 1
319 self._resizeable = True
320 else:
321 self._resizeable = True
322 343
323 def is_extended(self): 344 def is_extended(self):
324 if self._type == "extended": 345 if self._type == "extended":
325 return True 346 return True
326 else: 347 else:
331 if part and self._device._partitions[part].is_extended() and not part == self._minor: 352 if part and self._device._partitions[part].is_extended() and not part == self._minor:
332 return True 353 return True
333 return False 354 return False
334 355
335 def get_logicals(self): 356 def get_logicals(self):
357 if not self.is_extended():
358 return None
336 logicals = [] 359 logicals = []
337 start = self._start 360 start = self._start
338 while not start > self._end:
339 part = self._device.get_partition_at(start) 361 parts = self._device._partitions.keys()
340 if not part: break 362 parts.sort()
363 for part in parts:
364 if part < 5: continue
341 logicals.append(part) 365 logicals.append(part)
342 start = self._device._partitions[part].get_end() + 1
343 logicals.sort() 366 logicals.sort()
344 return logicals 367 return logicals
345 368
346 def get_extended_parent(self): 369 def get_extended_parent(self):
370 if not self.is_logical():
371 return None
372 else:
347 return self._device.get_partition_at(self._start, ignore_extended=0) 373 return self._device.get_partition_at(self._start, ignore_extended=0)
348 374
349 def set_start(self, start): 375 def set_start(self, start):
350 self._start = int(start) 376 self._start = int(start)
351 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 377 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
352 378
373 self._minor = int(minor) 399 self._minor = int(minor)
374 400
375 def get_minor(self): 401 def get_minor(self):
376 return int(self._minor) 402 return int(self._minor)
377 403
404 def set_orig_minor(self, orig_minor):
405 self._orig_minor = int(orig_minor)
406
407 def get_orig_minor(self):
408 return self._orig_minor
409
378 def set_mountpoint(self, mountpoint): 410 def set_mountpoint(self, mountpoint):
379 self._mountpoint = mountpoint 411 self._mountpoint = mountpoint
380 412
381 def get_mountpoint(self): 413 def get_mountpoint(self):
382 return self._mountpoint 414 return self._mountpoint
394 return self._format 426 return self._format
395 427
396 def get_blocks(self): 428 def get_blocks(self):
397 return int(self._blocks) 429 return int(self._blocks)
398 430
399 def get_min_cylinders_for_resize(self): 431 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(): 432 if self.is_extended():
410 min_size = self._start 433 min_size = self._start
411 for part in self._device._partitions: 434 for part in self._device._partitions:
412 if part < 5: continue 435 if part < 5: continue
413 min_size = part.get_end() 436 if part.get_end > min_size: min_size = part.get_end()
437 return min_size
414 else: 438 else:
415 return self._min_cylinders_for_resize 439 return self._min_sectors_for_resize
416 440
417 def get_max_cylinders_for_resize(self): 441 def get_max_sectors_for_resize(self):
418 free_start, free_end = self._device.get_free_space(self._end) 442 free_start, free_end = self._device.get_free_space(self._end)
419 if free_end == -1: return self._end 443 if free_end == -1: return self._end
420 if free_start - 1 == self._end: 444 if free_start - 1 == self._end:
421 if self.is_logical(): 445 if self.is_logical():
422 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 446 if free_end <= self._device._partitions[self.get_extended_parent()]._end:
423 return free_end 447 return free_end - self._start
424 else: 448 else:
425 return self._end 449 return self._end - self._start
426 else: 450 else:
427 return free_end 451 return free_end - self._start
428 452
429 def resize(self, start, end): 453 def resize(self, start, end):
430 part_at_start = self._device.get_partition_at(int(start)) 454 part_at_start = self._device.get_partition_at(int(start))
431 part_at_end = self._device.get_partition_at(int(end)) 455 part_at_end = self._device.get_partition_at(int(end))
432 logicals = None 456 logicals = None

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

  ViewVC Help
Powered by ViewVC 1.1.20