]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-destroy.in
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / lxc-destroy.in
1 #!/bin/sh
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <daniel.lezcano@free.fr>
8
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
23 #
24 # This script allows to set or remove the capabilities on the lxc tools.
25 # When the capabilities are set, a non root user can manage the containers.
26 #
27
28 usage() {
29 echo "usage: $(basename $0) -n NAME [-f] [-P lxcpath]" >&2
30 }
31
32 help() {
33 usage
34 echo >&2
35 echo "Remove an existing container on the system." >&2
36 echo >&2
37 echo "Options:" >&2
38 echo " -n NAME specify the name of the container" >&2
39 echo " -f stop the container if it is running (rather than abort)" >&2
40 echo " -P lxcpath container is in specified lxcpath" >&2
41 }
42
43 . @DATADIR@/lxc/lxc.functions
44
45 usage_err() {
46 [ -n "$1" ] && echo "$1" >&2
47 usage
48 exit 1
49 }
50
51 verify_zfs() {
52 local path=$1
53 which zfs > /dev/null 2>&1 || { echo no; return; }
54 if zfs list -H $path >/dev/null 2>&1; then
55 echo zfs
56 else
57 echo no
58 fi
59 }
60
61 busy_zfs() {
62 local path=$1
63 local dev
64 dev=`zfs list -H $path 2>/dev/null | awk '{ print $1 }'`
65 if zfs list -t snapshot | grep -q "$dev"; then
66 echo busy
67 else
68 echo zfs
69 fi
70 }
71
72 verify_lvm() {
73 local path=$1
74 if [ -b $path -o -h $path ]; then
75 lvdisplay $path > /dev/null 2>&1 && { echo lvm; return; }
76 fi
77 echo no
78 }
79
80 busy_lvm() {
81 local path=$1
82 lvdisplay $path | grep -q "LV snapshot status.*source of" && { echo busy; return; }
83 echo lvm
84 }
85
86 optarg_check() {
87 if [ -z "$2" ]; then
88 usage_err "option '$1' requires an argument"
89 fi
90 }
91
92 force=0
93
94 while [ $# -gt 0 ]; do
95 opt="$1"
96 shift
97 case "$opt" in
98 -h|--help)
99 help
100 exit 1
101 ;;
102 -n|--name)
103 optarg_check "$opt" "$1"
104 lxc_name=$1
105 shift
106 ;;
107 -P|--lxcpath)
108 optarg_check "$opt" "$1"
109 lxc_path=$1
110 shift
111 ;;
112 -f)
113 force=1
114 ;;
115 --)
116 break
117 ;;
118 -?)
119 usage_err "unknown option '$opt'"
120 ;;
121 -*)
122 # split opts -abc into -a -b -c
123 set -- $(echo "${opt#-}" | sed 's/\(.\)/ -\1/g') "$@"
124 ;;
125 *)
126 usage
127 exit 1
128 ;;
129 esac
130 done
131
132 if [ -z "$lxc_name" ]; then
133 echo "$(basename $0): no container name specified" >&2
134 usage $0
135 exit 1
136 fi
137
138 if [ "$(id -u)" != "0" ]; then
139 echo "$(basename $0): must be run as root" >&2
140 exit 1
141 fi
142
143 if [ ! -d "$lxc_path/$lxc_name" ]; then
144 echo "$(basename $0): '$lxc_name' does not exist" >&2
145 exit 1
146 fi
147
148 # make sure the container is stopped
149 if ! lxc-info -n $lxc_name -P $lxc_path --state-is "STOPPED"; then
150 if [ $force -eq 1 ]; then
151 lxc-stop -P $lxc_path -n $lxc_name
152 lxc-wait -P $lxc_path -n $lxc_name -s STOPPED
153 else
154 echo "$(basename $0): '$lxc_name' $(lxc-info -P $lxc_path -n $lxc_name -s); aborted" >&2
155 exit 1
156 fi
157 fi
158
159 # Deduce the type of rootfs
160 # If LVM partition, destroy it. For btrfs, we delete the subvolume. If anything
161 # else, ignore it. We'll support deletion of others later.
162 rootdev=`grep '^\s*lxc\.rootfs' $lxc_path/$lxc_name/config 2>/dev/null | sed -e 's/^[^/]*//'`
163 if [ -n "$rootdev" ]; then
164 if [ `verify_lvm $rootdev` = "lvm" ]; then
165 if [ `busy_lvm $rootdev` = "busy" ]; then
166 echo "$rootdev has lvm snapshots - not deleting"
167 exit 1
168 else
169 echo "removing backing store: $rootdev"
170 lvremove -f $rootdev
171 fi
172 elif [ `verify_zfs $rootdev` = "zfs" ]; then
173 if [ `busy_zfs $rootdev` = "busy" ]; then
174 echo "$rootdev has zfs snapshots - not deleting"
175 exit 1
176 else
177 zfs destroy $(zfs list | grep $rootdev | awk '{ print $1 }')
178 if [ $? -ne 0 ]; then
179 echo "zfs destroy failed - please wait a bit and try again"
180 exit 1
181 fi
182 fi
183 elif [ -h "$rootdev" -o -d "$rootdev" ]; then
184 if which btrfs >/dev/null 2>&1 &&
185 btrfs subvolume list "$rootdev" >/dev/null 2>&1; then
186 btrfs subvolume delete "$rootdev"
187 else
188 # In case rootfs is not under $lxc_path/$lxc_name, remove it
189 rm -rf --one-file-system --preserve-root $rootdev
190 fi
191 fi
192 fi
193
194 # recursively remove the container to remove old container configuration
195 rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name