~ubuntu-branches/ubuntu/precise/lxc/precise-updates

« back to all changes in this revision

Viewing changes to debian/local/lxc-wait

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2012-03-21 08:20:06 UTC
  • Revision ID: package-import@ubuntu.com-20120321082006-bsepg8w3z7qb79xt
Tags: 0.7.5-3ubuntu41
* add lxc-shutdown command:
  - 0060-lxc-shutdown: add the command to the source
  - debian/lxc.upstart: use lxc-shutdown to shut down containers cleanly
  - debian/lxc.default: add LXC_SHUTDOWN_TIMEOUT (default 120s)
* support per-container apparmor policies:  (LP: #953453)
  - 0061-lxc-start-apparmor: add lxc.aa_profile to config file.  If not
    specified, lxc-default profile is used for container.  Otherwise, the
    specified profile is used.
    Note that per-container profiles must be named 'lxc-*'.
  - split debian/lxc-default.apparmor from debian/lxc.apparmor.
  - have /etc/apparmor.d/lxc-containers #include /etc/apparmor.d/lxc/*
  - debian/lxc.postinst: load the new lxc-containers profiles
  - debian/lxc.postrm: remove lxc-containers profiles
  - debian/rules: make new etc/apparmor.d/lxc dir and copy lxc-default into it
  - debian/control: add libapparmor-dev to build-depends
  - debian/lxc.upstart: load apparmor per-container policies at pre-start.
* debian/lxc.apparmor: insert the stricter mount rules for lxc-start
  (LP: #645625) (LP: #942934)
* debian/local/lxc-start-ephemeral: re-enable aufs option (LP: #960262)
* replace upstream lxc-wait with our own bash script (LP: #951181)
  - debian/local/lxc-wait: the script
  - debian/rules: copy the script into place
* 0062-templates-relative-paths: update templates to use relative paths,
  and make lxc-start always accept /var/lib/lxc/CN/rootfs as target prefix,
  to make lvm containers work.  (LP: #960860)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
# (C) Copyright Canonical 2011,2012
 
4
 
 
5
# This library is free software; you can redistribute it and/or
 
6
# modify it under the terms of the GNU Lesser General Public
 
7
# License as published by the Free Software Foundation; either
 
8
# version 2.1 of the License, or (at your option) any later version.
 
9
 
 
10
# This library is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
# Lesser General Public License for more details.
 
14
 
 
15
# You should have received a copy of the GNU Lesser General Public
 
16
# License along with this library; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 
 
19
usage() {
 
20
    echo "Usage: lxc-wait --name=NAME --state=STATE"
 
21
}
 
22
 
 
23
help() {
 
24
    usage
 
25
    echo
 
26
    echo "lxc-wait waits for NAME container state to reach STATE"
 
27
    echo
 
28
    echo "Options :"
 
29
    echo "   -n, --name=NAME   NAME for name of the container"
 
30
    echo "   -s, --state=STATE ORed states to wait for"
 
31
    echo "                     STOPPED, STARTING, RUNNING, STOPPING,"
 
32
    echo "                     ABORTING, FREEZING, FROZEN"
 
33
}
 
34
 
 
35
set -e
 
36
 
 
37
valid_states=("STOPPED" "STARTING" "RUNNING" "STOPPING"
 
38
"ABORTING" "FREEZING" "FROZEN" "THAWED" "MAX_STATE")
 
39
 
 
40
container_exists() {
 
41
    local c=$1
 
42
    local list=`lxc-ls -1`
 
43
    local name
 
44
 
 
45
    for name in $list; do
 
46
        if [ "$c" = "$name" ]; then
 
47
            echo "yes"
 
48
        fi
 
49
        echo "no"
 
50
    done
 
51
}
 
52
 
 
53
verify_state() {
 
54
    local s="$1"
 
55
    local i
 
56
 
 
57
    for((i=0;i<${#valid_states[@]};i++)); do
 
58
        if [ "$s" = "${valid_states[$i]}" ]; then
 
59
            echo "ok"; return;
 
60
        fi
 
61
    done
 
62
    echo "bad"; return;
 
63
}
 
64
        
 
65
badstate=""
 
66
verify_states() {
 
67
    local states=$1
 
68
    local s
 
69
    local v
 
70
 
 
71
    for s in $states; do
 
72
        echo "verifying $s"
 
73
        v=$(verify_state "$s")
 
74
        if [ $v = "bad" ]; then
 
75
            echo "bad";
 
76
            badstate="$s"
 
77
            return;
 
78
        fi
 
79
    done
 
80
    echo "ok";
 
81
}
 
82
 
 
83
state_is_in() {
 
84
    local state=$1
 
85
    local states=$2
 
86
    local s
 
87
 
 
88
    for s in $states; do
 
89
        if [ "$state" = "$s" ]; then
 
90
            echo "yes"; return;
 
91
        fi
 
92
    done
 
93
    echo "no"
 
94
}
 
95
 
 
96
shortoptions='hn:s:'
 
97
longoptions='help,name:,states:'
 
98
 
 
99
getopt=$(getopt -o $shortoptions --longoptions  $longoptions -- "$@")
 
100
if [ $? != 0 ]; then
 
101
    usage
 
102
    exit 1;
 
103
fi
 
104
 
 
105
eval set -- "$getopt"
 
106
 
 
107
states="RUNNING"
 
108
 
 
109
while true; do
 
110
    case "$1" in
 
111
    -h|--help)
 
112
        help
 
113
        exit 1
 
114
        ;;
 
115
    -n|--name)
 
116
        shift
 
117
        lxc_name=$1
 
118
        shift
 
119
        ;;
 
120
    -s|--states)
 
121
        shift
 
122
        states=$1
 
123
        shift
 
124
        ;;
 
125
    --)
 
126
        shift
 
127
        break;;
 
128
    *)
 
129
        echo $1
 
130
        usage
 
131
        exit 1
 
132
        ;;
 
133
    esac
 
134
done
 
135
 
 
136
if [ -z "$lxc_name" ]; then
 
137
    echo "no container name specified"
 
138
    usage
 
139
    exit 1
 
140
fi
 
141
 
 
142
if [ "$(id -u)" != "0" ]; then
 
143
   echo "This command has to be run as root"
 
144
   exit 1
 
145
fi
 
146
 
 
147
type lxc-info > /dev/null || { echo "lxc-info not found."; exit 1; }
 
148
 
 
149
v=$(container_exists $lxc_name)
 
150
if [ "$v" = "no" ]; then
 
151
    echo "Container $lxc_name does not exist"
 
152
    exit 1
 
153
fi
 
154
states2="`echo $states | sed -e 's/|/ /g'`"
 
155
v=$(verify_states "${states2}")
 
156
if [ "$v" = "bad" ]; then
 
157
   echo "invalid state $badstate in provided set"
 
158
   exit 1
 
159
fi
 
160
        
 
161
 
 
162
while [ 1 ]; do
 
163
    o=`lxc-info -s -n $lxc_name`
 
164
    s=`echo $o | awk '{ print $2 }'`
 
165
    if [ $(state_is_in "$s" "${states2}") = "yes" ]; then
 
166
        break;
 
167
    fi
 
168
    sleep 1
 
169
done
 
170
 
 
171
exit 0