OSDN Git Service

Correspondence to interchangeability with old release.
[lxcf/lxcf.git] / lxcf / lib / lxcf-list
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 # check root
20 if [ ${EUID:-${UID}} != 0 ]; then
21     echo "error: Because you are not root, you cannot execute this command. "
22     exit 1
23 fi
24
25 # check option
26 FLG_Q=0
27
28 while getopts q OPT ; do
29   case $OPT in
30   q) FLG_Q=1 ;;
31   esac
32 done
33 shift $((OPTIND - 1))
34
35 if [ $FLG_Q -eq 1 ]; then
36         exec /usr/lib64/lxcf/lxcf-queue-list $*
37 fi
38
39
40 print_list() {
41
42         LNAME=`cut -d ' ' -f 1 /etc/lxcf/rsc/${LXCNAME}/container_name`
43         LMODEL=`cut -d ' ' -f 2 /etc/lxcf/rsc/${LXCNAME}/container_name`
44         LSTATE=`LANG=C /usr/bin/virsh -c lxc:/// list | /usr/bin/awk '($2 == "'${LXCNAME}'"){print $3}'`
45         if [ -z "${LSTATE}"  ]; then
46                 LSTATE="stopped";
47         fi
48         if [ -f /etc/lxcf/rsc/${LXCNAME}/autostart ]; then
49                 AUTOSTART="y"
50         else
51                 AUTOSTART="n"
52         fi
53         LPATH=`readlink -f /opt/lxcf/${LXCNAME}`
54         printf "%s\t\t%s    \t%s\t  %s\t      %s\n" ${LNAME} ${LMODEL} ${LSTATE} ${AUTOSTART} ${LPATH}
55 }
56
57
58 lsdir() {
59   ls -f --ind=none $1 | sed '/^\.\{1,2\}$/d'
60 }
61
62
63 # When uuid is specified, the container name is returned. 
64 uuid2name() {
65   NAME=$1
66   if [ -d /opt/lxcf/${NAME} ]; then
67     echo -n $NAME
68     return
69   fi
70   for i in `lsdir /etc/lxcf/rsc`
71   do
72      if [ x$NAME == x`cat /etc/lxcf/rsc/${i}/uuid` ]; then
73        echo -n $i
74        return
75      fi
76   done
77   echo -n $NAME
78 }
79
80 printf "Name\t\tMode    \tState\t  Autostart    Path\n"
81 echo   "-------------------------------------------------------------------------"
82
83 if [ $# -eq 0 ] ; then
84   for LXCNAME in `ls -1 /opt/lxcf/`
85   do
86         # Correspondence to interchangeability with old release
87         if [ ! -f /etc/lxcf/rsc/${LXCNAME}/container_name ]; then
88                 cp -p /opt/lxcf/${LXCNAME}/etc/lxcf/container_name \
89                         /etc/lxcf/rsc/${LXCNAME}/container_name
90         fi
91
92         if [ -e /etc/lxcf/rsc/${LXCNAME}/container_name  ] ; then
93                 print_list 
94         fi
95   done
96 else
97   for LXCNAME in $*
98   do
99     LXCNAME=`uuid2name $LXCNAME`
100     if ! /usr/lib64/lxcf/lxcf-parmchk-cname $LXCNAME ; then
101       echo "error:" $LXCNAME "is not a container name"
102       echo "        The container name must be alphanumeric character, \"-\", and \"_\"."
103       exit 1
104     fi
105
106     if [ ! -e /opt/lxcf/${LXCNAME} ] ; then
107       echo "error:" $LXCNAME "is the unknown container name"
108       exit 1
109     fi
110
111     # Correspondence to interchangeability with old release
112     if [ ! -f /etc/lxcf/rsc/${LXCNAME}/container_name ]; then
113         cp -p /opt/lxcf/${LXCNAME}/etc/lxcf/container_name \
114                 /etc/lxcf/rsc/${LXCNAME}/container_name
115     fi
116
117     if [ -e /etc/lxcf/rsc/${LXCNAME}/container_name ] ; then
118                 print_list
119     fi
120   done
121 fi
122
123 exit 0
124