~raharper/curtin/new-bionic-upstream-snapshot-v2

« back to all changes in this revision

Viewing changes to curtin/commands/curthooks.py

  • Committer: Package Import Robot
  • Author(s): Scott Moser
  • Date: 2014-03-26 17:34:57 UTC
  • mto: (27.1.1 wily) (58.1.1 pkg)
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20140326173457-mr46h4jvuju8q775
Tags: upstream-0.1.0~bzr121
ImportĀ upstreamĀ versionĀ 0.1.0~bzr121

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import glob
19
19
import os
 
20
import platform
20
21
import re
21
22
import sys
22
23
import shutil
23
24
 
 
25
from curtin import config
24
26
from curtin import block
25
 
from curtin import config
26
27
from curtin import futil
27
28
from curtin.log import LOG
28
29
from curtin import util
39
40
     )
40
41
)
41
42
 
 
43
KERNEL_MAPPING = {
 
44
    'precise': {
 
45
        '3.2.0': '',
 
46
        '3.5.0': '-lts-quantal',
 
47
        '3.8.0': '-lts-raring',
 
48
        '3.11.0': '-lts-saucy',
 
49
        '3.13.0': '-lts-trusty',
 
50
    },
 
51
    'trusty': {
 
52
        '3.13.0': '',
 
53
    },
 
54
}
 
55
 
42
56
 
43
57
def write_files(cfg, target):
44
58
    # this takes 'write_files' entry in config and writes files in the target
54
68
    for (key, info) in cfg.get('write_files').items():
55
69
        if not info.get('path'):
56
70
            LOG.warn("Warning, write_files[%s] had no 'path' entry", key)
 
71
            continue
57
72
 
58
73
        futil.write_finfo(path=target + os.path.sep + info['path'],
59
74
                          content=info.get('content', ''),
60
75
                          owner=info.get('owner', "-1:-1"),
61
 
                          perms=info.get('perms', "0644"))
 
76
                          perms=info.get('permissions',
 
77
                                         info.get('perms', "0644")))
62
78
 
63
79
 
64
80
def apt_config(cfg, target):
120
136
        os.unlink(dpkg_cfg)
121
137
 
122
138
 
 
139
def install_kernel(cfg, target):
 
140
    kernel_cfg = cfg.get('kernel', {'package': None,
 
141
                                    'fallback-package': None,
 
142
                                    'mapping': {}})
 
143
 
 
144
    with util.RunInChroot(target) as in_chroot:
 
145
        if kernel_cfg is not None:
 
146
            kernel_package = kernel_cfg.get('package')
 
147
            kernel_fallback = kernel_cfg.get('fallback-package')
 
148
        else:
 
149
            kernel_package = None
 
150
            kernel_fallback = None
 
151
 
 
152
        if kernel_package:
 
153
            util.install_packages([kernel_package], target=target)
 
154
            return
 
155
 
 
156
        _, _, kernel, _, _ = os.uname()
 
157
        out, _ = in_chroot(['lsb_release', '--codename', '--short'],
 
158
                           capture=True)
 
159
        version, _, flavor = kernel.split('-', 2)
 
160
        config.merge_config(kernel_cfg['mapping'], KERNEL_MAPPING)
 
161
 
 
162
        try:
 
163
            map_suffix = kernel_cfg['mapping'][out.strip()][version]
 
164
        except KeyError:
 
165
            LOG.warn("Couldn't detect kernel package to install for %s."
 
166
                     % kernel)
 
167
            if kernel_fallback is not None:
 
168
                util.install_packages([kernel_fallback])
 
169
            return
 
170
 
 
171
        package = "linux-{flavor}{map_suffix}".format(
 
172
            flavor=flavor, map_suffix=map_suffix)
 
173
        out, _ = in_chroot(['apt-cache', 'search', package], capture=True)
 
174
        if (len(out.strip()) > 0 and
 
175
                not util.has_pkg_installed(package, target)):
 
176
            util.install_packages([package], target=target)
 
177
        else:
 
178
            LOG.warn("Tried to install kernel %s but package not found."
 
179
                     % package)
 
180
            if kernel_fallback is not None:
 
181
                util.install_packages([kernel_fallback], target=target)
 
182
 
 
183
 
123
184
def apply_debconf_selections(cfg, target):
124
185
    # debconf_selections:
125
186
    #  set1: |
200
261
 
201
262
 
202
263
def setup_grub(cfg, target):
203
 
    if 'grub_install_devices' in cfg:
204
 
        instdevs = cfg.get('grub_install_devices')
 
264
    grubcfg = cfg.get('grub', {})
 
265
 
 
266
    # copy legacy top level name
 
267
    if 'grub_install_devices' in cfg and 'install_devices' not in grubcfg:
 
268
        grubcfg['install_devices'] = cfg['grub_install_devices']
 
269
 
 
270
    if 'install_devices' in grubcfg:
 
271
        instdevs = grubcfg.get('install_devices')
205
272
        if isinstance(instdevs, str):
206
273
            instdevs = [instdevs]
207
274
        if instdevs is None:
215
282
 
216
283
        instdevs = list(blockdevs)
217
284
 
 
285
    env = os.environ.copy()
 
286
 
 
287
    replace_default = grubcfg.get('replace_linux_default', True)
 
288
    if str(replace_default).lower() in ("0", "false"):
 
289
        env['REPLACE_GRUB_LINUX_DEFAULT'] = "0"
 
290
    else:
 
291
        env['REPLACE_GRUB_LINUX_DEFAULT'] = "1"
 
292
 
218
293
    instdevs = [block.get_dev_name_entry(i)[1] for i in instdevs]
219
 
    LOG.debug("installing grub to %s", instdevs)
 
294
    LOG.debug("installing grub to %s [replace_default=%s]",
 
295
              instdevs, replace_default)
220
296
    with util.ChrootableTarget(target):
221
 
        util.subp(['install-grub', target] + instdevs)
 
297
        util.subp(['install-grub', target] + instdevs, env=env)
 
298
 
 
299
 
 
300
def update_initramfs(target):
 
301
    with util.RunInChroot(target) as in_chroot:
 
302
        in_chroot(['update-initramfs', '-u'])
222
303
 
223
304
 
224
305
def copy_fstab(fstab, target):
260
341
    else:
261
342
        target = state['target']
262
343
 
263
 
    if args.config is not None:
264
 
        cfg_file = args.config
265
 
    else:
266
 
        cfg_file = state['config']
267
 
 
268
344
    if target is None:
269
345
        sys.stderr.write("Unable to find target.  "
270
346
                         "Use --target or set TARGET_MOUNT_POINT\n")
271
347
        sys.exit(2)
272
348
 
273
 
    if not cfg_file:
274
 
        LOG.debug("config file was none!")
275
 
        cfg = {}
276
 
    else:
277
 
        cfg = config.load_config(cfg_file)
 
349
    cfg = util.load_command_config(args, state)
278
350
 
279
351
    write_files(cfg, target)
280
352
    apt_config(cfg, target)
281
353
    disable_overlayroot(cfg, target)
 
354
    install_kernel(cfg, target)
282
355
    apply_debconf_selections(cfg, target)
283
356
 
284
357
    restore_dist_interfaces(cfg, target)
285
358
 
286
359
    copy_interfaces(state.get('interfaces'), target)
287
360
    copy_fstab(state.get('fstab'), target)
288
 
    setup_grub(cfg, target)
 
361
 
 
362
    # As a rule, ARMv7 systems don't use grub. This may change some
 
363
    # day, but for now, assume no. They do require the initramfs
 
364
    # to be updated, and this also triggers boot loader setup via
 
365
    # flash-kernel.
 
366
    machine = platform.machine()
 
367
    if machine.startswith('armv7'):
 
368
        update_initramfs(target)
 
369
    else:
 
370
        setup_grub(cfg, target)
289
371
 
290
372
    sys.exit(0)
291
373