summaryrefslogtreecommitdiff
path: root/virtinst/installerinject.py
blob: 8619b71804a0eb1c07e9eb5e2e0950d172ed95b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
# Copyright 2006-2009, 2013, 2014 Red Hat, Inc.
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import logging
import os
import shutil
import subprocess
import tempfile


def _run_initrd_commands(initrd, tempdir):
    logging.debug("Appending to the initrd.")

    find_proc = subprocess.Popen(['find', '.', '-print0'],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 cwd=tempdir)
    cpio_proc = subprocess.Popen(['cpio', '--create', '--null', '--quiet',
                                  '--format=newc', '--owner=+0:+0'],
                                 stdin=find_proc.stdout,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 cwd=tempdir)
    f = open(initrd, 'ab')
    gzip_proc = subprocess.Popen(['gzip'], stdin=cpio_proc.stdout,
                                 stdout=f, stderr=subprocess.PIPE)
    cpio_proc.wait()
    find_proc.wait()
    gzip_proc.wait()
    f.close()

    finderr = find_proc.stderr.read()
    cpioerr = cpio_proc.stderr.read()
    gziperr = gzip_proc.stderr.read()
    if finderr:  # pragma: no cover
        logging.debug("find stderr=%s", finderr)
    if cpioerr:  # pragma: no cover
        logging.debug("cpio stderr=%s", cpioerr)
    if gziperr:  # pragma: no cover
        logging.debug("gzip stderr=%s", gziperr)


def _run_iso_commands(iso, tempdir):
    cmd = ["genisoimage",
           "-o", iso,
           "-J",
           "-input-charset", "utf8",
           "-rational-rock",
           tempdir]
    logging.debug("Running iso build command: %s", cmd)
    output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
    logging.debug("cmd output: %s", output)


def _perform_generic_injections(injections, scratchdir, media, cb):
    if not injections:
        return

    tempdir = tempfile.mkdtemp(dir=scratchdir)
    try:
        os.chmod(tempdir, 0o775)

        for filename in injections:
            if type(filename) is tuple:
                filename, dst = filename
            else:
                dst = os.path.basename(filename)

            logging.debug("Injecting src=%s dst=%s into media=%s",
                    filename, dst, media)
            shutil.copy(filename, os.path.join(tempdir, dst))

        return cb(media, tempdir)
    finally:
        shutil.rmtree(tempdir)


def perform_initrd_injections(initrd, injections, scratchdir):
    """
    Insert files into the root directory of the initial ram disk
    """
    _perform_generic_injections(injections, scratchdir, initrd,
            _run_initrd_commands)


def perform_cdrom_injections(injections, scratchdir):
    """
    Insert files into the root directory of a generated cdrom
    """
    fileobj = tempfile.NamedTemporaryFile(
        prefix="virtinst-", suffix="-unattended.iso",
        dir=scratchdir, delete=False)
    iso = fileobj.name

    try:
        _perform_generic_injections(injections, scratchdir, iso,
            _run_iso_commands)
    except Exception:  # pragma: no cover
        os.unlink(iso)
        raise

    return iso