File: tmpfs.py

package info (click to toggle)
mock 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,572 kB
  • ctags: 533
  • sloc: python: 4,816; sh: 429; ansic: 66; makefile: 47
file content (97 lines) | stat: -rw-r--r-- 3,635 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
# License: GPL2 or later see COPYING
# Written by Michael Brown
# Copyright (C) 2007 Michael E Brown <mebrown@michaels-house.net>

# python library imports
import os

# our imports
from mockbuild.trace_decorator import getLog, traceLog
import mockbuild.util

requires_api_version = "1.1"


# plugin entry point
@traceLog()
def init(plugins, conf, buildroot):
    system_ram_bytes = os.sysconf(os.sysconf_names['SC_PAGE_SIZE']) * os.sysconf(os.sysconf_names['SC_PHYS_PAGES'])
    system_ram_mb = system_ram_bytes / (1024 * 1024)
    if system_ram_mb > conf['required_ram_mb']:
        Tmpfs(plugins, conf, buildroot)
    else:
        getLog().warning(
            "Tmpfs plugin disabled. "
            "System does not have the required amount of RAM to enable the tmpfs plugin. "
            "System has %sMB RAM, but the config specifies the minimum required is %sMB RAM. ",
            system_ram_mb, conf['required_ram_mb'])


class Tmpfs(object):
    """Mounts a tmpfs on the chroot dir"""
    @traceLog()
    def __init__(self, plugins, conf, buildroot):
        self.buildroot = buildroot
        self.main_config = buildroot.config
        self.state = buildroot.state
        self.conf = conf
        self.maxSize = self.conf['max_fs_size']
        self.mode = self.conf['mode']
        self.optArgs = ['-o', 'mode=%s' % self.mode]
        self.optArgs += ['-o', 'nr_inodes=0']
        if self.maxSize:
            self.optArgs += ['-o', 'size=' + self.maxSize]
        plugins.add_hook("mount_root", self._tmpfsMount)
        plugins.add_hook("postumount", self._tmpfsPostUmount)
        plugins.add_hook("umount_root", self._tmpfsUmount)
        if not os.path.ismount(self.buildroot.make_chroot_path()):
            self.mounted = False
        else:
            self.mounted = True
        getLog().info("tmpfs initialized")

    @traceLog()
    def _tmpfsMount(self):
        getLog().info("mounting tmpfs at %s.", self.buildroot.make_chroot_path())

        if not self.mounted:
            mountCmd = ["mount", "-n", "-t", "tmpfs"] + self.optArgs + \
                       ["mock_chroot_tmpfs", self.buildroot.make_chroot_path()]
            mockbuild.util.do(mountCmd, shell=False)
        else:
            getLog().info("reusing tmpfs at %s.", self.buildroot.make_chroot_path())
        self.mounted = True

    @traceLog()
    def _tmpfsPostUmount(self):
        if "keep_mounted" in self.conf and self.conf["keep_mounted"]:
            self.mounted = False
        else:
            self._tmpfsUmount()

    @traceLog()
    def _tmpfsUmount(self):
        if not self.mounted:
            return
        force = False
        getLog().info("unmounting tmpfs.")
        umountCmd = ["umount", "-n", self.buildroot.make_chroot_path()]
        # since we're in a separate namespace, the mount will be cleaned up
        # on exit, so just warn if it fails here
        try:
            mockbuild.util.do(umountCmd, shell=False)
        except:
            getLog().warning("tmpfs-plugin: exception while umounting tmpfs! (cwd: %s)", mockbuild.util.pretty_getcwd())
            force = True

        if force:
            # try umounting with force option
            umountCmd = ["umount", "-n", "-f", self.buildroot.make_chroot_path()]
            try:
                mockbuild.util.do(umountCmd, shell=False)
            except:
                getLog().warning(
                    "tmpfs-plugin: exception while force umounting tmpfs! (cwd: %s)", mockbuild.util.pretty_getcwd())
        self.mounted = False