lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | """ |
| 3 | Simple script to setup unattended installs on KVM guests. |
| 4 | """ |
| 5 | # -*- coding: utf-8 -*- |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 6 | import os, sys, shutil, tempfile, re, ConfigParser, glob, inspect |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 7 | import common |
| 8 | |
| 9 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 10 | SCRIPT_DIR = os.path.dirname(sys.modules[__name__].__file__) |
| 11 | KVM_TEST_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "..")) |
| 12 | |
| 13 | |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 14 | class SetupError(Exception): |
| 15 | """ |
| 16 | Simple wrapper for the builtin Exception class. |
| 17 | """ |
| 18 | pass |
| 19 | |
| 20 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 21 | def find_command(cmd): |
| 22 | """ |
| 23 | Searches for a command on common paths, error if it can't find it. |
| 24 | |
| 25 | @param cmd: Command to be found. |
| 26 | """ |
| 27 | for dir in ["/usr/local/sbin", "/usr/local/bin", |
| 28 | "/usr/sbin", "/usr/bin", "/sbin", "/bin"]: |
| 29 | file = os.path.join(dir, cmd) |
| 30 | if os.path.exists(file): |
| 31 | return file |
| 32 | raise ValueError('Missing command: %s' % cmd) |
| 33 | |
| 34 | |
| 35 | def run(cmd, info=None): |
| 36 | """ |
| 37 | Run a command and throw an exception if it fails. |
| 38 | Optionally, you can provide additional contextual info. |
| 39 | |
| 40 | @param cmd: Command string. |
| 41 | @param reason: Optional string that explains the context of the failure. |
| 42 | |
| 43 | @raise: SetupError if command fails. |
| 44 | """ |
| 45 | print "Running '%s'" % cmd |
| 46 | cmd_name = cmd.split(' ')[0] |
| 47 | find_command(cmd_name) |
| 48 | if os.system(cmd): |
| 49 | e_msg = 'Command failed: %s' % cmd |
| 50 | if info is not None: |
| 51 | e_msg += '. %s' % info |
| 52 | raise SetupError(e_msg) |
| 53 | |
| 54 | |
| 55 | def cleanup(dir): |
| 56 | """ |
| 57 | If dir is a mountpoint, do what is possible to unmount it. Afterwards, |
| 58 | try to remove it. |
| 59 | |
| 60 | @param dir: Directory to be cleaned up. |
| 61 | """ |
| 62 | print "Cleaning up directory %s" % dir |
| 63 | if os.path.ismount(dir): |
| 64 | os.system('fuser -k %s' % dir) |
| 65 | run('umount %s' % dir, info='Could not unmount %s' % dir) |
| 66 | if os.path.isdir(dir): |
| 67 | shutil.rmtree(dir) |
| 68 | |
| 69 | |
| 70 | def clean_old_image(image): |
| 71 | """ |
| 72 | Clean a leftover image file from previous processes. If it contains a |
| 73 | mounted file system, do the proper cleanup procedures. |
| 74 | |
| 75 | @param image: Path to image to be cleaned up. |
| 76 | """ |
| 77 | if os.path.exists(image): |
| 78 | mtab = open('/etc/mtab', 'r') |
| 79 | mtab_contents = mtab.read() |
| 80 | mtab.close() |
| 81 | if image in mtab_contents: |
| 82 | os.system('fuser -k %s' % image) |
| 83 | os.system('umount %s' % image) |
| 84 | os.remove(image) |
| 85 | |
| 86 | |
| 87 | class Disk(object): |
| 88 | """ |
| 89 | Abstract class for Disk objects, with the common methods implemented. |
| 90 | """ |
| 91 | def __init__(self): |
| 92 | self.path = None |
| 93 | |
| 94 | |
| 95 | def setup_answer_file(self, filename, contents): |
| 96 | answer_file = open(os.path.join(self.mount, filename), 'w') |
| 97 | answer_file.write(contents) |
| 98 | answer_file.close() |
| 99 | |
| 100 | |
| 101 | def copy_to(self, src): |
| 102 | dst = os.path.join(self.mount, os.path.basename(src)) |
| 103 | if os.path.isdir(src): |
| 104 | shutil.copytree(src, dst) |
| 105 | elif os.path.isfile(src): |
| 106 | shutil.copyfile(src, dst) |
| 107 | |
| 108 | |
| 109 | def close(self): |
| 110 | os.chmod(self.path, 0755) |
| 111 | cleanup(self.mount) |
| 112 | print "Disk %s successfuly set" % self.path |
| 113 | |
| 114 | |
| 115 | class FloppyDisk(Disk): |
| 116 | """ |
| 117 | Represents a 1.44 MB floppy disk. We can copy files to it, and setup it in |
| 118 | convenient ways. |
| 119 | """ |
| 120 | def __init__(self, path): |
| 121 | print "Creating floppy unattended image %s" % path |
| 122 | try: |
| 123 | qemu_img_binary = os.environ['KVM_TEST_qemu_img_binary'] |
| 124 | except KeyError: |
| 125 | qemu_img_binary = os.path.join(KVM_TEST_DIR, qemu_img_binary) |
| 126 | if not os.path.exists(qemu_img_binary): |
| 127 | raise SetupError('The qemu-img binary that is supposed to be used ' |
| 128 | '(%s) does not exist. Please verify your ' |
| 129 | 'configuration' % qemu_img_binary) |
| 130 | |
| 131 | self.mount = tempfile.mkdtemp(prefix='floppy_', dir='/tmp') |
| 132 | self.virtio_mount = None |
| 133 | self.path = path |
| 134 | clean_old_image(path) |
| 135 | if not os.path.isdir(os.path.dirname(path)): |
| 136 | os.makedirs(os.path.dirname(path)) |
| 137 | |
| 138 | try: |
| 139 | c_cmd = '%s create -f raw %s 1440k' % (qemu_img_binary, path) |
| 140 | run(c_cmd, info='Could not create floppy image') |
| 141 | f_cmd = 'mkfs.msdos -s 1 %s' % path |
| 142 | run(f_cmd, info='Error formatting floppy image') |
| 143 | m_cmd = 'mount -o loop,rw %s %s' % (path, self.mount) |
| 144 | run(m_cmd, info='Could not mount floppy image') |
| 145 | except: |
| 146 | cleanup(self.mount) |
| 147 | |
| 148 | |
| 149 | def _copy_virtio_drivers(self, virtio_floppy): |
| 150 | """ |
| 151 | Copy the virtio drivers on the virtio floppy to the install floppy. |
| 152 | |
| 153 | 1) Mount the floppy containing the viostor drivers |
| 154 | 2) Copy its contents to the root of the install floppy |
| 155 | """ |
| 156 | virtio_mount = tempfile.mkdtemp(prefix='virtio_floppy_', dir='/tmp') |
| 157 | |
| 158 | pwd = os.getcwd() |
| 159 | try: |
| 160 | m_cmd = 'mount -o loop %s %s' % (virtio_floppy, virtio_mount) |
| 161 | run(m_cmd, info='Could not mount virtio floppy driver') |
| 162 | os.chdir(virtio_mount) |
| 163 | path_list = glob.glob('*') |
| 164 | for path in path_list: |
| 165 | self.copy_to(path) |
| 166 | finally: |
| 167 | os.chdir(pwd) |
| 168 | cleanup(virtio_mount) |
| 169 | |
| 170 | |
| 171 | def setup_virtio_win2003(self, virtio_floppy, virtio_oemsetup_id): |
| 172 | """ |
| 173 | Setup the install floppy with the virtio storage drivers, win2003 style. |
| 174 | |
| 175 | Win2003 and WinXP depend on the file txtsetup.oem file to install |
| 176 | the virtio drivers from the floppy, which is a .ini file. |
| 177 | Process: |
| 178 | |
| 179 | 1) Copy the virtio drivers on the virtio floppy to the install floppy |
| 180 | 2) Parse the ini file with config parser |
| 181 | 3) Modify the identifier of the default session that is going to be |
| 182 | executed on the config parser object |
| 183 | 4) Re-write the config file to the disk |
| 184 | """ |
| 185 | self._copy_virtio_drivers(virtio_floppy) |
| 186 | txtsetup_oem = os.path.join(self.mount, 'txtsetup.oem') |
| 187 | if not os.path.isfile(txtsetup_oem): |
| 188 | raise SetupError('File txtsetup.oem not found on the install ' |
| 189 | 'floppy. Please verify if your floppy virtio ' |
| 190 | 'driver image has this file') |
| 191 | parser = ConfigParser.ConfigParser() |
| 192 | parser.read(txtsetup_oem) |
| 193 | if not parser.has_section('Defaults'): |
| 194 | raise SetupError('File txtsetup.oem does not have the session ' |
| 195 | '"Defaults". Please check txtsetup.oem') |
| 196 | default_driver = parser.get('Defaults', 'SCSI') |
| 197 | if default_driver != virtio_oemsetup_id: |
| 198 | parser.set('Defaults', 'SCSI', virtio_oemsetup_id) |
| 199 | fp = open(txtsetup_oem, 'w') |
| 200 | parser.write(fp) |
| 201 | fp.close() |
| 202 | |
| 203 | |
| 204 | def setup_virtio_win2008(self, virtio_floppy): |
| 205 | """ |
| 206 | Setup the install floppy with the virtio storage drivers, win2008 style. |
| 207 | |
| 208 | Win2008, Vista and 7 require people to point out the path to the drivers |
| 209 | on the unattended file, so we just need to copy the drivers to the |
| 210 | driver floppy disk. |
| 211 | Process: |
| 212 | |
| 213 | 1) Copy the virtio drivers on the virtio floppy to the install floppy |
| 214 | """ |
| 215 | self._copy_virtio_drivers(virtio_floppy) |
| 216 | |
| 217 | |
| 218 | class CdromDisk(Disk): |
| 219 | """ |
| 220 | Represents a CDROM disk that we can master according to our needs. |
| 221 | """ |
| 222 | def __init__(self, path): |
| 223 | print "Creating ISO unattended image %s" % path |
| 224 | self.mount = tempfile.mkdtemp(prefix='cdrom_unattended_', dir='/tmp') |
| 225 | self.path = path |
| 226 | clean_old_image(path) |
| 227 | if not os.path.isdir(os.path.dirname(path)): |
| 228 | os.makedirs(os.path.dirname(path)) |
| 229 | |
| 230 | |
| 231 | def close(self): |
| 232 | g_cmd = ('mkisofs -o %s -max-iso9660-filenames ' |
| 233 | '-relaxed-filenames -D --input-charset iso8859-1 ' |
| 234 | '%s' % (self.path, self.mount)) |
| 235 | run(g_cmd, info='Could not generate iso with answer file') |
| 236 | |
| 237 | os.chmod(self.path, 0755) |
| 238 | cleanup(self.mount) |
| 239 | print "Disk %s successfuly set" % self.path |
| 240 | |
| 241 | |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 242 | class UnattendedInstall(object): |
| 243 | """ |
| 244 | Creates a floppy disk image that will contain a config file for unattended |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 245 | OS install. The parameters to the script are retrieved from environment |
| 246 | variables. |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 247 | """ |
| 248 | def __init__(self): |
| 249 | """ |
| 250 | Gets params from environment variables and sets class attributes. |
| 251 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 252 | images_dir = os.path.join(KVM_TEST_DIR, 'images') |
| 253 | self.deps_dir = os.path.join(KVM_TEST_DIR, 'deps') |
| 254 | self.unattended_dir = os.path.join(KVM_TEST_DIR, 'unattended') |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 255 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 256 | attributes = ['kernel_args', 'finish_program', 'cdrom_cd1', |
| 257 | 'unattended_file', 'medium', 'url', 'kernel', 'initrd', |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 258 | 'nfs_server', 'nfs_dir', 'install_virtio', 'floppy', |
| 259 | 'cdrom_unattended', 'boot_path', 'extra_params'] |
| 260 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 261 | for a in attributes: |
| 262 | self._setattr(a) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 263 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 264 | if self.install_virtio == 'yes': |
| 265 | v_attributes = ['virtio_floppy', 'virtio_storage_path', |
| 266 | 'virtio_network_path', 'virtio_oemsetup_id', |
| 267 | 'virtio_network_installer'] |
| 268 | for va in v_attributes: |
| 269 | self._setattr(va) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 270 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 271 | if self.cdrom_cd1: |
| 272 | self.cdrom_cd1 = os.path.join(KVM_TEST_DIR, self.cdrom_cd1) |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 273 | self.cdrom_cd1_mount = tempfile.mkdtemp(prefix='cdrom_cd1_', dir='/tmp') |
| 274 | if self.medium == 'nfs': |
| 275 | self.nfs_mount = tempfile.mkdtemp(prefix='nfs_', dir='/tmp') |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 276 | |
Eric Li | e0493a4 | 2010-11-15 13:05:43 -0800 | [diff] [blame] | 277 | if self.floppy: |
| 278 | self.floppy = os.path.join(KVM_TEST_DIR, self.floppy) |
| 279 | if not os.path.isdir(os.path.dirname(self.floppy)): |
| 280 | os.makedirs(os.path.dirname(self.floppy)) |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 281 | |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 282 | self.image_path = os.path.dirname(self.kernel) |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 283 | |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 284 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 285 | def _setattr(self, key): |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 286 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 287 | Populate class attributes with contents of environment variables. |
| 288 | |
| 289 | Example: KVM_TEST_medium will populate self.medium. |
| 290 | |
| 291 | @param key: Name of the class attribute we desire to have. |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 292 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 293 | env_name = 'KVM_TEST_%s' % key |
| 294 | value = os.environ.get(env_name, '') |
| 295 | setattr(self, key, value) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 296 | |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 297 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 298 | def render_answer_file(self): |
| 299 | # Replace KVM_TEST_CDKEY (in the unattended file) with the cdkey |
| 300 | # provided for this test and replace the KVM_TEST_MEDIUM with |
| 301 | # the tree url or nfs address provided for this test. |
| 302 | unattended_contents = open(self.unattended_file).read() |
| 303 | dummy_cdkey_re = r'\bKVM_TEST_CDKEY\b' |
| 304 | real_cdkey = os.environ.get('KVM_TEST_cdkey') |
| 305 | if re.search(dummy_cdkey_re, unattended_contents): |
| 306 | if real_cdkey: |
| 307 | unattended_contents = re.sub(dummy_cdkey_re, real_cdkey, |
| 308 | unattended_contents) |
Eric Li | 25fc6d1 | 2010-09-28 17:22:51 -0700 | [diff] [blame] | 309 | else: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 310 | print ("WARNING: 'cdkey' required but not specified for " |
| 311 | "this unattended installation") |
Eric Li | 25fc6d1 | 2010-09-28 17:22:51 -0700 | [diff] [blame] | 312 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 313 | dummy_medium_re = r'\bKVM_TEST_MEDIUM\b' |
| 314 | if self.medium == "cdrom": |
| 315 | content = "cdrom" |
| 316 | elif self.medium == "url": |
| 317 | content = "url --url %s" % self.url |
| 318 | elif self.medium == "nfs": |
| 319 | content = "nfs --server=%s --dir=%s" % (self.nfs_server, |
| 320 | self.nfs_dir) |
| 321 | else: |
| 322 | raise SetupError("Unexpected installation medium %s" % self.url) |
Eric Li | 25fc6d1 | 2010-09-28 17:22:51 -0700 | [diff] [blame] | 323 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 324 | unattended_contents = re.sub(dummy_medium_re, content, |
| 325 | unattended_contents) |
Benson Leung | 517d95a | 2010-09-28 18:00:17 -0700 | [diff] [blame] | 326 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 327 | def replace_virtio_key(contents, dummy_re, env): |
| 328 | """ |
| 329 | Replace a virtio dummy string with contents. |
Benson Leung | 517d95a | 2010-09-28 18:00:17 -0700 | [diff] [blame] | 330 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 331 | If install_virtio is not set, replace it with a dummy string. |
Benson Leung | 517d95a | 2010-09-28 18:00:17 -0700 | [diff] [blame] | 332 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 333 | @param contents: Contents of the unattended file |
| 334 | @param dummy_re: Regular expression used to search on the. |
| 335 | unattended file contents. |
| 336 | @param env: Name of the environment variable. |
| 337 | """ |
| 338 | dummy_path = "C:" |
| 339 | driver = os.environ.get(env, '') |
Benson Leung | 517d95a | 2010-09-28 18:00:17 -0700 | [diff] [blame] | 340 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 341 | if re.search(dummy_re, contents): |
| 342 | if self.install_virtio == "yes": |
| 343 | if driver.endswith("msi"): |
| 344 | driver = 'msiexec /passive /package ' + driver |
| 345 | else: |
| 346 | try: |
| 347 | # Let's escape windows style paths properly |
| 348 | drive, path = driver.split(":") |
| 349 | driver = drive + ":" + re.escape(path) |
| 350 | except: |
| 351 | pass |
| 352 | contents = re.sub(dummy_re, driver, contents) |
| 353 | else: |
| 354 | contents = re.sub(dummy_re, dummy_path, contents) |
| 355 | return contents |
| 356 | |
| 357 | vdict = {r'\bKVM_TEST_STORAGE_DRIVER_PATH\b': |
| 358 | 'KVM_TEST_virtio_storage_path', |
| 359 | r'\bKVM_TEST_NETWORK_DRIVER_PATH\b': |
| 360 | 'KVM_TEST_virtio_network_path', |
| 361 | r'\bKVM_TEST_VIRTIO_NETWORK_INSTALLER\b': |
| 362 | 'KVM_TEST_virtio_network_installer_path'} |
| 363 | |
| 364 | for vkey in vdict: |
| 365 | unattended_contents = replace_virtio_key(unattended_contents, |
| 366 | vkey, vdict[vkey]) |
| 367 | |
| 368 | print "Unattended install contents:" |
| 369 | print unattended_contents |
| 370 | return unattended_contents |
| 371 | |
| 372 | |
| 373 | def setup_boot_disk(self): |
| 374 | answer_contents = self.render_answer_file() |
| 375 | |
| 376 | if self.unattended_file.endswith('.sif'): |
| 377 | dest_fname = 'winnt.sif' |
| 378 | setup_file = 'winnt.bat' |
| 379 | boot_disk = FloppyDisk(self.floppy) |
| 380 | boot_disk.setup_answer_file(dest_fname, answer_contents) |
| 381 | setup_file_path = os.path.join(self.unattended_dir, setup_file) |
| 382 | boot_disk.copy_to(setup_file_path) |
| 383 | if self.install_virtio == "yes": |
| 384 | boot_disk.setup_virtio_win2003(self.virtio_floppy, |
| 385 | self.virtio_oemsetup_id) |
| 386 | boot_disk.copy_to(self.finish_program) |
| 387 | |
| 388 | elif self.unattended_file.endswith('.ks'): |
| 389 | # Red Hat kickstart install |
| 390 | dest_fname = 'ks.cfg' |
| 391 | if self.cdrom_unattended: |
| 392 | boot_disk = CdromDisk(self.cdrom_unattended) |
| 393 | elif self.floppy: |
| 394 | boot_disk = FloppyDisk(self.floppy) |
| 395 | else: |
| 396 | raise SetupError("Neither cdrom_unattended nor floppy set " |
| 397 | "on the config file, please verify") |
| 398 | boot_disk.setup_answer_file(dest_fname, answer_contents) |
| 399 | |
| 400 | elif self.unattended_file.endswith('.xml'): |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 401 | if "autoyast" in self.extra_params: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 402 | # SUSE autoyast install |
| 403 | dest_fname = "autoinst.xml" |
| 404 | if self.cdrom_unattended: |
| 405 | boot_disk = CdromDisk(self.cdrom_unattended) |
| 406 | elif self.floppy: |
| 407 | boot_disk = FloppyDisk(self.floppy) |
| 408 | else: |
| 409 | raise SetupError("Neither cdrom_unattended nor floppy set " |
| 410 | "on the config file, please verify") |
| 411 | boot_disk.setup_answer_file(dest_fname, answer_contents) |
| 412 | |
| 413 | else: |
| 414 | # Windows unattended install |
| 415 | dest_fname = "autounattend.xml" |
| 416 | boot_disk = FloppyDisk(self.floppy) |
| 417 | boot_disk.setup_answer_file(dest_fname, answer_contents) |
| 418 | if self.install_virtio == "yes": |
| 419 | boot_disk.setup_virtio_win2008(self.virtio_floppy) |
| 420 | boot_disk.copy_to(self.finish_program) |
| 421 | |
| 422 | else: |
| 423 | raise SetupError('Unknown answer file %s' % |
| 424 | self.unattended_file) |
| 425 | |
| 426 | boot_disk.close() |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 427 | |
| 428 | |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 429 | def setup_cdrom(self): |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 430 | """ |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 431 | Mount cdrom and copy vmlinuz and initrd.img. |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 432 | """ |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 433 | print "Copying vmlinuz and initrd.img from cdrom" |
| 434 | m_cmd = ('mount -t iso9660 -v -o loop,ro %s %s' % |
| 435 | (self.cdrom_cd1, self.cdrom_cd1_mount)) |
| 436 | run(m_cmd, info='Could not mount CD image %s.' % self.cdrom_cd1) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 437 | |
lmr | b010c7e | 2010-02-24 11:54:30 +0000 | [diff] [blame] | 438 | try: |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 439 | img_path_cmd = ("mkdir -p %s" % self.image_path) |
| 440 | run(img_path_cmd, info=("Could not create image path dir %s" % |
| 441 | self.image_path)) |
| 442 | kernel_fetch_cmd = ("cp %s/%s/%s %s" % |
| 443 | (self.cdrom_cd1_mount, self.boot_path, |
| 444 | os.path.basename(self.kernel), self.kernel)) |
| 445 | run(kernel_fetch_cmd, info=("Could not copy the vmlinuz from %s" % |
| 446 | self.cdrom_cd1_mount)) |
| 447 | initrd_fetch_cmd = ("cp %s/%s/%s %s" % |
| 448 | (self.cdrom_cd1_mount, self.boot_path, |
| 449 | os.path.basename(self.initrd), self.initrd)) |
| 450 | run(initrd_fetch_cmd, info=("Could not copy the initrd.img from " |
| 451 | "%s" % self.cdrom_cd1_mount)) |
lmr | b010c7e | 2010-02-24 11:54:30 +0000 | [diff] [blame] | 452 | finally: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 453 | cleanup(self.cdrom_cd1_mount) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 454 | |
lmr | b010c7e | 2010-02-24 11:54:30 +0000 | [diff] [blame] | 455 | |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 456 | def setup_url(self): |
| 457 | """ |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 458 | Download the vmlinuz and initrd.img from URL. |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 459 | """ |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 460 | print "Downloading vmlinuz and initrd.img from URL" |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 461 | os.chdir(self.image_path) |
| 462 | |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 463 | kernel_fetch_cmd = "wget -q %s/%s/%s" % (self.url, self.boot_path, |
| 464 | os.path.basename(self.kernel)) |
| 465 | initrd_fetch_cmd = "wget -q %s/%s/%s" % (self.url, self.boot_path, |
| 466 | os.path.basename(self.initrd)) |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 467 | |
| 468 | if os.path.exists(self.kernel): |
| 469 | os.unlink(self.kernel) |
| 470 | if os.path.exists(self.initrd): |
| 471 | os.unlink(self.initrd) |
| 472 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 473 | run(kernel_fetch_cmd, info="Could not fetch vmlinuz from %s" % self.url) |
| 474 | run(initrd_fetch_cmd, info=("Could not fetch initrd.img from %s" % |
| 475 | self.url)) |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 476 | |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 477 | |
| 478 | def setup_nfs(self): |
| 479 | """ |
| 480 | Copy the vmlinuz and initrd.img from nfs. |
| 481 | """ |
| 482 | print "Copying the vmlinuz and initrd.img from nfs" |
| 483 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 484 | m_cmd = ("mount %s:%s %s -o ro" % |
| 485 | (self.nfs_server, self.nfs_dir, self.nfs_mount)) |
| 486 | run(m_cmd, info='Could not mount nfs server') |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 487 | |
| 488 | try: |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 489 | kernel_fetch_cmd = ("cp %s/%s/%s %s" % |
| 490 | (self.nfs_mount, self.boot_path, |
| 491 | os.path.basename(self.kernel), self.image_path)) |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 492 | run(kernel_fetch_cmd, info=("Could not copy the vmlinuz from %s" % |
| 493 | self.nfs_mount)) |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 494 | initrd_fetch_cmd = ("cp %s/%s/%s %s" % |
| 495 | (self.nfs_mount, self.boot_path, |
| 496 | os.path.basename(self.initrd), self.image_path)) |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 497 | run(initrd_fetch_cmd, info=("Could not copy the initrd.img from " |
| 498 | "%s" % self.nfs_mount)) |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 499 | finally: |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 500 | cleanup(self.nfs_mount) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 501 | |
| 502 | |
| 503 | def setup(self): |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 504 | """ |
| 505 | Configure the environment for unattended install. |
| 506 | |
| 507 | Uses an appropriate strategy according to each install model. |
| 508 | """ |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 509 | print "Starting unattended install setup" |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 510 | print |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 511 | |
| 512 | print "Variables set:" |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 513 | for member in inspect.getmembers(self): |
| 514 | name, value = member |
| 515 | attribute = getattr(self, name) |
| 516 | if not (name.startswith("__") or callable(attribute) or not value): |
| 517 | print " %s: %s" % (name, value) |
| 518 | print |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 519 | |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 520 | if self.unattended_file and (self.floppy or self.cdrom_unattended): |
| 521 | self.setup_boot_disk() |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 522 | if self.medium == "cdrom": |
Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 523 | if self.kernel and self.initrd: |
| 524 | self.setup_cdrom() |
lmr | ee1e40f | 2010-06-10 15:32:27 +0000 | [diff] [blame] | 525 | elif self.medium == "url": |
| 526 | self.setup_url() |
| 527 | elif self.medium == "nfs": |
| 528 | self.setup_nfs() |
| 529 | else: |
| 530 | raise SetupError("Unexpected installation method %s" % |
Eric Li | 6f27d4f | 2010-09-29 10:55:17 -0700 | [diff] [blame] | 531 | self.medium) |
lmr | 5d73e2f | 2009-10-09 20:46:36 +0000 | [diff] [blame] | 532 | print "Unattended install setup finished successfuly" |
| 533 | |
| 534 | |
| 535 | if __name__ == "__main__": |
| 536 | os_install = UnattendedInstall() |
| 537 | os_install.setup() |