]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-destroy.in
4 # lxc: linux Container library
7 # Daniel Lezcano <daniel.lezcano@free.fr>
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.
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.
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
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.
29 echo "usage: $(basename $0) -n NAME [-f] [-P lxcpath]" >&2
35 echo "Remove an existing container on the system." >&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
43 . @DATADIR@
/lxc
/lxc.functions
46 [ -n "$1" ] && echo "$1" >&2
53 which zfs
> /dev
/null
2>&1 ||
{ echo no
; return; }
54 if zfs list
-H $path >/dev
/null
2>&1; then
64 dev
=`zfs list -H $path 2>/dev/null | awk '{ print $1 }'`
65 if zfs list
-t snapshot |
grep -q "$dev"; then
74 if [ -b $path -o -h $path ]; then
75 lvdisplay
$path > /dev
/null
2>&1 && { echo lvm
; return; }
82 lvdisplay
$path |
grep -q "LV snapshot status.*source of" && { echo busy
; return; }
88 usage_err
"option '$1' requires an argument"
94 while [ $# -gt 0 ]; do
103 optarg_check
"$opt" "$1"
108 optarg_check
"$opt" "$1"
119 usage_err
"unknown option '$opt'"
122 # split opts -abc into -a -b -c
123 set -- $
(echo "${opt#-}" |
sed 's/\(.\)/ -\1/g') "$@"
132 if [ -z "$lxc_name" ]; then
133 echo "$(basename $0): no container name specified" >&2
138 if [ "$(id -u)" != "0" ]; then
139 echo "$(basename $0): must be run as root" >&2
143 if [ ! -d "$lxc_path/$lxc_name" ]; then
144 echo "$(basename $0): '$lxc_name' does not exist" >&2
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
154 echo "$(basename $0): '$lxc_name' $(lxc-info -P $lxc_path -n $lxc_name -s); aborted" >&2
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"
169 echo "removing backing store: $rootdev"
172 elif [ `verify_zfs $rootdev` = "zfs" ]; then
173 if [ `busy_zfs $rootdev` = "busy" ]; then
174 echo "$rootdev has zfs snapshots - not deleting"
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"
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"
188 # In case rootfs is not under $lxc_path/$lxc_name, remove it
189 rm -rf --one-file-system --preserve-root $rootdev
194 # recursively remove the container to remove old container configuration
195 rm -rf --one-file-system --preserve-root $lxc_path/$lxc_name