OSDN Git Service

When cntl-c is pushed sysgen, clone and erase on the way,
[lxcf/lxcf.git] / lxcf / lib / lxcf-sysgen
1 #!/bin/sh
2 # copyright (C) 2013-2014 FUJITSU LIMITED All Rights Reserved
3
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; version 2
7 # of the License.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
17 # 02110-1301, USA.
18
19
20 # check /opt/lxcf
21 mkdir -p /opt/lxcf
22 chmod 755 /opt/lxcf
23
24 # check /etc/lxcf/rsc
25 mkdir -p /etc/lxcf/rsc
26 chmod 755 /etc/lxcf
27 chmod 755 /etc/lxcf/rsc
28
29 # check lxcf.conf
30 if [ ! -e /etc/lxcf/lxcf.conf ] ; then
31   cp -p /usr/lib/lxcf/lxcf.conf /etc/lxcf/lxcf.conf
32 fi
33
34 # check default separate mdoel
35 /usr/lib/lxcf/lxcf-config
36 if [ -e /etc/lxcf/separate ] ; then
37   FLG_S=1
38   VAL_SEPARATE="-s"
39 else
40   FLG_S=0
41   VAL_SEPARATE=""
42 fi
43
44 # check options
45 FLG_C=0
46 FLG_P=0
47 JSONFILE=""
48 FILEPATH=""
49
50 while getopts sjc:p: OPT
51 do
52   case $OPT in
53     "s" ) FLG_S=1 ; VAL_SEPARATE="-s" ;;
54     "j" ) FLG_S=0 ; VAL_SEPARATE="-j" ;;
55     "c" ) FLG_C=1 ; JSONFILE=$OPTARG ;;
56     "p" ) FLG_P=1 ; FILEPATH=$OPTARG ;;
57   esac
58 done
59
60 shift `expr $OPTIND - 1`
61
62 # check args
63 if [ $# -lt 1 ]; then
64         echo "usage lxcf sysgen [-s][-j][-c configfile] LXCNAME"
65         exit 1
66 fi
67
68 # check jsonfile
69 if [ ${FLG_C} -eq 1 ] ; then
70   /usr/lib/lxcf/lxcf-check-json ${JSONFILE}
71   if [ $? -ne 0 ] ; then
72     echo "error: illegal format json file :" ${JSONFILE}
73     exit 1
74   fi
75 fi
76
77 # check -p path
78 if [ ${FLG_P} -eq 1 ] ; then
79   if [ ! -d $FILEPATH ] ; then
80     echo "error:" $FILEPATH "is not a directory"
81     exit 1
82   fi
83   if echo $FILEPATH | grep "^/opt/lxcf" ; then
84     echo "error:" $FILEPATH "overlaps with passing other containers. "
85     echo "      " $FILEPATH "contains /opt/lxcf."
86     exit 1
87   fi
88   for i in /opt/lxcf/*
89   do
90     apath=`readlink -f $i`
91     if echo $FILEPATH | grep "^${apath}" ; then
92       echo "error:" $FILEPATH "overlaps with passing other containers. "
93       echo "      " $FILEPATH "contains $apath."
94       exit 1
95     fi    
96   done
97 fi
98
99 # generate one container
100 lxcf_sysgen1() {
101   LXCNAME=${1}
102
103   VNAME=`virsh list --all | \
104     awk '{if ((NR > 2) && (NF == 3) && ($2 == "'${LXCNAME}'")) print $2}'`
105   if [ x${LXCNAME} == x${VNAME} ] ; then
106     echo "error: The same VM name already exists:" ${LXCNAME}
107     return
108   fi
109
110   LNAME=`virsh -c lxc:/// list --all | \
111     awk '{if ((NR > 2) && (NF == 3) && ($2 == "'${LXCNAME}'")) print $2}'`
112
113   if [ x${LXCNAME} == x${LNAME} ] ; then
114     echo "error: The same LXC name already exists:" ${LXCNAME}
115     return
116   fi
117
118   if ! /usr/lib/lxcf/lxcf-parmchk-cname $LXCNAME ; then
119     echo "error:" $LXCNAME "is not a container name"
120     exit 1
121   fi
122
123   PREFIX=/opt/lxcf/${LXCNAME}
124
125   # check LXCNAME
126   if [ -e /${PREFIX} ] ; then
127         echo "error: There is already " ${LXCNAME}
128         exit 1
129   fi 
130
131   echo "Generate" ${LXCNAME}
132   trap '/usr/lib/lxcf/lxcf-erase ${LXCNAME};exit 1' 2
133
134   # set symbolic link for -p option
135   if [ $FLG_P -eq 1 ] ; then
136     mkdir -p ${FILEPATH}/${LXCNAME}
137     ln -s  ${FILEPATH}/${LXCNAME} ${PREFIX}
138   fi
139
140   # main operation
141   /usr/lib/lxcf/lxcf-createfile ${VAL_SEPARATE} ${LXCNAME}
142   /usr/lib/lxcf/lxcf-setup      ${VAL_SEPARATE} ${LXCNAME}
143   /usr/lib/lxcf/lxcf-define     ${LXCNAME}
144
145   # disable lxcf.service
146   rm /opt/lxcf/${LXCNAME}/etc/systemd/system/multi-user.target.wants/lxcf.service
147
148   # create an initial rsc file
149   /usr/lib/lxcf/lxcf-resource1 initrsc ${LXCNAME} > /etc/lxcf/rsc/${LXCNAME}/resource.val
150
151   /usr/lib/lxcf/lxcf-start ${LXCNAME}
152
153   # create known_hosts entry
154   sed -i "/^${LXCNAME}[ |,]/d"  /root/.ssh/known_hosts
155   ssh-keyscan localhost |& egrep "ssh-rsa" >> /root/.ssh/known_hosts
156   LXCIPADR=`awk '{if ($2 == "'${LXCNAME}'") printf "%s",$1}' /etc/hosts`
157   sed -i "s/localhost/${LXCNAME},${LXCIPADR}/" /root/.ssh/known_hosts 
158
159   # set resource 
160   if [ $FLG_C -eq 1 ] ; then
161     echo "load json file :" $JSONFILE
162     /usr/lib/lxcf/lxcf-load $LXCNAME $JSONFILE
163   fi
164   #/usr/lib/lxcf/lxcf-resource show ${LXCNAME}
165 }
166
167 # erase yum chache
168 yum clean packages >& /dev/null &
169 yum clean all >& /dev/null &
170
171 # check /etc/hosts
172 if [ ! -e /etc/hosts ] ; then
173   echo "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4" > /etc/hosts
174   echo "::1         localhost localhost.localdomain localhost6 localhost6.localdomain6" >> /etc/hosts
175 fi
176
177 # generate containers of args
178 for i in $*
179 do
180   lxcf_sysgen1 $i 
181 done
182
183
184 exit 0
185