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

Diff of /trunk/src/GLIStorageDevice.py

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

Revision 198 Revision 313
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 39
40 self._sector_bytes = self._parted_dev.sector_size
40 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
41 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
42 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
43 45
44 def set_partitions_from_disk(self): 46 def set_partitions_from_disk(self):
45 parted_part = self._parted_disk.next_partition() 47 parted_part = self._parted_disk.next_partition()
46 while parted_part != None: 48 while parted_part != None:
48 parted_part = self._parted_disk.next_partition(parted_part) 50 parted_part = self._parted_disk.next_partition(parted_part)
49 continue 51 continue
50 fs_type = "" 52 fs_type = ""
51 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
52 if parted_part.type == 2: fs_type = "extended" 54 if parted_part.type == 2: fs_type = "extended"
53 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)
54 parted_part = self._parted_disk.next_partition(parted_part) 56 parted_part = self._parted_disk.next_partition(parted_part)
55 57
56 def set_partitions_from_install_profile_structure(self, ips): 58 def set_partitions_from_install_profile_structure(self, ips):
57 for part in ips: 59 for part in ips:
58 tmppart = ips[part] 60 tmppart = ips[part]
72 return self._device 74 return self._device
73 75
74 def clear_partitions(self): 76 def clear_partitions(self):
75 self._partitions = {} 77 self._partitions = {}
76 78
77 def add_partition(self, minor, start, end, type): 79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''):
78 free_start, free_end = self.get_free_space(start) 80 free_start, free_end = self.get_free_space(start)
79 minor = int(minor) 81 minor = int(minor)
80 if not free_end: 82 if not free_end:
81 return False 83 return False
82 if self._partitions.has_key(minor): 84 if self._partitions.has_key(minor):
95 if (i >= hole_at) and (hole_at): continue 97 if (i >= hole_at) and (hole_at): continue
96 if i >= minor: 98 if i >= minor:
97 self._partitions[i].set_minor(i+1) 99 self._partitions[i].set_minor(i+1)
98 self._partitions[i+1] = self._partitions[i] 100 self._partitions[i+1] = self._partitions[i]
99 if i == minor: stopscooting = 1 101 if i == minor: stopscooting = 1
100 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)
101 103
102 def remove_partition(self, minor): 104 def remove_partition(self, minor):
103 del self._partitions[int(minor)] 105 del self._partitions[int(minor)]
104 106
105 def get_free_space(self, start): 107 def get_free_space(self, start):
108 GAP_SIZE = 100
106 parts = self._partitions.keys() 109 parts = self._partitions.keys()
107 parts.sort() 110 parts.sort()
108 lastend_pri = 0 111 lastend_pri = 0
109 lastend_log = 0 112 lastend_log = 0
110 free_start = -1 113 free_start = -1
111 free_end = -1 114 free_end = -1
112 if start > self.get_num_cylinders(): return (-1, -1) 115 if start > self._total_sectors: return (-1, -1)
113 for part in parts: 116 for part in parts:
114 if part > 4: break 117 if part > 4: break
115 tmppart = self._partitions[part] 118 tmppart = self._partitions[part]
116 if (tmppart.get_start() > lastend_pri) and (lastend_pri >= start): 119 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
117 free_start = lastend_pri 120 free_start = lastend_pri
118 free_end = tmppart.get_start() - 1 121 free_end = tmppart.get_start() - 1
119 break 122 break
120 if tmppart.is_extended() and start < tmppart.get_end(): 123 if tmppart.is_extended() and start < tmppart.get_end():
121 lastend_log = tmppart.get_start() 124 lastend_log = tmppart.get_start()
122 for part_log in parts: 125 for part_log in parts:
123 if part_log < 5: continue 126 if part_log < 5: continue
124 tmppart_log = self._partitions[part_log] 127 tmppart_log = self._partitions[part_log]
125 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):
126 free_start = lastend_log 129 free_start = lastend_log
127 free_end = tmppart_log.get_start() - 1 130 free_end = tmppart_log.get_start() - 1
128 break 131 break
129 lastend_log = tmppart_log.get_end() + 1 132 lastend_log = tmppart_log.get_end() + 1
130 if free_start == -1 and lastend_log < tmppart.get_end(): 133 if free_start == -1 and lastend_log < tmppart.get_end():
131 free_start = lastend_log 134 free_start = lastend_log
132 free_end = tmppart.get_end() 135 free_end = tmppart.get_end()
133 break 136 break
134 lastend_pri = tmppart.get_end() + 1 137 lastend_pri = tmppart.get_end() + 1
135 if free_start == -1 and lastend_pri < self.get_num_cylinders(): 138 if free_start == -1 and lastend_pri < self._total_sectors:
136 free_start = lastend_pri 139 free_start = lastend_pri
137 free_end = self.get_num_cylinders() 140 free_end = self._total_sectors
138 return (free_start, free_end) 141 return (free_start, free_end)
139 142
140 def get_partition_at(self, cylinder, ignore_extended=1): 143 def get_partition_at(self, sector, ignore_extended=1):
141 parts = self._partitions.keys() 144 parts = self._partitions.keys()
142 parts.sort() 145 parts.sort()
143 for part in parts: 146 for part in parts:
144 tmppart = self._partitions[part] 147 tmppart = self._partitions[part]
145 if ignore_extended and tmppart.is_extended(): continue 148 if ignore_extended and tmppart.is_extended(): continue
146 if (cylinder >= tmppart.get_start()) and (cylinder <= tmppart.get_end()): 149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
147 return part 150 return part
148 return 0 151 return 0
149 152
150 def get_free_minor_at(self, start, end): 153 def get_free_minor_at(self, start, end):
151 parts = self._partitions.keys() 154 parts = self._partitions.keys()
210 213
211 def get_install_profile_structure(self): 214 def get_install_profile_structure(self):
212 devdic = {} 215 devdic = {}
213 for part in self._partitions: 216 for part in self._partitions:
214 tmppart = self._partitions[part] 217 tmppart = self._partitions[part]
215 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), 'start': tmppart.get_start(), 'end': tmppart.get_end(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format() }
216 return devdic 219 return devdic
217 220
218 def get_extended_partition(self): 221 def get_extended_partition(self):
219 for part in self._partitions: 222 for part in self._partitions:
220 tmppart = self._partitions[part] 223 tmppart = self._partitions[part]
225 def get_num_sectors(self): 228 def get_num_sectors(self):
226 return int(self._total_sectors) 229 return int(self._total_sectors)
227 230
228 def get_cylinder_size(self): 231 def get_cylinder_size(self):
229 return int(self._cylinder_bytes) 232 return int(self._cylinder_bytes)
233
234 def get_sector_size(self):
235 return int(self._sector_bytes)
230 236
231 def get_num_cylinders(self): 237 def get_num_cylinders(self):
232 return int(self._geometry['cylinders']) 238 return int(self._geometry['cylinders'])
233 239
234 def get_drive_bytes(self): 240 def get_drive_bytes(self):
284 _type = None 290 _type = None
285 _mountpoint = None 291 _mountpoint = None
286 _mountopts = None 292 _mountopts = None
287 _format = None 293 _format = None
288 _resizeable = None 294 _resizeable = None
289 _min_cylinders_for_resize = 0 295 _min_sectors_for_resize = 0
290 296
291 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 297 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False):
292 self._device = device 298 self._device = device
293 self._minor = int(minor) 299 self._minor = int(minor)
294 self._bootflag = bootflag 300 self._bootflag = bootflag
300 self._mountpoint = mountpoint 306 self._mountpoint = mountpoint
301 self._mountopts = mountopts 307 self._mountopts = mountopts
302 self._format = format 308 self._format = format
303 if blocks == 0: 309 if blocks == 0:
304 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 310 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
305# if existing: 311 if existing:
306# parted_part = device._parted_disk.get_partition(minor) 312 parted_part = device._parted_disk.get_partition(minor)
307# if type == "ntfs": 313 if type == "ntfs":
308# 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 .\+//'")) 314 min_bytes = int(commands.getoutput("ntfsresize -f --info " + device._device + str(minor) + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
309# self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 315 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1
310# self._resizeable == True 316 self._resizeable == True
311# elif type == "ext2" or type == "ext3": 317 elif type == "ext2" or type == "ext3":
312# block_size = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + " 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'")) 318 block_size = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\s\+//'"))
313# free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + " 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")) 319 free_blocks = string.strip(commands.getoutput("dumpe2fs -h " + device._device + str(minor) + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'"))
314# free_cyl = int(int(block_size) * int(free_blocks) / self._device._cylinder_bytes) 320 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes)
315# free_cyl = int(free_cyl)
316# free_cyl = free_cyl - 200 # just to be safe 321 free_sec = free_sec - 2000 # just to be safe
317# self._min_cylinders_for_resize = (self._end - self._start + 1) - free_cyl 322 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec
318# self._resizeable == True 323 self._resizeable == True
319# elif type == "fat16" or type == "fat32": 324 else:
320# parted_part = self._device._parted_disk.get_partition(self._minor) 325 parted_part = self._device._parted_disk.get_partition(self._minor)
326 try:
321# parted_fs = parted_part.geom.file_system_open() 327 parted_fs = parted_part.geom.file_system_open()
328 except:
329 self._resizeable = False
330 return
322# resize_constraint = parted_fs.get_resize_constraint() 331 resize_constraint = parted_fs.get_resize_constraint()
323# min_size = float(resize_constraint.min_size / self._device._sectors_in_cylinder) 332 min_size = resize_constraint.min_size
324# if int(min_size) != min_size: min_size = int(min_size) + 1 333 if int(min_size) != min_size: min_size = int(min_size) + 1
325# self._min_cylinders_for_resize = min_size 334 self._min_sectors_for_resize = min_size
326# self._resizeable = True 335 self._resizeable = True
327# elif type == "":
328# self._min_cylinders_for_resize = 1
329# self._resizeable = True
330# else:
331# self._resizeable = True
332 336
333 def is_extended(self): 337 def is_extended(self):
334 if self._type == "extended": 338 if self._type == "extended":
335 return True 339 return True
336 else: 340 else:
345 def get_logicals(self): 349 def get_logicals(self):
346 if not self.is_extended(): 350 if not self.is_extended():
347 return None 351 return None
348 logicals = [] 352 logicals = []
349 start = self._start 353 start = self._start
350# while not start > self._end:
351# part = self._device.get_partition_at(start)
352# print "part=" + str(part)
353# if not part: break
354# logicals.append(part)
355# start = self._device._partitions[part].get_end() + 1
356 parts = self._device._partitions.keys() 354 parts = self._device._partitions.keys()
357 parts.sort() 355 parts.sort()
358 for part in parts: 356 for part in parts:
359 if part < 5: continue 357 if part < 5: continue
360 logicals.append(part) 358 logicals.append(part)
415 return self._format 413 return self._format
416 414
417 def get_blocks(self): 415 def get_blocks(self):
418 return int(self._blocks) 416 return int(self._blocks)
419 417
420 def get_min_cylinders_for_resize(self): 418 def get_min_sectors_for_resize(self):
421 if self.is_extended(): 419 if self.is_extended():
422 min_size = self._start 420 min_size = self._start
423 for part in self._device._partitions: 421 for part in self._device._partitions:
424 if part < 5: continue 422 if part < 5: continue
425 min_size = part.get_end() 423 if part.get_end > min_size: min_size = part.get_end()
424 return min_size
426 else: 425 else:
427 return self._min_cylinders_for_resize 426 return self._min_sectors_for_resize
428 427
429 def get_max_cylinders_for_resize(self): 428 def get_max_sectors_for_resize(self):
430 free_start, free_end = self._device.get_free_space(self._end) 429 free_start, free_end = self._device.get_free_space(self._end)
431 if free_end == -1: return self._end 430 if free_end == -1: return self._end
432 if free_start - 1 == self._end: 431 if free_start - 1 == self._end:
433 if self.is_logical(): 432 if self.is_logical():
434 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 433 if free_end <= self._device._partitions[self.get_extended_parent()]._end:
435 return free_end 434 return free_end - self._start
436 else: 435 else:
437 return self._end 436 return self._end - self._start
438 else: 437 else:
439 return free_end 438 return free_end - self._start
440 439
441 def resize(self, start, end): 440 def resize(self, start, end):
442 part_at_start = self._device.get_partition_at(int(start)) 441 part_at_start = self._device.get_partition_at(int(start))
443 part_at_end = self._device.get_partition_at(int(end)) 442 part_at_end = self._device.get_partition_at(int(end))
444 logicals = None 443 logicals = None

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

  ViewVC Help
Powered by ViewVC 1.1.20