2
#########################################################################
4
# lxcu-copy.sh is automatically generated, #
5
# please do not modify! #
7
#########################################################################
9
#########################################################################
11
# Script ID: lxcu-copy.sh #
12
# Author: Copyright (C) 2020 Mark Grant #
14
# Released under the GPLv3 only. #
15
# SPDX-License-Identifier: GPL-3.0 #
18
# A script to copy an existing container to a new container. The only #
19
# extra thing this does compared to an lxc-copy is that it will create #
20
# any extra lxcu bridge config files. #
22
# Syntax: lxcu-copy.sh [ -h or --help #
23
# || -n or --from-name 'container name' copy from #
24
# || -N or --to-name 'container name' copy to #
25
# || -V or --version ] #
28
# Bash standard Exit Codes: 0 - success #
29
# 1 - general failure #
30
# User-defined exit code range is 64 - 113 #
31
# C/C++ Semi-standard exit codes from sysexits.h range is 64 - 78 #
32
# EX_USAGE 64 command line usage error #
33
# EX_DATAERR 65 data format error #
34
# EX_NOINPUT 66 cannot open input #
35
# EX_NOUSER 67 addressee unknown #
36
# EX_NOHOST 68 host name unknown #
37
# EX_UNAVAILABLE 69 service unavailable #
38
# EX_SOFTWARE 70 internal software error #
39
# EX_OSERR 71 system error (e.g., can't fork) #
40
# EX_OSFILE 72 critical OS file missing #
41
# EX_CANTCREAT 73 can't create (user) output file #
42
# EX_IOERR 74 input/output error #
43
# EX_TEMPFAIL 75 temp failure; user is invited #
45
# EX_PROTOCOL 76 remote error in protocol #
46
# EX_NOPERM 77 permission denied #
47
# EX_CONFIG 78 configuration error #
48
# User-defined (here) exit codes range 79 - 113: #
53
#########################################################################
55
#########################################################################
59
# Date Author Version Description #
61
# 21/11/2020 MG 1.0.1 Created. #
63
#########################################################################
72
readonly outputprefix="$(basename $0):"
74
readonly version=1.0.1 # Script version
75
readonly packageversion=@pkgversion@ # Package version
77
readonly etclocation=@sysconfdir@/lxcu # Path to config directory
91
-h or --help Display this help information
92
-n or --from-name 'container name'
93
-N or --to-name 'container name'
94
-V or --version Print script version information
98
# Standard function to emit messages depending on various parameters.
99
# Parameters - $1 What:- The message to emit.
100
# $2 Where:- stdout == 0
108
printf "%s\n" "$1" 1>&2
112
# Standard function to tidy up and return exit code.
113
# Parameters - $1 is the exit code.
120
# Standard function to test command error and exit if non-zero.
121
# Parameters - $1 is the exit code, (normally $? from the preceeding command).
123
std_cmd_err_handler()
130
# Standard trap exit function.
135
local -i exit_code=$?
138
msg="Script terminating with exit code $exit_code due to trap received."
140
script_exit $exit_code
144
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM
146
# Process the config file just for the parameters of interest.
156
exec 3<"$etclocation/lxcu.conf"
157
while read -u3 -ra input; do
163
contlocation=$(echo ${input[1]} | envsubst)
172
# Process command line arguments with GNU getopt.
173
# Parameters - $1 is the command line.
181
tmp="getopt -o hn:N:V --long dist:,help,from-name:,to-name:,version"
182
GETOPTTEMP=$($tmp -n "$0" -- "$@")
183
std_cmd_err_handler $?
185
eval set -- "$GETOPTTEMP"
186
std_cmd_err_handler $?
204
printf "%s Script version %s\n" $0 $version
205
printf "%s Package version %s\n" $0 $packageversion
212
*) output "Internal error." 1
218
# Script does not accept other arguments.
219
if (( $# > 0 )); then
220
output "Invalid argument." 1
224
# Both from name and to name must be specified.
225
if [[ -z $fromname || -z $toname ]]; then
226
msg="Both from name and to name must be specified."
241
lxc-copy -n $fromname -N $toname
242
std_cmd_err_handler $?
244
# Any bridge to provide a config file for?
245
if [[ -n $bridge ]]; then
246
cp -v $contlocation/$fromname/config-$bridge \
247
$contlocation/$toname
248
std_cmd_err_handler $?
249
sed -i -e "/lxc.rootfs.path = dir:/s/$fromname/$toname/" \
250
$contlocation/$toname/config-$bridge
251
std_cmd_err_handler $?
252
sed -i -e "/lxc.uts.name = /s/$fromname/$toname/" \
253
$contlocation/$toname/config-$bridge
254
std_cmd_err_handler $?
257
# Correct host name on target container.
258
lxcu-start.sh -n $toname
259
std_cmd_err_handler $?
261
lxc-attach -n $toname sudo sed -- -i -e "s/$fromname/$toname/" /etc/hostname
262
std_cmd_err_handler $?
264
lxc-attach -n $toname sudo sed -- -i -e "s/$fromname/$toname/" /etc/hosts
265
std_cmd_err_handler $?
268
std_cmd_err_handler $?