/[gli]/trunk/src/templates/x86ArchitectureTemplate.py
Gentoo

Contents of /trunk/src/templates/x86ArchitectureTemplate.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 220 - (show annotations) (download) (as text)
Fri Jan 7 07:00:13 2005 UTC (15 years, 10 months ago) by agaffney
File MIME type: text/x-python
File size: 13955 byte(s)
partition code move

1 """
2 Gentoo Linux Installer
3
4 $Id: x86ArchitectureTemplate.py,v 1.7 2005/01/07 07:00:13 agaffney Exp $
5 Copyright 2004 Gentoo Technologies Inc.
6
7
8 This fills in x86 specific functions.
9 """
10
11 import GLIUtility, string
12 from GLIArchitectureTemplate import ArchitectureTemplate
13 from GLIException import *
14
15
16 class x86ArchitectureTemplate(ArchitectureTemplate):
17 def __init__(self,configuration=None, install_profile=None, client_controller=None):
18 ArchitectureTemplate.__init__(self, configuration, install_profile, client_controller)
19 self._architecture_name = 'x86'
20 # self._install_steps = [self.stage1, self.stage2, self.stage3] um, we ain't doin this this way anymore folks.
21
22 def install_bootloader(self):
23 "Installs and configures bootloader"
24 #
25 # THIS IS ARCHITECTURE DEPENDANT!!!
26 # This is the x86 way.. it uses grub
27
28 if self._install_profile.get_boot_loader_pkg():
29 exitstatus = self._emerge(self._install_profile.get_boot_loader_pkg())
30 if exitstatus != 0:
31 raise GLIException("BootLoaderEmergeError", 'fatal', 'install_bootloader', "Could not emerge bootloader!")
32 else:
33 pass
34
35 boot_device = ""
36 boot_minor = ""
37 root_device = ""
38 root_minor = ""
39 grub_root_minor = ""
40 grub_boot_minor = ""
41 grub_boot_drive = ""
42 grub_root_drive = ""
43 minornum = 0
44 #Assign root to the root mount point to make lines more readable
45 root = self._chroot_dir
46 file_name = root + "/boot/grub/bootdevice"
47 file_name1 = root + "/boot/grub/rootdevice"
48 file_name2 = root + "/boot/grub/device.map"
49 file_name3 = root + "/boot/grub/kernel_name"
50 foundboot = False
51 #partitions = self._install_profile.get_fstab()
52 parts = self._install_profile.get_partition_tables()
53 for device in parts:
54 #in parts['/dev/hda']
55 for partition in parts[device]:
56 #print parts[device][partition]
57 mountpoint = parts[device][partition]['mountpoint']
58 if (mountpoint == "/boot"):
59 foundboot = True
60 if (( (mountpoint == "/") and (not foundboot) ) or (mountpoint == "/boot")):
61 boot_minor = str(parts[device][partition]['minor'])
62 grub_boot_minor = str(parts[device][partition]['minor'] - 1)
63 boot_device = device
64 if mountpoint == "/":
65 root_minor = str(parts[device][partition]['minor'])
66 grub_root_minor = str(parts[device][partition]['minor'] - 1)
67 root_device = device
68
69
70 #for partition in partitions:
71 #if find a /boot then stop.. else we'll take a / and overwrite with a /boot if we find it too.
72 # if (partition == "/boot"):
73 #try to get the drive LETTER from /dev/hdc1 8th character
74 # boot_minor = partitions[partition][0][8]
75 # grub_boot_minor = str(int(boot_minor) - 1)
76 # boot_device = partitions[partition][0][0:8]
77 # foundboot = True
78 # if ( (partition == "/") and (not foundboot) ):
79 # boot_minor = partitions[partition][0][8]
80 # grub_boot_minor = str(int(boot_minor) - 1)
81 # boot_device = partitions[partition][0][0:8]
82 #Foundboot IS STILL FALSE
83 # if partition == "/":
84 # root_minor = partitions[partition][0][8]
85 # grub_root_minor = str(int(root_minor) - 1)
86 # root_device = partitions[partition][0][0:8]
87
88 exitstatus0 = GLIUtility.spawn("ls -l " + boot_device + " > " + file_name)
89 exitstatus1 = GLIUtility.spawn("ls -l " + root_device + " > " + file_name1)
90 exitstatus2 = GLIUtility.spawn("echo quit | "+ root+"/sbin/grub --device-map="+file_name2)
91 exitstatus3 = GLIUtility.spawn("ls "+root+"/boot/kernel-* > "+file_name3)
92 exitstatus4 = GLIUtility.spawn("ls "+root+"/boot/initrd-* >> "+file_name3)
93 if (exitstatus0 != 0) or (exitstatus1 != 0) or (exitstatus2 != 0) or (exitstatus3 != 0) or (exitstatus4 != 0):
94 raise GLIException("BootloaderError", 'fatal', 'install_bootloader', "Error in one of THE FOUR run commands")
95
96 """
97 read the device map. sample looks like this:
98 (fd0) /dev/floppy/0
99 (hd0) /dev/ide/host2/bus0/target0/lun0/disc
100 (hd1) /dev/ide/host0/bus0/target0/lun0/disc
101 (hd2) /dev/ide/host0/bus0/target1/lun0/disc
102 """
103 e = open(file_name) #Looking for the boot device
104 ls_output = e.readlines()
105 e.close()
106 # looks like lr-xr-xr-x 1 root root 32 Oct 1 16:09 /dev/hda -> ide/host0/bus0/target0/lun0/disc
107 ls_output = ls_output[0].split(">")[-1]
108 ls_output = ls_output[1:]
109
110 eb = open(file_name1) #Looking for the root device
111 ls_outputb = eb.readlines()
112 eb.close()
113 ls_outputb = ls_outputb[0].split(">")[-1]
114 ls_outputb = ls_outputb[1:]
115
116 # Search for the key
117 f = open(file_name2)
118 file = f.readlines()
119 f.close()
120 for i in range(len(file)):
121 if file[i][11:] == ls_output:
122 #eurika we found the drivenum
123 grub_boot_drive = file[i][1:4]
124 if file[i][11:] == ls_outputb:
125 grub_root_drive = file[i][1:4]
126 if (not grub_root_drive) or (not grub_boot_drive):
127 raise GLIException("BootloaderError", 'fatal', 'install_bootloader',"Couldn't find the drive num in the list from the device.map")
128
129 g = open(file_name3)
130 kernel_name = g.readlines()
131 g.close()
132 if not kernel_name[0]:
133 raise GLIException("BootloaderError", 'fatal', 'install_bootloader',"Error: We have no kernel in /boot to put in the grub.conf file!")
134 kernel_name = map(string.strip, kernel_name)
135 kernel_name[0] = kernel_name[0].split(root)[1]
136 kernel_name[1] = kernel_name[1].split(root)[1]
137 #-------------------------------------------------------------
138 #OK, now that we have all the info, let's build that grub.conf
139 newgrubconf = ""
140 newgrubconf += "default 0\ntimeout 30\n"
141 if foundboot: #we have a /boot
142 newgrubconf += "splashimage=(" + grub_boot_drive + "," + grub_boot_minor + ")/grub/splash.xpm.gz\n"
143 else: #we have / and /boot needs to be included
144 newgrubconf += "splashimage=(" + grub_boot_drive + "," + grub_boot_minor + ")/boot/grub/splash.xpm.gz\n"
145
146 newgrubconf += "title=Gentoo Linux\n"
147 newgrubconf += "root (" + grub_boot_drive + "," + grub_boot_minor + ")\n"
148 if foundboot:
149 newgrubconf += "kernel " + kernel_name[0][5:] + " root=/dev/ram0 init=/linuxrc ramdisk=8192 real_root="
150 newgrubconf += root_device + root_minor + "\n"
151 newgrubconf += "initrd " + kernel_name[1][5:] + "\n"
152 else:
153 newgrubconf += "kernel /boot" + kernel_name[0][5:] + " root=/dev/ram0 init=/linuxrc ramdisk=8192 real_root="
154 newgrubconf += root_device + root_minor + "\n"
155 newgrubconf += "initrd /boot" + kernel_name[1][5:] + "\n"
156
157 #-------------------------------------------------------------
158 #OK, now that the file is built. Install grub.
159 #cp /proc/mounts /etc/mtab
160 #grub-install --root-directory=/boot /dev/hda
161 #shutil.copy("/proc/mounts",root +"/etc/mtab")
162 grubinstallstring = "echo -en 'root ("+grub_boot_drive + "," + grub_boot_minor + ")\n"
163 if not self._install_profile.get_boot_loader_mbr():
164 grubinstallstring +="setup ("+grub_boot_drive + "," + grub_boot_minor + ")\n"
165 else:
166 grubinstallstring +="setup ("+grub_boot_drive+")\n"
167 grubinstallstring += "quit\n' | "+root+"/sbin/grub"
168 print grubinstallstring
169 exitstatus = GLIUtility.spawn(grubinstallstring,chroot=self._chroot_dir)
170 if exitstatus != 0:
171 raise GLIException("GrubInstallError", 'fatal', 'install_bootloader', "Could not install grub!")
172
173 #now make the grub.conf file
174 file_name = root + "/boot/grub/grub.conf"
175 try:
176 shutil.move(file_name, file_name + ".OLDdefault")
177 except:
178 pass
179 f = open(file_name, 'w')
180 f.writelines(newgrubconf)
181 f.close()
182
183 def _cylinders_to_sectors(self, minor, start, end, sectors_in_cylinder):
184 cylinders = int(end) - int(start) + 1
185 total_sectors = cylinders * int(sectors_in_cylinder)
186 start_sector = int(start) * sectors_in_cylinder
187 end_sector = start_sector + total_sectors - 1
188 if int(minor) == 1 and start_sector == 0: start_sector = 63
189 return (start_sector, end_sector)
190
191 def _sectors_to_megabytes(self, sectors, sector_bytes=512):
192 return float((float(sectors) * sector_bytes)/ float(1024*1024))
193
194 def _sectors_to_bytes(self, sectors, sector_bytes=512):
195 return (int(sectors) * sector_bytes)
196
197 def _run_parted_command(self, device, cmd):
198 parted_output = commands.getoutput("parted -s " + device + " " + cmd)
199 print "parted -s " + device + " " + cmd
200
201 def _add_partition(self, device, start, end, type, fs):
202 start = self._sectors_to_megabytes(start)
203 end = self._sectors_to_megabytes(end)
204 self._run_parted_command(device, "mkpart " + type + " " + fs + " " + str(start) + " " + str(end))
205 if type == "ntfs":
206 pass
207 elif type == "ext2" or type == "ext3":
208 pass
209 else:
210 pass
211
212 def do_partitioning(self):
213 import GLIStorageDevice, parted, pprint
214
215 devices_old = {}
216 parts_old = {}
217 parts_new = self._install_profile.get_partition_tables()
218 drives = GLIStorageDevice.detect_devices()
219 drives.sort()
220 for drive in drives:
221 devices_old[drive] = GLIStorageDevice.Device(drive)
222 devices_old[drive].set_partitions_from_disk()
223 for part in devices_old.keys(): parts_old[part] = devices_old[part].get_install_profile_structure()
224
225 pp = pprint.PrettyPrinter(indent=4)
226 pp.pprint(parts_old)
227 pp.pprint(parts_new)
228
229 for dev in parts_old.keys():
230 parts_active = []
231 parts_lba = []
232 print "\nProcessing " + dev + "..."
233 parted_dev = parted.PedDevice.get(dev)
234 parted_disk = parted.PedDisk.new(parted_dev)
235 last_partition_touched = 0
236 sectors_in_cylinder = devices_old[dev]._sectors_in_cylinder
237 # First pass to delete old partitions that aren't resized
238 for part in parts_old[dev]:
239 oldpart = parts_old[dev][part]
240 old_start, old_end = self._cylinders_to_sectors(part, oldpart['start'], oldpart['end'], sectors_in_cylinder)
241 matchingminor = 0
242 for new_part in parts_new[dev]:
243 tmppart = parts_new[dev][new_part]
244 new_start, new_end = self._cylinders_to_sectors(new_part, tmppart['start'], tmppart['end'], sectors_in_cylinder)
245 if int(tmppart['start']) == int(oldpart['start']) and tmppart['format'] == False and tmppart['type'] == oldpart['type'] and int(tmppart['end']) == int(oldpart['end']):
246 matchingminor = new_part
247 print " Deleting old minor " + str(part) + " to be recreated later"
248 self._run_parted_command(dev, "rm " + str(part))
249 break
250 if int(tmppart['start']) == int(oldpart['start']) and tmppart['format'] == False and tmppart['type'] == oldpart['type'] and int(tmppart['end']) != int(oldpart['end']):
251 matchingminor = new_part
252 print " Ignoring old minor " + str(part) + " to resize later"
253 break
254 if not matchingminor:
255 print " No match found...deleting partition " + str(part)
256 self._run_parted_command(dev, "rm " + str(part))
257 else:
258 if parted_disk.get_partition(part).get_flag(1): # Active/boot
259 print " Partition " + str(part) + " was active...noted"
260 parts_active.append(int(matchingminor))
261 if parted_disk.get_partition(part).get_flag(7): # LBA
262 print " Partition " + str(part) + " was LBA...noted"
263 parts_lba.append(int(matchingminor))
264 # Second pass to resize old partitions that need to be resized
265 print " Second pass..."
266 for part in parts_old[dev]:
267 oldpart = parts_old[dev][part]
268 old_start, old_end = self._cylinders_to_sectors(part, oldpart['start'], oldpart['end'], sectors_in_cylinder)
269 for new_part in parts_new[dev]:
270 tmppart = parts_new[dev][new_part]
271 new_start, new_end = self._cylinders_to_sectors(new_part, tmppart['start'], tmppart['end'], sectors_in_cylinder)
272 if int(tmppart['start']) == int(oldpart['start']) and tmppart['format'] == False and tmppart['type'] == oldpart['type'] and int(tmppart['end']) != int(oldpart['end']):
273 print " Resizing old minor " + str(part) + " from " + str(oldpart['start']) + "-" + str(oldpart['end'])+ " to " + str(tmppart['start']) + "-" + str(tmppart['end'])
274 type = tmppart['type']
275 device = dev
276 minor = part
277 start = int(new_start)
278 end = int(new_end)
279 if type == "ext2" or type == "ext3":
280 total_sectors = end - start + 1
281 commands.getstatus("resize2fs " + device + str(minor) + " " + str(total_sectors) + "s")
282 print "resize2fs " + device + str(minor) + " " + str(total_sectors) + "s"
283 elif type == "ntfs":
284 total_sectors = end - start + 1
285 total_bytes = int(self._sectors_to_bytes(total_sectors))
286 commands.getstatus("ntfsresize --size " + str(total_bytes) + " " + device + str(minor))
287 print "ntfsresize --size " + str(total_bytes) + " " + device + str(minor)
288 else:
289 start = float(self._sectors_to_megabytes(start))
290 end = float(self._sectors_to_megabytes(end))
291 self._run_parted_command(device, "resize " + str(minor) + " " + str(start) + " " + str(end))
292 print " Deleting old minor " + str(part) + " to be recreated in 3rd pass"
293 self._run_parted_command(dev, "rm " + str(part))
294 break
295 # Third pass to create new partition table
296 print " Third pass..."
297 for part in parts_new[dev]:
298 newpart = parts_new[dev][part]
299 new_start, new_end = self._cylinders_to_sectors(part, newpart['start'], newpart['end'], sectors_in_cylinder)
300 if newpart['type'] == "extended":
301 print " Adding extended partition from " + str(newpart['start']) + " to " + str(newpart['end'])
302 self._add_partition(dev, new_start, new_end, "extended", "")
303 elif int(part) < 5:
304 print " Adding primary partition from " + str(newpart['start']) + " to " + str(newpart['end'])
305 self._add_partition(dev, new_start, new_end, "primary", newpart['type'])
306 elif int(part) > 4:
307 print " Adding logical partition from " + str(newpart['start']) + " to " + str(newpart['end'])
308 self._add_partition(dev, new_start, new_end, "logical", newpart['type'])
309 if int(part) in parts_active and not newpart['format']:
310 print " Partition was previously active...setting"
311 self._run_parted_command(dev, "set " + str(part) + " boot on")
312 if int(part) in parts_lba and not newpart['format']:
313 print " Partition was previously LBA...setting"
314 self._run_parted_command(dev, "set " + str(part) + " lba on")

Properties

Name Value
svn:eol-style native

  ViewVC Help
Powered by ViewVC 1.1.20