~heber013/ubuntu-system-tests/fixing-vm-provisioning

« back to all changes in this revision

Viewing changes to ubuntu_system_tests/host/qemu.py

  • Committer: Heber Parrucci
  • Date: 2018-10-02 13:06:51 UTC
  • Revision ID: heber.parrucci@canonical.com-20181002130651-i9k1sfjy5fw7yxj9
Fixing VM provisioning to work with multipart initrd

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#
21
21
 
22
22
import os
 
23
import pipes
23
24
import shlex
24
25
import shutil
25
26
import stat
30
31
KERNEL_FILTER = 'casper/vmlinuz*'
31
32
# initial ram disk
32
33
CASPER_INITRD = 'casper/initrd'
33
 
CASPER_INITRDLZ = 'casper/initrd.lz'
34
34
 
35
35
KERNEL_OPTS = (
36
36
    'boot=casper DEBCONF_DEBUG=developer -- debconf/priority=critical '
129
129
        cmd += options.format(workdir=work_dir, serial_port=serial_port)
130
130
    # Add install options if specified
131
131
    if iso_path and kernel and kernel_opts:
132
 
        initrd_path = os.path.join(work_dir, CASPER_INITRD)
 
132
        initrd_path = os.path.join(work_dir, 'initrd.d', 'initrd')
133
133
        kernel_path = os.path.join(work_dir, kernel)
134
134
        cmd += QEMU_INSTALL_OPTS.format(
135
135
            iso=iso_path, initrd=initrd_path, kernel=kernel_path,
232
232
def extract_kernel(iso_path, kernel_path, dst_path):
233
233
    """Extract kernel from iso to specified path."""
234
234
    subprocess.check_call(
235
 
        ['bsdtar', 'xf', iso_path, '-C', dst_path, CASPER_INITRDLZ,
 
235
        ['bsdtar', 'xf', iso_path, '-C', dst_path, CASPER_INITRD,
236
236
         kernel_path])
237
237
 
238
238
 
313
313
    :return: Path of initrd file with installer scripts added.
314
314
    """
315
315
    initrd_path = os.path.join(working_dir, CASPER_INITRD)
316
 
    initrdlz_path = os.path.join(working_dir, CASPER_INITRDLZ)
317
 
    initrd_work_dir = os.path.join(working_dir, 'initrd')
 
316
    initrd_work_dir = os.path.join(working_dir, 'initrd.d')
 
317
    my_initrd_path = os.path.join(initrd_work_dir, 'initrd')
318
318
    # create dir to extract initrd, change to it and then extract files
319
319
    os.mkdir(initrd_work_dir)
320
320
    os.chdir(initrd_work_dir)
321
 
    ps = subprocess.Popen(['xzcat', initrdlz_path], stdout=subprocess.PIPE)
322
 
    subprocess.check_call(['cpio', '--quiet', '-id'], stdin=ps.stdout)
323
 
    ps.wait()
 
321
    subprocess.check_call(['unmkinitramfs', initrd_path, os.getcwd()])
324
322
    # copy the qemu scripts into initrd
325
 
    copy_qemu_scripts_to_path(initrd_work_dir)
326
 
    casper_prov = os.path.join(initrd_work_dir, 'qemu-casper-provision')
 
323
    copy_qemu_scripts_to_path(os.path.join(initrd_work_dir, 'main'))
 
324
    casper_prov = os.path.join(initrd_work_dir, 'main', 'qemu-casper-provision')
327
325
    add_script_to_casper_setup(initrd_work_dir, casper_prov)
328
326
    # create the preseed file
329
 
    preseed_path = os.path.join(initrd_work_dir, PRESEED)
 
327
    preseed_path = os.path.join(initrd_work_dir, 'main', PRESEED)
330
328
    create_preseed(preseed_path, username, password, display_name)
331
329
    # Copy all files back to the archive
332
 
    with open(initrd_path, 'w') as initrd:
333
 
        ps = subprocess.Popen(['find', '.'], stdout=subprocess.PIPE)
334
 
        subprocess.check_call(
335
 
            ['cpio', '--quiet', '-o', '-H', 'newc'],
336
 
            stdin=ps.stdout, stdout=initrd)
337
 
        ps.wait()
338
 
    return initrd_path
 
330
    os.chdir(os.path.join(initrd_work_dir, 'main'))
 
331
    pipe = pipes.Template()
 
332
    pipe.prepend('find .', '.-')
 
333
    pipe.append('cpio --quiet -o -H newc', '--')
 
334
    initrd = os.path.join(initrd_work_dir, 'initrd')
 
335
    if pipe.copy('/dev/null', initrd) != 0:
 
336
        raise Exception('Failed to repack initrd')
 
337
    return my_initrd_path
339
338
 
340
339
 
341
340
def add_script_to_casper_setup(initrd_path, script_path):
343
342
    :param initrd_path: Path of initial ram disk to add the scripts.
344
343
    :param script_path: Path of script file to add.
345
344
    """
346
 
    casper_dir = os.path.join(initrd_path, 'scripts', 'casper-bottom')
 
345
    casper_dir = os.path.join(initrd_path, 'main', 'scripts', 'casper-bottom')
347
346
    script_name = os.path.basename(script_path)
348
347
    script_dst = os.path.join(casper_dir, script_name)
349
348
    shutil.copy2(script_path, script_dst)