#!/bin/bash

# LXCF - LXC Facility
# Copyright (C) 2013-2014 FUJITSU LIMITED

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.

# cgroups path
CPU_LXCFMAINTENANCE=/sys/fs/cgroup/cpu/lxcf-maintenance.slice
CPUSET_LXCFMAINTENANCE=/sys/fs/cgroup/cpuset/lxcf-maintenance.slice
MEM_LXCFMAINTENANCE=/sys/fs/cgroup/memory/lxcf-maintenance.slice

# backup /usr
WORKUSR=/opt/lxcf/.work/usr
mkdir ^p $WORKUSR
nice -20 cp /usr/* $WORKUSR/. >& /dev/null 

# make cgroup directories
if [ ! -d $CPU_LXCFMAINTENANCE ] ; then
  mkdir -p $CPU_LXCFMAINTENANCE
fi
if [ ! -d $CPUSET_LXCFMAINTENANCE ] ; then
  mkdir -p $CPUSET_LXCFMAINTENANCE
fi
if [ ! -d $MEM_LXCFMAINTENANCE ] ; then
  mkdir -p $MEM_LXCFMAINTENANCE
fi

# set CPU 10%
echo -n 100000 > $CPU_LXCFMAINTENANCE/cpu.cfs_period_us
echo -n 10000 > $CPU_LXCFMAINTENANCE/cpu.cfs_quota_us
# set CPU Number 0
echo -n 0 > $CPUSET_LXCFMAINTENANCE/cpuset.cpus
#set MEMORY 100M
echo -n $((100 * 1024 ** 2)) > $MEM_LXCFMAINTENANCE/memory.limit_in_bytes

# make queue
mkdir -p /var/lib/lxcf
touch /var/lib/lxcf/hqueue
chmod 644 /var/lib/lxcf/hqueue
touch /var/lib/lxcf/qqueue
chmod 644 /var/lib/lxcf/qqueue
touch /var/lib/lxcf/lqueue
chmod 644 /var/lib/lxcf/lqueue
touch /var/lib/lxcf/exjob
chmod 644 /var/lib/lxcf/exjob

# check separate model
/usr/lib64/lxcf/lxcf-config

wait
