OSDN Git Service

The restart processing of libvirt-bin is added.
[lxcf/lxcf.git] / lxcf / cmd / lxcf
1 #!/bin/bash
2
3 # LXCF - LXC Facility
4 # Copyright (C) 2013-2014 FUJITSU LIMITED
5
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; version 2
9 # of the License.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 # check root
22 if [ ${EUID:-$UID} -ne 0 ] ; then
23   echo "error: Because you are not root, you cannot execute this command."
24   exit 1
25 fi
26
27 # make queue and message
28 mkdir -p /var/lib/lxcf
29 touch /var/lib/lxcf/hqueue
30 chmod 644 /var/lib/lxcf/hqueue
31 touch /var/lib/lxcf/qqueue
32 chmod 644 /var/lib/lxcf/qqueue
33 touch /var/lib/lxcf/lqueue
34 chmod 644 /var/lib/lxcf/lqueue
35 touch /var/lib/lxcf/exjob
36 chmod 644 /var/lib/lxcf/exjob
37 mkdir -p /var/log/lxcf
38 touch /var/log/lxcf/lxcf-messages
39 chmod 644 /var/log/lxcf/lxcf-messages
40
41 # check systemd for lxcf and lxcf-sched service
42 if [ -x /usr/bin/systemctl ] ; then
43   if ! systemctl -q is-enabled lxcf.service ; then
44     systemctl -q enable lxcf.service
45   fi
46   if ! systemctl -q is-active lxcf.service ; then
47     systemctl start lxcf.service
48   fi
49   if ! systemctl -q is-enabled lxcf-sched.service ; then
50     systemctl -q enable lxcf-sched.service
51   fi
52   if ! systemctl -q is-active lxcf-sched.service ; then
53     systemctl start lxcf-sched.service
54   fi
55 fi
56
57 # check upstart for lxcf and lxcf-sched service
58 if [ -x /sbin/initctl ] ; then
59   initctl status lxcf-sched |& grep "start/running" >& /dev/null
60   if [ $? -ne 0 ] ; then
61     initctl start lxcf-init >& /dev/null
62     initctl start lxcf-sched >& /dev/null
63     initctl restart libvirt-bin >& /dev/null
64     /etc/init.d/libvirt-bin restart >& /dev/null
65   fi
66 fi
67
68
69 # check on repetition execution
70 if [ -f /etc/lxcf/container_name ] ; then
71   echo "The lxcf command is executed in the LXCF container."
72   exit 1
73 fi
74
75 levenshtein() {
76   word1=$1 ; word2=$2
77   cat <<- EOF | awk -f -
78         BEGIN {
79             print levdist("$word1", "$word2")
80         }
81         function levdist(str1, str2,    i, j, l1, l2, x, t, a, b, c) {
82             if (str1 == str2) {
83                 return 0
84             } else if (str1 == "" || str2 == "") {
85                 return length(str1 str2)
86             } else if (substr(str1, 1, 1) == substr(str2, 1, 1)) {
87                 for (i = 2; substr(str1, i, 1) == substr(str2, i, 1); i++)
88                     ;
89                 return levdist(substr(str1, i), substr(str2, i))
90             } else if (substr(str1, l1 = length(str1), 1) \\
91                     == substr(str2, l2 = length(str2), 1)) {
92                 for (i = 1; substr(str1, l1 - i, 1) == substr(str2, l2 - i, 1); i++)
93                     ;
94                 return levdist(substr(str1, 1, l1 - i), substr(str2, 1, l2 - i))
95             }
96             for (i = 0; i < l2 + 1; i++)
97                 x[0, i] = i
98             for (i = t = 0; i < l1; i++) {
99                 x[t = !t, 0] = i + 1
100                 for (j = 0; j < l2; j++) {
101                     a = x[!t, j + 1] + 1
102                     b = x[t, j] + 1
103                     c = x[!t, j] + (substr(str1, i + 1, 1) != substr(str2, j + 1, 1))
104                     x[t, j + 1] = (a < b && a < c) ? a : (b < a && b < c) ? b : c
105                 }
106             }
107             return x[t, j]
108         }
109         EOF
110 }
111
112 export PATH=/usr/lib64/lxcf/sbin:$PATH
113
114 if [ $# -eq 0 ] ; then
115   cat <<- "EOF"
116         
117                            LXCF 0.9
118              GNU GENERAL PUBLIC LICENSE Version 2
119             Type: 'helpcmd' for help with commands
120                   'exit' or '^d' to quit
121         
122         EOF
123   PS1="LXCF # " exec /bin/sh
124 else
125   cmds=(`ls -f --ind=none /usr/lib64/lxcf/sbin | sed '/^\.\{1,2\}$/d'`)
126   for c in "${cmds[@]}" ; do [ "$c" == "$1" ] && exec "$@" ; done
127   dist=() ; for c in "${cmds[@]}" ; do dist+=(`levenshtein "$c" "$1"`) ; done
128   min=$dist ; for d in ${dist[@]} ; do [ $d -lt $min ] && min=$d ; done
129   cand=() ; i=0
130   for c in "${cmds[@]}" ; do
131     [ ${dist[$i]} -eq $min ] && cand+=("$c") ; let i++
132   done
133   [ ${#cand[@]} -eq 1 ] && object="this" || object="one of these"
134   cat <<- EOF
135         ${0##*/}: '$1' is not a ${0##*/} command. See '${0##*/} helpcmd'.
136         
137         Did you mean $object?
138         EOF
139   for c in "${cand[@]}" ; do echo $'\t'"$c" ; done
140   exit 1
141 fi