コード例 #1
0
ファイル: _run.py プロジェクト: gaocegege/treadmill
def _create_root_dir(tm_env, container_dir, root_dir, app):
    """Prepares chrooted environment and creates all mountpoints.

    :param tm_env:
        Treadmill application environment
    :type tm_env:
        `appenv.AppEnvironment`
    """
    # Generate a unique name for the app
    unique_name = appcfg.app_unique_name(app)

    # First wait for the block device to be ready
    localdisk_client = tm_env.svc_localdisk.make_client(
        os.path.join(container_dir, 'localdisk'))
    localdisk = localdisk_client.wait(unique_name)

    already_initialized = fs.test_filesystem(localdisk['block_dev'])
    if not already_initialized:
        # Format the block device
        fs.create_filesystem(localdisk['block_dev'])

    _LOGGER.info('Creating container root directory: %s', root_dir)
    fs.chroot_init(root_dir)
    fs.mount_filesystem(localdisk['block_dev'], root_dir)
    fs.make_rootfs(root_dir, app.proid)

    fs.configure_plugins(tm_env.root, root_dir, app)

    # Ensure .etc directory is clean in case of volume restore.
    try:
        shutil.rmtree(os.path.join(root_dir, '.etc'))
    except OSError as err:
        if err.errno != errno.ENOENT:
            raise

    shutil.copytree(os.path.join(tm_env.root, 'etc'),
                    os.path.join(root_dir, '.etc'))

    shutil.copyfile('/etc/hosts', os.path.join(root_dir, '.etc/hosts'))
    shutil.copyfile('/etc/hosts', os.path.join(root_dir,
                                               '.etc/hosts.original'))

    hosts_aliases = os.path.join(root_dir, '.etc', 'hosts-aliases')
    fs.mkdir_safe(hosts_aliases)

    pwnam = pwd.getpwnam(app.proid)
    os.chown(hosts_aliases, pwnam.pw_uid, pwnam.pw_gid)

    # Always use our own resolv.conf. Safe to rbind, as we are running in
    # private mount subsystem by now.
    resolv_conf_path = os.path.join(tm_env.root, 'etc/resolv.conf')
    if os.path.exists(resolv_conf_path):
        subproc.check_call(
            ['mount', '-n', '--bind', resolv_conf_path, '/etc/resolv.conf'])
コード例 #2
0
 def test_chroot_init_empty_existing(self):
     """Checks that chroot can be done over existing empty dir."""
     fs.chroot_init('/var/bla')
     os.makedirs.assert_called_with('/var/bla', mode=0777)
     treadmill.syscall.unshare.unshare.assert_called_with(_CLONE_NEWNS)
コード例 #3
0
 def test_chroot_init_ok(self):
     """Mock test, verifies root directory created and unshare called."""
     fs.chroot_init('/var/bla')
     os.makedirs.assert_called_with('/var/bla', mode=0777)
     treadmill.syscall.unshare.unshare.assert_called_with(_CLONE_NEWNS)