-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot-lxc
executable file
·293 lines (236 loc) · 7.6 KB
/
snapshot-lxc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
set -eu -o pipefail
safe_source () { [[ ! -z ${1:-} ]] && source $1; _dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"; _sdir=$(dirname "$(readlink -f "$0")"); }; safe_source
safe_source $_sdir/dcs-tools/aktos-bash-lib/all.sh
rm_folder(){
if is_btrfs $1; then
btrfs sub del $1
else
rm -r $1
fi
}
cp_folder(){
local target
[[ -d $2 ]] && target=$2 || target=$(dirname $2)
if is_btrfs $1 && is_btrfs $target; then
btrfs sub snap $1 $2
else
cp -al $1 $2
fi
}
# show help
show_help(){
cat <<HELP
Usage:
$(basename $0) ...options
Options
--src /path/to/subvolume : Subvolume source
--name your-container-name : Container name
--delete your-container-name : Delete the container
--keep-ssh-keys : Do not regenerate SSH keys
--config : Open config file after creating the container
--list : List currently assigned IP's
HELP
exit
}
die(){
echo_red "$1"
show_help
exit 1
}
# Parse command line arguments
# ---------------------------
# Initialize parameters
new_keys=true
NAME=
SUBVOL=
delete_vm=false
open_config=false
list=false
# ---------------------------
args=("$@")
_count=1
while :; do
key="${1:-}"
case $key in
-h|-\?|--help|'')
show_help # Display a usage synopsis.
exit
;;
# --------------------------------------------------------
--name) shift
if [[ ! -z ${1:-} ]]; then
NAME=$1
shift
fi
;;
--src) shift
if [[ ! -z ${1:-} ]]; then
SUBVOL=$1
shift
fi
;;
--keep-ssh-keys) shift
new_keys=false
;;
--config) shift
open_config=true
;;
--list) shift
list=true
;;
--delete) shift
delete_vm=true
if [[ ! -z ${1:-} ]]; then
NAME=$1
shift
fi
;;
# --------------------------------------------------------
-*) # Handle unrecognized options
echo
echo "Unknown option: $1"
show_help
exit 1
;;
*) # generate the positional arguments
declare _arg$((_count++))="$1"
shift
esac
[[ -z ${1:-} ]] && break
done; set -- "${args[@]}"
# use $_arg1 in place of $1, $_arg2 in place of $2 and so on, "$@" is intact
lxc_major=$(lxc-ls --version | cut -d. -f1)
if [[ $list = true ]]; then
[[ $(whoami) = "root" ]] || { sudo $0 "$@"; exit 0; }
echo "List of containers:"
echo "------------------"
set +e
for vm_conf in /var/lib/lxc/*/config; do
[[ -f $vm_conf ]] || continue
vm_name=$(basename $(dirname $vm_conf))
if [[ "$lxc_major" == "2" ]]; then
ip=$(cat $vm_conf | egrep "^lxc\.network\.ipv4\s*=" | cut -d "=" -f 2)
elif [[ "$lxc_major" == "3" ]] || [[ "$lxc_major" == "4" ]] || [[ "$lxc_major" == "5" ]]; then
ip=$(cat $vm_conf | egrep "^lxc\.net.*\.ipv4.address\s*=" | cut -d "=" -f 2)
fi
echo -e "$vm_name\t\t: $ip"
done
exit 0
fi
# Check parameters
[[ -z $NAME ]] && die "Name of VM is required."
[[ $delete_vm = false ]] && [[ -z $SUBVOL ]] && die "Source directory (rootfs) is required"
# All checks are done, run as root.
[[ $(whoami) = "root" ]] || { sudo $0 "$@"; exit 0; }
if ! is_btrfs /var/lib/lxc; then
echo_info "This is a non-BTRFS file system."
else
echo_info "BTRFS file system"
fi
GUEST_ROOT="/var/lib/lxc/$NAME/rootfs"
if [[ $delete_vm = true ]]; then
read -p "Press enter to stop and delete LXC container: $NAME"
set +e
if [[ $(lxc-info -n $NAME | awk '$1 ~ /State/ {print $2}') == "RUNNING" ]]; then
lxc-stop -n $NAME
fi
rm_folder $GUEST_ROOT
rm -r $(dirname $GUEST_ROOT)
echo_green "Removed $NAME"
exit
fi
START_TIME=$SECONDS
echo "creating the container directory: $NAME"
mkdir -p $(dirname $GUEST_ROOT)
echo "creating a writable snapshot of given subvolume"
cp_folder $SUBVOL $GUEST_ROOT
_param=
[[ $new_keys = false ]] && _param="--skip-ssh-keys"
$_sdir/dcs-tools/give-new-id \
--hostname $NAME \
--root-dir $GUEST_ROOT \
$_param
if [[ -f $GUEST_ROOT/etc/fstab ]]; then
echo "emptying the /etc/fstab file"
mv $GUEST_ROOT/etc/fstab $GUEST_ROOT/etc/fstab.real
touch $GUEST_ROOT/etc/fstab
else
echo "No /etc/fstab file found, skipping removal."
fi
echo "remove the LXC specific entry from GUEST/etc/network/interfaces file"
IFACE_FILE=$GUEST_ROOT/etc/network/interfaces
mv $IFACE_FILE $IFACE_FILE.real
awk '/lxc-bridge/ {do_print=0}
do_print==1 {print}
NF==0 {do_print=1}' $IFACE_FILE.real > $IFACE_FILE
echo "creating the config file"
if [[ "$lxc_major" == "2" ]]; then
cat <<- CONFIG2 > $(dirname $GUEST_ROOT)/config
# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# Template script checksum (SHA-1): 740c51206e35463362b735e68b867876048a8baf
# For additional config options, please look at lxc.container.conf(5)
# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)
# Distribution configuration
lxc.include = /usr/share/lxc/config/debian.common.conf
lxc.arch = $(uname -m)
# Container specific configuration
lxc.rootfs = /var/lib/lxc/$NAME/rootfs
lxc.rootfs.backend = dir
lxc.utsname = $NAME
# Network configuration
lxc.network.type = veth
lxc.network.link = lxc-bridge
lxc.network.flags = up
#lxc.network.ipv4.gateway = 10.0.8.1
#lxc.network.ipv4 = 10.0.8.8
# External mounts
#lxc.mount.entry = /path/to/host/folder path/to/folder/in/container none bind 0 0
CONFIG2
elif [[ "$lxc_major" == "3" ]] || [[ "$lxc_major" == "4" ]] || [[ "$lxc_major" == "5" ]]; then
cat <<- CONFIG3 > $(dirname $GUEST_ROOT)/config
# Template used to create this container: /usr/share/lxc/templates/lxc-download
# Parameters passed to the template:
# Template script checksum (SHA-1): 740c51206e35463362b735e68b867876048a8baf
# For additional config options, please look at lxc.container.conf(5)
# Uncomment the following line to support nesting containers:
#lxc.include = /usr/share/lxc/config/nesting.conf
# (Be aware this has security implications)
# Distribution configuration
lxc.include = /usr/share/lxc/config/debian.common.conf
lxc.arch = $(uname -m)
# Container specific configuration
lxc.rootfs.path = /var/lib/lxc/$NAME/rootfs
lxc.uts.name = $NAME
# Network configuration
lxc.net.0.type = veth
lxc.net.0.link = lxc-bridge
lxc.net.0.flags = up
#lxc.net.0.ipv4.gateway = 10.0.8.1
#lxc.net.0.ipv4.address = 10.0.8.8
# External mounts
#lxc.mount.entry = /path/to/host/folder path/to/folder/in/container none bind 0 0
CONFIG3
else
echo_red "Unknown lxc version: $lxc_major"
echo_red "No configuration file is generated."
echo_yellow "For config options, please look at lxc.container.conf(5)"
fi
echo_green "done in $(($SECONDS - $START_TIME)) seconds..."
cat <<USAGE
to run the vm:
sudo lxc-start -n $NAME
to attach the root console:
sudo lxc-attach -n $NAME
to run GUI applications, make SSH with X forwarding:
ssh -X youruser@container-ip
---------------------------------------------------
NOTE:
* Configure network in /var/lib/lxc/$NAME/config
---------------------------------------------------
USAGE
# Open configuration file
[[ $open_config = true ]] && nano /var/lib/lxc/$NAME/config