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

Diff of /trunk/src/GLIStorageDevice.py

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

Revision 313 Revision 639
1import commands, string, re, os, parted 1import commands, string, os, parted
2from GLIException import *
3import GLIUtility
2 4
5MEGABYTE = 1024 * 1024
6
7# these are here so that we can change them easily in future
8# the values were chosen to represent perfect floating point representations
9FREE_MINOR_FRAC_PRI = 1.0/32.0
10FREE_MINOR_FRAC_LOG = 1.0/8.0
11
12archinfo = { 'sparc': { 'fixedparts': [ { 'minor': 3, 'type': "wholedisk" } ], 'disklabel': 'sun', 'extended': False },
13 'hppa': { 'fixedparts': [ { 'minor': 1, 'type': "boot" } ], 'disklabel': 'msdos', 'extended': False },
14 'x86': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
15 'amd64': { 'fixedparts': [], 'disklabel': 'msdos', 'extended': True },
16 'ppc': { 'fixedparts': [ { 'minor': 1, 'type': "metadata" } ], 'disklabel': 'mac', 'extended': False }
17 }
18
19##
20# This class provides a partitioning abstraction for the frontends
3class Device: 21class Device:
4 "Class representing a partitionable device." 22 "Class representing a partitionable device."
5 23
6 _device = None 24 _device = None
7 _partitions = None 25 _partitions = None
11 _cylinder_bytes = 0 29 _cylinder_bytes = 0
12 _sectors_in_cylinder = 0 30 _sectors_in_cylinder = 0
13 _parted_dev = None 31 _parted_dev = None
14 _parted_disk = None 32 _parted_disk = None
15 _sector_bytes = 0 33 _sector_bytes = 0
16 34 _total_mb = 0
17 def __init__(self, device): 35 _arch = None
36 _disklabel = None
37
38 ##
39 # Initialization function for GLIStorageDevice class
40 # @param device Device node (e.g. /dev/hda) of device being represented
41 # @param arch="x86" Architecture that we're partition for (defaults to 'x86' for now)
42 def __init__(self, device, arch="x86", set_geometry=True):
18 self._device = device 43 self._device = device
19 self._partitions = {} 44 self._partitions = {}
20 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512} 45 self._geometry = {'cylinders': 0, 'heads': 0, 'sectors': 0, 'sectorsize': 512}
21 self._total_bytes = 0 46 self._total_bytes = 0
22 self._cylinder_bytes = 0 47 self._cylinder_bytes = 0
48 self._arch = arch
23 self._parted_dev = parted.PedDevice.get(self._device) 49 self._parted_dev = parted.PedDevice.get(self._device)
50 try:
24 self._parted_disk = parted.PedDisk.new(self._parted_dev) 51 self._parted_disk = parted.PedDisk.new(self._parted_dev)
52 except:
53 self._parted_disk = self._parted_dev.disk_new_fresh(parted.disk_type_get(archinfo[self._arch]['disklabel']))
54 self._disklabel = self._parted_disk.type.name
55 if set_geometry:
25 self.set_disk_geometry_from_disk() 56 self.set_disk_geometry_from_disk()
26 57
58 ##
59 # Sets disk geometry info from disk. This function is used internally by __init__()
27 def set_disk_geometry_from_disk(self): 60 def set_disk_geometry_from_disk(self):
28 self._total_bytes = self._parted_dev.length * self._parted_dev.sector_size 61 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 62 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 63 self._sector_bytes = self._parted_dev.sector_size
41 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes 64 self._cylinder_bytes = self._geometry['heads'] * self._geometry['sectors'] * self._sector_bytes
42# self._total_sectors = self._geometry['cylinders'] * self._geometry['heads'] * self._geometry['sectors']
43 self._total_sectors = self._parted_dev.length 65 self._total_sectors = self._parted_dev.length
44 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors'] 66 self._sectors_in_cylinder = self._geometry['heads'] * self._geometry['sectors']
67 self._total_mb = long(self._total_bytes / MEGABYTE)
45 68
69 ##
70 # Sets partition info from disk.
46 def set_partitions_from_disk(self): 71 def set_partitions_from_disk(self):
72 last_part = 0
73 last_log_part = 4
47 parted_part = self._parted_disk.next_partition() 74 parted_part = self._parted_disk.next_partition()
48 while parted_part != None: 75 while parted_part:
76 part_mb = long((parted_part.geom.end - parted_part.geom.start + 1) * self._sector_bytes / MEGABYTE)
49 if parted_part.num < 1: 77 if parted_part.num >= 1:
50 parted_part = self._parted_disk.next_partition(parted_part)
51 continue
52 fs_type = "" 78 fs_type = ""
53 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name 79 if parted_part.fs_type != None: fs_type = parted_part.fs_type.name
54 if parted_part.type == 2: fs_type = "extended" 80 if parted_part.type == 2: fs_type = "extended"
81 if archinfo[self._arch]['extended'] and parted_part.num > 4:
82 last_log_part = parted_part.num
83 else:
84 last_part = parted_part.num
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) 85 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)
86 elif parted_part.type_name == "free":
87 parent_part = self.get_partition_at(parted_part.geom.start, ignore_extended=0)
88 if parent_part:
89 self._partitions[last_log_part+FREE_MINOR_FRAC_LOG] = Partition(self, last_log_part+FREE_MINOR_FRAC_LOG, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
90 last_log_part += 1
91 else:
92 self._partitions[last_part+FREE_MINOR_FRAC_PRI] = Partition(self, last_part+FREE_MINOR_FRAC_PRI, part_mb, parted_part.geom.start, parted_part.geom.end, "free", format=False, existing=False)
93 last_part += 1
56 parted_part = self._parted_disk.next_partition(parted_part) 94 parted_part = self._parted_disk.next_partition(parted_part)
57 95
96 ##
97 # Imports partition info from the install profile partition structure
98 # @param ips Parameter structure returned from install_profile.get_partition_tables()
58 def set_partitions_from_install_profile_structure(self, ips): 99 def set_partitions_from_install_profile_structure(self, ips):
59 for part in ips: 100 for part in ips:
60 tmppart = ips[part] 101 tmppart = ips[part]
61 existing = False 102 existing = False
62 parted_part = self._parted_disk.get_partition(part) 103 if tmppart['origminor'] and not tmppart['format']:
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 104 existing = True
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'])) 105 self._partitions[tmppart['minor']] = Partition(self, tmppart['minor'], tmppart['mb'], tmppart['start'], tmppart['end'], tmppart['type'], format=tmppart['format'], origminor=tmppart['origminor'], existing=existing, mountpoint=tmppart['mountpoint'], mountopts=tmppart['mountopts'], mkfsopts=tmppart['mkfsopts'])
72 106
107 ##
108 # Returns name of device (e.g. /dev/hda) being represented
73 def get_device(self): 109 def get_device(self):
74 return self._device 110 return self._device
75 111
112 ##
113 # Combines free space and closes gaps in minor numbers. This is used internally
76 def clear_partitions(self): 114 def tidy_partitions(self):
77 self._partitions = {} 115 last_minor = 0
116 last_log_minor = 4
117 last_free = 0
118 last_log_free = 0
119 parts = self._partitions.keys()
120 parts.sort()
121 for part in parts:
122 if archinfo[self._arch]['extended'] and part > 4: break
123 tmppart = self._partitions[part]
124 if tmppart.get_type() == "extended":
125 for part_log in parts:
126 if part_log < (4 + FREE_MINOR_FRAC_LOG): continue
127 tmppart_log = self._partitions[part_log]
128 if tmppart_log.get_type() == "free":
129 if last_log_minor < last_log_free:
130 self._partitions[last_log_free].set_mb(self._partitions[last_log_free].get_mb()+tmppart_log.get_mb())
131 del self._partitions[part_log]
132 else:
133 if not last_log_free:
134 last_log_free = last_log_minor + FREE_MINOR_FRAC_LOG
135 else:
136 last_log_free = part_log
137 tmppart_log.set_minor(last_log_free)
138 self._partitions[last_log_free] = tmppart_log
139 if part_log != last_log_free: del self._partitions[part_log]
140 else:
141 if part_log > (last_log_minor + 1):
142 tmppart_log.set_minor(last_log_minor + 1)
143 last_log_minor = last_log_minor + 1
144 self._partitions[last_log_minor] = tmppart_log
145 del self._partitions[part_log]
146 else:
147 last_log_minor = part_log
148 elif tmppart.get_type() == "free":
149 if last_minor < last_free:
150 self._partitions[last_free].set_mb(self._partitions[last_free].get_mb()+tmppart.get_mb())
151 del self._partitions[part]
152 else:
153 if not last_free:
154 last_free = last_minor + FREE_MINOR_FRAC_PRI
155 else:
156 last_free = part
157 tmppart.set_minor(last_free)
158 self._partitions[last_free] = tmppart
159 if part != last_free: del self._partitions[part]
160 else:
161 if part > (last_minor + 1):
162 tmppart.set_minor(last_minor + 1)
163 last_minor = last_minor + 1
164 self._partitions[last_minor] = tmppart
165 del self._partitions[part]
166 else:
167 last_minor = part
78 168
169 ##
170 # Adds a new partition to the partition info
171 # @param free_minor minor of unallocated space partition is being created in
172 # @param mb size of partition in MB
173 # @param start Start sector (only used for existing partitions)
174 # @param end End sector (only used for existing partitions)
175 # @param type Partition type (ext2, ext3, fat32, linux-swap, free, extended, etc.)
176 # @param mountpoint='' Partition mountpoint
177 # @param mountopts='' Partition mount options
178 # @param mkfsopts='' Additional mkfs options
79 def add_partition(self, minor, start, end, type, mountpoint='', mountopts=''): 179 def add_partition(self, free_minor, mb, start, end, type, mountpoint='', mountopts='',mkfsopts=''):
80 free_start, free_end = self.get_free_space(start)
81 minor = int(minor) 180 new_minor = int(free_minor) + 1
82 if not free_end:
83 return False
84 if self._partitions.has_key(minor): 181 if self._partitions.has_key(new_minor):
85 parts = self._partitions.keys() 182 parts = self._partitions.keys()
86 parts.sort() 183 parts.sort()
87 parts.reverse() 184 parts.reverse()
88 hole_at = 0 185 hole_at = 0
89 for i in range(1, parts[0]+1): 186 for i in range(1, parts[0]+1):
90 if i <= minor: continue 187 if i <= new_minor: continue
91 if not self._partitions.has_key(int(i)): 188 if not self._partitions.has_key(i):
92 hole_at = i 189 hole_at = i
93 break 190 break
94 stopscooting = 0 191 stopscooting = 0
95 for i in parts: 192 for i in parts:
96 if stopscooting: break 193 if stopscooting: break
97 if (i >= hole_at) and (hole_at): continue 194 if i >= hole_at and hole_at: continue
98 if i >= minor: 195 if i >= new_minor:
99 self._partitions[i].set_minor(i+1) 196 self._partitions[i].set_minor(i+1)
100 self._partitions[i+1] = self._partitions[i] 197 self._partitions[i+1] = self._partitions[i]
101 if i == minor: stopscooting = 1 198 if i == new_minor: stopscooting = 1
199 if mb != self._partitions[free_minor].get_mb():
200 old_free_mb = self._partitions[free_minor].get_mb()
201 del self._partitions[free_minor]
202 if archinfo[self._arch]['extended'] and new_minor >= 5:
203 free_minor = new_minor + FREE_MINOR_FRAC_LOG
204 else:
205 free_minor = new_minor + FREE_MINOR_FRAC_PRI
206 self._partitions[free_minor] = Partition(self, free_minor, old_free_mb-mb, 0, 0, "free")
207 else:
208 del self._partitions[free_minor]
102 self._partitions[minor] = Partition(self, minor, '', start, end, 0, type, mountpoint=mountpoint, mountopts=mountopts) 209 self._partitions[new_minor] = Partition(self, new_minor, mb, start, end, type, mountpoint=mountpoint, mountopts=mountopts,mkfsopts=mkfsopts)
210 if type == "extended":
211 self._partitions[4 + FREE_MINOR_FRAC_LOG] = Partition(self, (4 + FREE_MINOR_FRAC_LOG), mb, 0, 0, "free")
212 self.tidy_partitions()
103 213
214 ##
215 # Removes partition from partition info
216 # @param minor Minor of partition to remove
104 def remove_partition(self, minor): 217 def remove_partition(self, minor):
218 tmppart = self._partitions[int(minor)]
219 free_minor = 0
220 if tmppart.is_logical():
221 free_minor = int(minor-1)+FREE_MINOR_FRAC_LOG
222 else:
223 free_minor = int(minor-1)+FREE_MINOR_FRAC_PRI
224 if free_minor in self._partitions:
225 self._partitions[free_minor].set_mb(self._partitions[free_minor].get_mb() + tmppart.get_mb())
226 else:
227 self._partitions[free_minor] = Partition(self, free_minor, tmppart.get_mb(), 0, 0, "free", format=False, existing=False)
105 del self._partitions[int(minor)] 228 del self._partitions[int(minor)]
229 self.tidy_partitions()
106 230
107 def get_free_space(self, start): 231 ##
108 GAP_SIZE = 100 232 # Returns an ordered list (disk order) of partition minors
233 def get_ordered_partition_list(self):
109 parts = self._partitions.keys() 234 parts = self._partitions.keys()
110 parts.sort() 235 parts.sort()
111 lastend_pri = 0 236 partlist = []
112 lastend_log = 0 237 tmppart = None
113 free_start = -1
114 free_end = -1
115 if start > self._total_sectors: return (-1, -1)
116 for part in parts: 238 for part in parts:
117 if part > 4: break 239 if archinfo[self._arch]['extended'] and part > (4 + FREE_MINOR_FRAC_PRI): break
118 tmppart = self._partitions[part] 240 tmppart = self._partitions[part]
119 if (tmppart.get_start() > (lastend_pri + GAP_SIZE)) and (lastend_pri >= start):
120 free_start = lastend_pri
121 free_end = tmppart.get_start() - 1
122 break
123 if tmppart.is_extended() and start < tmppart.get_end():
124 lastend_log = tmppart.get_start()
125 for part_log in parts:
126 if part_log < 5: continue
127 tmppart_log = self._partitions[part_log]
128 if (tmppart_log.get_start() > (lastend_log + GAP_SIZE)) and (lastend_log >= start):
129 free_start = lastend_log
130 free_end = tmppart_log.get_start() - 1
131 break
132 lastend_log = tmppart_log.get_end() + 1
133 if free_start == -1 and lastend_log < tmppart.get_end():
134 free_start = lastend_log
135 free_end = tmppart.get_end()
136 break
137 lastend_pri = tmppart.get_end() + 1
138 if free_start == -1 and lastend_pri < self._total_sectors:
139 free_start = lastend_pri
140 free_end = self._total_sectors
141 return (free_start, free_end)
142
143 def get_partition_at(self, sector, ignore_extended=1):
144 parts = self._partitions.keys()
145 parts.sort()
146 for part in parts:
147 tmppart = self._partitions[part]
148 if ignore_extended and tmppart.is_extended(): continue
149 if (sector >= tmppart.get_start()) and (sector <= tmppart.get_end()):
150 return part
151 return 0
152
153 def get_free_minor_at(self, start, end):
154 parts = self._partitions.keys()
155 parts.sort()
156 minor = 1
157 lastpart = 0
158 for part in parts:
159 if part > 4: break
160 tmppart = self._partitions[part]
161 if end < tmppart.get_start():
162 minor = part
163 if (minor - 1) > lastpart: minor = lastpart + 1
164 break
165 if tmppart.is_extended() and start < tmppart.get_end():
166 minor = 5
167 lastpart = 4
168 for part_log in parts:
169 if part_log < 5: continue
170 tmppart_log = self._partitions[part_log]
171 if end < tmppart_log.get_start():
172 minor = part_log
173 if (minor - 1) > lastpart: minor = lastpart + 1
174 break
175 minor = part_log + 1
176 lastpart = part_log
177 break
178 minor = part + 1
179 lastpart = part
180 return minor
181
182 def get_ordered_partition_list(self):
183 parts = self._partitions.keys()
184 parts.sort()
185 partlist = []
186 free_start, free_end = self.get_free_space(0)
187 tmppart = None
188 tmppart_log = None
189 for part in parts:
190 if part > 4: break
191 tmppart = self._partitions[part]
192 if free_end < tmppart.get_start() and not free_start == -1:
193 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
194 free_start, free_end = self.get_free_space(free_end)
195 newitem = self._device + str(part) + ": " + str(tmppart.get_start()) + "-" + str(tmppart.get_end())
196 if tmppart.is_extended(): newitem = newitem + " extended"
197 partlist.append(newitem) 241 partlist.append(part)
198 if tmppart.is_extended(): 242 if tmppart.is_extended():
199 for part_log in parts: 243 for part_log in parts:
200 if part_log < 5: continue 244 if part_log < (4 + FREE_MINOR_FRAC_LOG): continue
201 tmppart_log = self._partitions[part_log]
202 if free_end < tmppart_log.get_start() and free_end <= tmppart.get_end() and not free_start == -1:
203 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
204 free_start, free_end = self.get_free_space(free_end)
205 newitem = self._device + str(part_log) + ": " + str(tmppart_log.get_start()) + "-" + str(tmppart_log.get_end()) + " logical"
206 partlist.append(newitem) 245 partlist.append(part_log)
207 if ((tmppart_log == None) or (free_start > tmppart_log.get_end())) and free_start < tmppart.get_end():
208 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ") logical")
209 free_start, free_end = self.get_free_space(free_end)
210 if (tmppart == None) or (free_start > tmppart.get_end()):
211 partlist.append("Free Space (" + str(free_start) + "-" + str(free_end) + ")")
212 return partlist 246 return partlist
213 247
248 ##
249 # Returns partition info in a format suitable for passing to install_profile.set_partition_tables()
214 def get_install_profile_structure(self): 250 def get_install_profile_structure(self):
215 devdic = {} 251 devdic = {}
216 for part in self._partitions: 252 for part in self._partitions:
217 tmppart = self._partitions[part] 253 tmppart = self._partitions[part]
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() } 254 devdic[part] = { 'mb': tmppart.get_mb(), 'minor': float(part), 'origminor': tmppart.get_orig_minor(), 'type': tmppart.get_type(), 'mountpoint': tmppart.get_mountpoint(), 'mountopts': tmppart.get_mountopts(), 'format': tmppart.get_format(), 'mkfsopts': tmppart.get_mkfsopts(), 'start': 0, 'end': 0 }
219 return devdic 255 return devdic
220 256
257 ##
258 # Returns the minor of the extended partition, if any
221 def get_extended_partition(self): 259 def get_extended_partition(self):
222 for part in self._partitions: 260 for part in self._partitions:
223 tmppart = self._partitions[part] 261 tmppart = self._partitions[part]
224 if tmppart.is_extended(): 262 if tmppart.is_extended():
225 return part 263 return part
226 return 0 264 return 0
227 265
266 ##
267 # Returns the drive model
268 def get_model(self):
269 return self._parted_dev.model
270
271 ##
272 # Sets the disklabel type
273 def set_disklabel(self, disklabel):
274 self._disklabel = disklabel
275
276 ##
277 # Returns the disklabel type
278 def get_disklabel(self):
279 return self._disklabel
280
281 ##
282 # Returns the number of sectors on the device
228 def get_num_sectors(self): 283 def get_num_sectors(self):
229 return int(self._total_sectors) 284 return long(self._total_sectors)
230 285
286 ##
287 # Returns the size of a cylinder in bytes
231 def get_cylinder_size(self): 288 def get_cylinder_size(self):
232 return int(self._cylinder_bytes) 289 return long(self._cylinder_bytes)
233 290
291 ##
292 # Returns the size of a sector in bytes
234 def get_sector_size(self): 293 def get_sector_size(self):
235 return int(self._sector_bytes) 294 return long(self._sector_bytes)
236 295
296 ##
297 # Returns the number of cylinders
237 def get_num_cylinders(self): 298 def get_num_cylinders(self):
238 return int(self._geometry['cylinders']) 299 return long(self._geometry['cylinders'])
239 300
301 ##
302 # Returns the total number of bytes on the device
240 def get_drive_bytes(self): 303 def get_drive_bytes(self):
241 return int(self._total_bytes) 304 return long(self._total_bytes)
242 305
306 ##
307 # Returns the total number of MB on the device
308 def get_total_mb(self):
309 return self._total_mb
310
311 ##
312 # Returns partition info dictionary
243 def get_partitions(self): 313 def get_partitions(self):
244 return self._partitions 314 return self._partitions
245 315
246 def print_partitions(self): 316 ##
247 for part in self._partitions.keys(): 317 # Prints disk geometry to STDOUT (no longer used)
248 print self._partitions[part].return_info()
249
250 def print_geometry(self): 318 def print_geometry(self):
251 print self._total_bytes, self._geometry 319 print self._total_bytes, self._geometry
252 320
253 def _error(self, message): 321##
254 "Raises an exception" 322# This class represents a partition within a GLIStorageDevice object
255 raise "DeviceObjectError", message
256
257 def _run(self, cmd):
258 "Runs a command and returns the output"
259
260 # Run command
261 output_string = commands.getoutput(cmd)
262
263 # What we will return
264 output_list = []
265
266 # As long as there is a new line in the output_string
267 while output_string.find("\n") != -1:
268
269 # Find the \n in the string
270 index = output_string.find("\n") + 1
271
272 # Add the line to the output and remove it from
273 # the output_string
274 output_list.append(output_string[:index])
275 output_string = output_string[index:]
276
277 # return output
278 return output_list
279
280
281class Partition: 323class Partition:
282 "Class representing a single partition within a Device object" 324 "Class representing a single partition within a Device object"
283 325
284 _device = None 326 _device = None
285 _minor = None 327 _minor = 0
286 _bootflag = None 328 _orig_minor = 0
287 _start = None 329 _start = 0
288 _end = None 330 _end = 0
289 _blocks = None
290 _type = None 331 _type = None
291 _mountpoint = None 332 _mountpoint = None
292 _mountopts = None 333 _mountopts = None
293 _format = None 334 _format = None
294 _resizeable = None 335 _resizeable = None
295 _min_sectors_for_resize = 0 336 _min_mb_for_resize = 0
337 _mb = 0
338 _mkfsopts = None
296 339
340 ##
341 # Initialization function for the Partition class
342 # @param device Parent GLIStorageDevice object
343 # @param minor Minor of partition
344 # @param mb Parameter Size of partition in MB
345 # @param start Parameter Start sector of partition
346 # @param end Parameter Start sector of partition
347 # @param type Parameter Type of partition (ext2, ext3, fat32, linux-swap, free, extended, etc.)
348 # @param mountpoint='' Mountpoint of partition
349 # @param mountopts='' Mount options of partition
350 # @param mkfsopts='' Additional mkfs options
351 # @param format=True Format partition
352 # @param existing=False This partition exists on disk
297 def __init__(self, device, minor, bootflag, start, end, blocks, type, mountpoint='', mountopts='', format=True, existing=False): 353 def __init__(self, device, minor, mb, start, end, type, mountpoint='', mountopts='', format=True, existing=False, origminor=0, mkfsopts=''):
298 self._device = device 354 self._device = device
299 self._minor = int(minor) 355 self._minor = float(minor)
300 self._bootflag = bootflag
301 self._start = int(start) 356 self._start = long(start)
302 self._end = int(end) 357 self._end = long(end)
303 self._blocks = int(blocks)
304 if type == "": type = "unknown" 358 self._type = type or "unknown"
305 self._type = type
306 self._mountpoint = mountpoint 359 self._mountpoint = mountpoint
307 self._mountopts = mountopts 360 self._mountopts = mountopts
308 self._format = format 361 self._format = format
309 if blocks == 0: 362 self._mb = mb
310 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512 363 self._orig_minor = origminor
364 self._mkfsopts = mkfsopts
365 if type != "free":
366 if existing and not origminor:
367 self._orig_minor = self._minor
368 self._minor = int(self._minor)
369 self._orig_minor = int(self._orig_minor)
311 if existing: 370 if existing:
312 parted_part = device._parted_disk.get_partition(minor) 371 parted_part = device._parted_disk.get_partition(self._orig_minor)
372 label_type = device._parted_disk.type.name
373 if label_type == "loop":
374 dev_node = device._device
375 else:
376 dev_node = device._device + str(self._orig_minor)
377# print "dev_node = " + dev_node
313 if type == "ntfs": 378 if type == "ntfs":
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 .\+//'")) 379 min_bytes = long(commands.getoutput("ntfsresize -f --info " + dev_node + " | grep -e '^You might resize' | sed -e 's/You might resize at //' -e 's/ bytes or .\+//'"))
315 self._min_cylinders_for_resize = int(min_bytes / self._device._cylinder_bytes) + 1 380 self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
316 self._resizeable == True 381 self._resizeable = True
317 elif type == "ext2" or type == "ext3": 382 elif type == "ext2" or type == "ext3":
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\+//'")) 383 block_size = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Block size:' | sed -e 's/^Block size:\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\+//'")) 384 free_blocks = long(string.strip(commands.getoutput("dumpe2fs -h " + dev_node + r" 2>&1 | grep -e '^Free blocks:' | sed -e 's/^Free blocks:\s\+//'")))
320 free_sec = int(int(block_size) * int(free_blocks) / self._device._sector_bytes) 385 free_bytes = long(block_size * free_blocks)
321 free_sec = free_sec - 2000 # just to be safe 386 # can't hurt to pad (the +50) it a bit since this is really just a guess
322 self._min_sectors_for_resize = (self._end - self._start + 1) - free_sec 387 self._min_mb_for_resize = self._mb - long(free_bytes / MEGABYTE) + 50
323 self._resizeable == True 388 self._resizeable = True
324 else: 389 else:
325 parted_part = self._device._parted_disk.get_partition(self._minor) 390 parted_part = self._device._parted_disk.get_partition(int(self._orig_minor))
326 try: 391 try:
327 parted_fs = parted_part.geom.file_system_open() 392 parted_fs = parted_part.geom.file_system_open()
328 except: 393 except:
329 self._resizeable = False 394 self._resizeable = False
330 return 395 return
331 resize_constraint = parted_fs.get_resize_constraint() 396 resize_constraint = parted_fs.get_resize_constraint()
332 min_size = resize_constraint.min_size 397 min_bytes = resize_constraint.min_size * self._device._sector_bytes
333 if int(min_size) != min_size: min_size = int(min_size) + 1 398 self._min_mb_for_resize = long(min_bytes / MEGABYTE) + 1
334 self._min_sectors_for_resize = min_size
335 self._resizeable = True 399 self._resizeable = True
336 400
401 ##
402 # Returns whether or not the partition is extended
337 def is_extended(self): 403 def is_extended(self):
338 if self._type == "extended": 404 if self._type == "extended":
339 return True 405 return True
340 else: 406 else:
341 return False 407 return False
342 408
409 ##
410 # Returns whether or not the partition is logical
343 def is_logical(self): 411 def is_logical(self):
344 part = self._device.get_partition_at(self._start, ignore_extended=0) 412 if self._type == "free":
345 if part and self._device._partitions[part].is_extended() and not part == self._minor: 413 if int(self._minor) + FREE_MINOR_FRAC_LOG == self._minor:
414 return True
415 else:
416 return False
417 elif archinfo[self._device._arch]['extended'] and self._minor > 4:
346 return True 418 return True
419 else:
347 return False 420 return False
348 421
422 ##
423 # Returns a list of logical partitions if this is an extended partition
349 def get_logicals(self): 424 def get_logicals(self):
350 if not self.is_extended(): 425 if not self.is_extended():
351 return None 426 return None
352 logicals = [] 427 logicals = []
353 start = self._start
354 parts = self._device._partitions.keys() 428 parts = self._device._partitions.keys()
355 parts.sort() 429 parts.sort()
356 for part in parts: 430 for part in parts:
357 if part < 5: continue 431 if part < 5: continue
358 logicals.append(part) 432 logicals.append(part)
359 logicals.sort() 433 logicals.sort()
360 return logicals 434 return logicals
361 435
436 ##
437 # Returns the extened parent partition if this is a logical partition (no longer used)
362 def get_extended_parent(self): 438 def get_extended_parent(self):
363 if not self.is_logical(): 439 if not self.is_logical():
364 return None 440 return None
365 else: 441 else:
366 return self._device.get_partition_at(self._start, ignore_extended=0) 442 return self._device.get_partition_at(self._start, ignore_extended=0)
367 443
444 ##
445 # Sets the options passed to mkfs
446 # @param mkfsopts Options passed to mkfs
447 def set_mkfsopts(self, mkfsopts):
448 self._mkfsopts = mkfsopts
449
450 ##
451 # Returns the options passes to mkfs
452 def get_mkfsopts(self):
453 return self._mkfsopts
454
455 ##
456 # Sets the start sector for the partition
457 # @param start Start sector
368 def set_start(self, start): 458 def set_start(self, start):
369 self._start = int(start) 459 self._start = long(start)
370 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
371 460
461 ##
462 # Returns the start sector for the partition
372 def get_start(self): 463 def get_start(self):
373 return int(self._start) 464 return long(self._start)
374 465
466 ##
467 # Sets the end sector of the partition
468 # @param end End sector
375 def set_end(self, end): 469 def set_end(self, end):
376 self._end = int(end) 470 self._end = long(end)
377 self._blocks = ((self._end - self._start) * self._device.get_cylinder_size()) / 512
378 471
472 ##
473 # Returns end sector for the partition
379 def get_end(self): 474 def get_end(self):
380 return int(self._end) 475 return long(self._end)
381 476
477 ##
478 # Returns size of partition in MB
479 def get_mb(self):
480 return long(self._mb)
481
482 ##
483 # Sets size of partition in MB
484 # @param mb Parameter description
485 def set_mb(self, mb):
486 self._mb = long(mb)
487
488 ##
489 # Sets type of partition
490 # @param type Parameter description
382 def set_type(self, type): 491 def set_type(self, type):
383 self._type = type 492 self._type = type
384 493
494 ##
495 # Returns type of partition
385 def get_type(self): 496 def get_type(self):
386 return self._type 497 return self._type
387 498
499 ##
500 # Returns parent GLIStorageDevice object
388 def get_device(self): 501 def get_device(self):
389 return self._device 502 return self._device
390 503
504 ##
505 # Sets minor of partition
506 # @param minor New minor
391 def set_minor(self, minor): 507 def set_minor(self, minor):
392 self._minor = int(minor) 508 self._minor = float(minor)
393 509
510 ##
511 # Returns minor of partition
394 def get_minor(self): 512 def get_minor(self):
395 return int(self._minor) 513 return float(self._minor)
396 514
515 ##
516 # Sets the original minor of the partition
517 # @param orig_minor Parameter description
518 def set_orig_minor(self, orig_minor):
519 self._orig_minor = int(orig_minor)
520
521 ##
522 # Returns the original minor of the partition
523 def get_orig_minor(self):
524 return self._orig_minor
525
526 ##
527 # Sets the mountpoint for the partition
528 # @param mountpoint Mountpoint
397 def set_mountpoint(self, mountpoint): 529 def set_mountpoint(self, mountpoint):
398 self._mountpoint = mountpoint 530 self._mountpoint = mountpoint
399 531
532 ##
533 # Returns the mountpoint for the partition
400 def get_mountpoint(self): 534 def get_mountpoint(self):
401 return self._mountpoint 535 return self._mountpoint
402 536
537 ##
538 # Sets the mount options for the partition
539 # @param mountopts Mount options
403 def set_mountopts(self, mountopts): 540 def set_mountopts(self, mountopts):
404 self._mountopts = mountopts 541 self._mountopts = mountopts
405 542
543 ##
544 # Returns the mount options for the partition
406 def get_mountopts(self): 545 def get_mountopts(self):
407 return self._mountopts 546 return self._mountopts
408 547
548 ##
549 # Set whether to format the partition
550 # @param format Format this partition (True/False)
409 def set_format(self, format): 551 def set_format(self, format):
410 self._format = format 552 self._format = format
411 553
554 ##
555 # Returns whether to format the partition
412 def get_format(self): 556 def get_format(self):
413 return self._format 557 return self._format
414 558
415 def get_blocks(self): 559 ##
416 return int(self._blocks) 560 # Returns whether the partition is resizeable
561 def is_resizeable(self):
562 return self._resizeable
417 563
564 ##
565 # Returns minimum MB for resize
418 def get_min_sectors_for_resize(self): 566 def get_min_mb_for_resize(self):
419 if self.is_extended(): 567# if self.is_extended():
420 min_size = self._start 568# min_size = self._start
421 for part in self._device._partitions: 569# for part in self._device._partitions:
422 if part < 5: continue 570# if part < 5: continue
423 if part.get_end > min_size: min_size = part.get_end() 571# if part.get_end > min_size: min_size = part.get_end()
424 return min_size 572# return min_size
425 else: 573# else:
574 if self._resizeable:
426 return self._min_sectors_for_resize 575 return self._min_mb_for_resize
576 else:
577 return -1
427 578
579 ##
580 # Returns maximum MB for resize
428 def get_max_sectors_for_resize(self): 581 def get_max_mb_for_resize(self):
429 free_start, free_end = self._device.get_free_space(self._end) 582 if self._resizeable:
430 if free_end == -1: return self._end 583 free_minor = 0
431 if free_start - 1 == self._end:
432 if self.is_logical(): 584 if self.is_logical():
433 if free_end <= self._device._partitions[self.get_extended_parent()]._end: 585 free_minor = self._minor + FREE_MINOR_FRAC_LOG
434 return free_end - self._start
435 else:
436 return self._end - self._start
437 else: 586 else:
438 return free_end - self._start 587 free_minor = self._minor + FREE_MINOR_FRAC_PRI
588 if free_minor in self._device._partitions:
589 return self._mb + self._device._partitions[free_minor]._mb
590 else:
591 return self._mb
592 else:
593 return -1
439 594
595 ##
596 # Resizes the partition
597 # @param mb New size in MB
440 def resize(self, start, end): 598 def resize(self, mb):
441 part_at_start = self._device.get_partition_at(int(start)) 599 free_minor = self._minor
442 part_at_end = self._device.get_partition_at(int(end))
443 logicals = None
444 if self.is_logical(): 600 if self.is_logical():
445 parent = self.get_extended_parent() 601 free_minor += FREE_MINOR_FRAC_LOG
446 parentstart = int(self._device._partitions[parent].get_start()) 602 else:
447 parentend = int(self._device._partitions[parent].get_end()) 603 free_minor += FREE_MINOR_FRAC_PRI
448 if (start < parentstart) or (end > parentend): return 0 604 if mb < self._mb:
449 if self.is_extended(): 605 # Shrinking
450 logicals = self.get_logicals() 606 if not free_minor in self._device._partitions:
451 if len(logicals): 607 self._device._partitions[free_minor] = Partition(self._device, free_minor, 0, 0, 0, "free", format=False, existing=False)
452 logicals_start = self._device._partitions[logicals[0]].get_start() 608 self._device._partitions[free_minor]._mb += self._mb - mb
453 logicals_end = self._device._partitions[logicals[len(logicals)-1]].get_end() 609 self._mb = mb
454 if (start > logicals_start) or (end < logicals_end): return 0 610 elif mb == self._mb + self._device._partitions[free_minor]._mb:
455 if part_at_start in logicals: part_at_start = 0 611 # Using all available unallocated space
456 if part_at_end in logicals: part_at_end = 0 612 del self._device._partitions[free_minor]
457 if ((not part_at_start == 0) and (part_at_start != self._minor)) or ((not part_at_end == 0) and (part_at_end != self._minor)): 613 self._mb = mb
458 return 0 614 elif mb > self._mb:
459 self.set_start(start) 615 # Growing
460 self.set_end(end) 616 self._device._partitions[free_minor]._mb = mb - self._mb
461 return 1 617 self._mb = mb
462 618
463 def _error(self, message): 619##
464 "Raises an exception" 620# Returns a list of detected partitionable devices
465 raise "PartitionObjectError", message
466
467
468def detect_devices(): 621def detect_devices():
469 "Returns a list of partitionable devices on the system"
470
471 devices = [] 622 devices = []
472 623
473 # Make sure sysfs exists 624 # Make sure sysfs exists
474 if not os.path.exists("/sys/bus"): 625 if not os.path.exists("/sys/bus"):
475 raise Exception, "no sysfs found (you MUST use a kernel >2.6)" 626 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "no sysfs found (you MUST use a kernel >2.6)")
476 # Make sure /proc/partitions exists 627 # Make sure /proc/partitions exists
477 if not os.path.exists("/proc/partitions"): 628 if not os.path.exists("/proc/partitions"):
478 raise Exception, "/proc/partitions does not exist!" 629 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "/proc/partitions does not exist! Please make sure procfs is in your kernel and mounted!")
479 630
480 # Load /proc/partitions into the variable 'partitions' 631 # Load /proc/partitions into the variable 'partitions'
481 partitions = [] 632 partitions = []
482 for line in open("/proc/partitions"): 633 for line in open("/proc/partitions"):
483 if len(line.split()) < 4 or not line.split()[0].isdigit() or \ 634 if len(line.split()) < 4 or not line.split()[0].isdigit() or not line.split()[1].isdigit():
484 not line.split()[1].isdigit():
485 continue 635 continue
486 636
487 # Get the major, minor and device name 637 # Get the major, minor and device name
488 major = line.split()[0] 638 major = line.split()[0]
489 minor = line.split()[1] 639 minor = line.split()[1]
499 # all the devices in /dev to try and find a 649 # all the devices in /dev to try and find a
500 # devices with the same major and minor 650 # devices with the same major and minor
501 if not os.path.exists(device): 651 if not os.path.exists(device):
502 device = None 652 device = None
503 for path, dirs, files in os.walk("/dev"): 653 for path, dirs, files in os.walk("/dev"):
504 for file in files: 654 for d_file in files:
505 full_file = os.path.join(path, file) 655 full_file = os.path.join(path, d_file)
506 if not os.path.exists(full_file): 656 if not os.path.exists(full_file):
507 continue 657 continue
508 statres = os.stat(full_file) 658 statres = os.stat(full_file)
509 fmaj = os.major(statres.st_rdev) 659 fmaj = os.major(statres.st_rdev)
510 fmin = os.minor(statres.st_rdev) 660 fmin = os.minor(statres.st_rdev)
522 if os.path.exists("/sys/bus/" + dev_type): 672 if os.path.exists("/sys/bus/" + dev_type):
523 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices") 673 sysfs_devices = os.listdir("/sys/bus/"+dev_type+"/devices")
524 674
525 # For each device in the devices on that bus 675 # For each device in the devices on that bus
526 for sysfs_device in sysfs_devices: 676 for sysfs_device in sysfs_devices:
527 dev_file = "/sys/bus/" + dev_type + "/devices/"\ 677 dev_file = "/sys/bus/" + dev_type + "/devices/" + sysfs_device + "/block/dev"
528 + sysfs_device + "/block/dev"
529 678
530 # If the file is not a block device, loop 679 # If the file is not a block device, loop
531 if not os.path.exists(dev_file): 680 if not os.path.exists(dev_file):
532 continue 681 continue
533 682
535 try: 684 try:
536 major, minor = open(dev_file).read().split(":") 685 major, minor = open(dev_file).read().split(":")
537 major = int(major) 686 major = int(major)
538 minor = int(minor) 687 minor = int(minor)
539 except: 688 except:
540 raise Exception, "invalid major minor in "\ 689 raise GLIException("GLIStorageDeviceError", 'fatal', 'detect_devices', "invalid major minor in " + dev_file)
541 + dev_file
542 690
543 # Find a device listed in /proc/partitions 691 # Find a device listed in /proc/partitions
544 # that has the same minor and major as our 692 # that has the same minor and major as our
545 # current block device. 693 # current block device.
546 for record in partitions: 694 for record in partitions:
547 if major == record[0] and minor == record[1]: 695 if major == record[0] and minor == record[1]:
548 devices.append(record[2]) 696 devices.append(record[2])
549 697
698 # For testing the partitioning code
699 if GLIUtility.is_file("/tmp/disk.img"):
700 devices.append("/tmp/disk.img")
701
550 # We have assembled the list of devices, so return it 702 # We have assembled the list of devices, so return it
551 return devices 703 return devices

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

  ViewVC Help
Powered by ViewVC 1.1.20