#!/bin/sh

# 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.

# check root
if [ ${EUID:-$UID} -ne 0 ] ; then
  echo "error: Because you are not root, you cannot execute this command."
  exit 1
fi

umask 022

FLG_H=FALSE
FLG_O=FALSE
FLG_S=FALSE
while getopts hosj OPT ; do
  case $OPT in
  h) FLG_H=TRUE ;;
  o) FLG_O=TRUE ;;
  s) FLG_S=TRUE ;;
  esac
done
shift $((OPTIND - 1))

if [ $# -ne 1 ] ; then
  echo "usage: ${0##*/} [-h] [-o] [-s] [-j] LXCNAME"
  exit 1
fi

LXCNAME=$1

if ! /usr/lib/lxcf/lxcf-parmchk-cname $LXCNAME ; then
  cat <<- EOF
	error: $LXCNAME is not a container name
	       The container name must be alphanumeric character, "-" and "_".
	EOF
  exit 1
fi

rootfs=/opt/lxcf/$LXCNAME

# create new lxcf management dir
mkdir -p /etc/lxcf/rsc/$LXCNAME

# create root dir
echo "creating $rootfs..."
mkdir -p $rootfs

# create /etc and lxcf config files
echo "creating $rootfs/etc..."
cp -a /etc $rootfs
mkdir -p $rootfs/etc/lxcf/rsc/$LXCNAME
rm -rf $rootfs/etc/libvirt

# create /run
echo "creating $rootfs/run..."
mkdir -p $rootfs/run

# create /opt
echo "creating $rootfs/opt..."
if [ $FLG_O == TRUE ] ; then
  rsync -a --inplace --exclude=lxcf/ /opt/ $rootfs/opt
else
  mkdir -p $rootfs/opt
fi

# create /home and /root
if [ $FLG_H == TRUE ] ; then
  echo "creating $rootfs/home..."
  rsync -a --inplace --exclude=lxcf/ /home/ $rootfs/home
  echo "creating $rootfs/root..."
  rsync -a --inplace --exclude=lxcf/ /root/ $rootfs/root
  cp -p /root/.bashrc $rootfs/root
  cp -p /root/.bash_profile $rootfs/root
else
  mkdir -p $rootfs/root
  cp -p /root/.bashrc $rootfs/root
  cp -p /root/.bash_profile $rootfs/root
fi

# create /boot
echo "creating $rootfs/boot..."
mkdir -p $rootfs/boot

# create /var
echo "creating $rootfs/var..."
mkdir -p $rootfs/var/lib/libvirt
for i in `ls -1 /var` ; do
  if [ $i == lib ] ; then
    for j in `ls -1 /var/lib` ; do
      if [ $j == libvirt ] ; then
        for k in `ls -1 /var/lib/libvirt` ; do
          if [ $k != images ] ; then
            cp -a /var/lib/libvirt/$k $rootfs/var/lib/libvirt
          fi
        done
      else
        cp -a /var/lib/$j $rootfs/var/lib
      fi
    done
    echo -n "."
  else
    cp -a /var/$i $rootfs/var
    echo -n "."
  fi
done
echo
rm -rf $rootfs/var/log/libvirt

# create /usr
echo "creating $rootfs/usr..."
if [ $FLG_S == TRUE ] ; then
  mkdir -p $rootfs/usr
  for i in `ls -1 /usr` ; do
    cp -a /usr/$i $rootfs/usr
    echo -n "."
  done
  echo
fi

cat /dev/null > $rootfs/var/log/messages
rm -rf $rootfs/var/tmp/kdecache-root
rm -f $rootfs/var/lib/libvirt/images/*
rm -rf $rootfs/var/log/audit/*
touch $rootfs/var/log/audit/audit.log
chmod 600 $rootfs/var/log/audit/audit.log
rm -rf $rootfs/var/tmp/abrt/*

mkdir -p $rootfs/root/.ssh
chmod 700 $rootfs/root/.ssh
cp -p /root/.ssh/lxcf_rsa.pub $rootfs/root/.ssh/authorized_keys
chmod 600 $rootfs/root/.ssh/authorized_keys

exit 0
