~m-grant-prg/vm-container-utils/focal-trunk

« back to all changes in this revision

Viewing changes to src/prg/bash/lxcu/lxcu-copy.sh.in

  • Committer: Mark Grant
  • Date: 2020-11-29 14:25:58 UTC
  • mfrom: (1.1.6)
  • Revision ID: m.grant.prg@gmail.com-20201129142558-c2jhqvixk9hnylhj
Merge new upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! @bashlocation@
 
2
#########################################################################
 
3
#                                                                       #
 
4
#       lxcu-copy.sh is automatically generated,                        #
 
5
#               please do not modify!                                   #
 
6
#                                                                       #
 
7
#########################################################################
 
8
 
 
9
#########################################################################
 
10
#                                                                       #
 
11
# Script ID: lxcu-copy.sh                                               #
 
12
# Author: Copyright (C) 2020  Mark Grant                                #
 
13
#                                                                       #
 
14
# Released under the GPLv3 only.                                        #
 
15
# SPDX-License-Identifier: GPL-3.0                                      #
 
16
#                                                                       #
 
17
# Purpose:                                                              #
 
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.                                   #
 
21
#                                                                       #
 
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 ]                            #
 
26
#                                                                       #
 
27
# Exit codes used:-                                                     #
 
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   #
 
44
#                                       to retry                        #
 
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:                  #
 
49
#               None                                                    #
 
50
#                                                                       #
 
51
# Further Info:                                                         #
 
52
#                                                                       #
 
53
#########################################################################
 
54
 
 
55
#########################################################################
 
56
#                                                                       #
 
57
# Changelog                                                             #
 
58
#                                                                       #
 
59
# Date          Author  Version Description                             #
 
60
#                                                                       #
 
61
# 21/11/2020    MG      1.0.1   Created.                                #
 
62
#                                                                       #
 
63
#########################################################################
 
64
 
 
65
 
 
66
set -o pipefail
 
67
 
 
68
 
 
69
##################
 
70
# Init variables #
 
71
##################
 
72
readonly outputprefix="$(basename $0):"
 
73
 
 
74
readonly version=1.0.1                          # Script version
 
75
readonly packageversion=@pkgversion@            # Package version
 
76
 
 
77
readonly etclocation=@sysconfdir@/lxcu          # Path to config directory
 
78
 
 
79
 
 
80
#############
 
81
# Functions #
 
82
#############
 
83
 
 
84
# -h --help output.
 
85
# No parameters
 
86
# No return value
 
87
usage()
 
88
{
 
89
cat << EOF
 
90
Usage is $0 [options]
 
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
 
95
EOF
 
96
}
 
97
 
 
98
# Standard function to emit messages depending on various parameters.
 
99
# Parameters -  $1 What:-       The message to emit.
 
100
#               $2 Where:-      stdout == 0
 
101
#                               stderr == 1
 
102
# No return value.
 
103
output()
 
104
{
 
105
        if (( !$2 )); then
 
106
                printf "%s\n" "$1"
 
107
        else
 
108
                printf "%s\n" "$1" 1>&2
 
109
        fi
 
110
}
 
111
 
 
112
# Standard function to tidy up and return exit code.
 
113
# Parameters -  $1 is the exit code.
 
114
# No return value.
 
115
script_exit()
 
116
{
 
117
        exit $1
 
118
}
 
119
 
 
120
# Standard function to test command error and exit if non-zero.
 
121
# Parameters -  $1 is the exit code, (normally $? from the preceeding command).
 
122
# No return value.
 
123
std_cmd_err_handler()
 
124
{
 
125
        if (( $1 )); then
 
126
                script_exit $1
 
127
        fi
 
128
}
 
129
 
 
130
# Standard trap exit function.
 
131
# No parameters.
 
132
# No return value.
 
133
trap_exit()
 
134
{
 
135
        local -i exit_code=$?
 
136
        local msg
 
137
 
 
138
        msg="Script terminating with exit code $exit_code due to trap received."
 
139
        output "$msg" 1
 
140
        script_exit $exit_code
 
141
}
 
142
 
 
143
# Setup trap
 
144
trap trap_exit SIGHUP SIGINT SIGQUIT SIGTERM
 
145
 
 
146
# Process the config file just for the parameters of interest.
 
147
# Parameters - None
 
148
# No return value.
 
149
proc_config_file()
 
150
{
 
151
        local input=()
 
152
        local oldIFS=$IFS
 
153
 
 
154
        IFS="="
 
155
 
 
156
        exec 3<"$etclocation/lxcu.conf"
 
157
        while read -u3 -ra input; do
 
158
                case ${input[0]} in
 
159
                bridge)
 
160
                        bridge=${input[1]}
 
161
                        ;;
 
162
                contlocation)
 
163
                        contlocation=$(echo ${input[1]} | envsubst)
 
164
                        ;;
 
165
                esac
 
166
        done
 
167
        exec 3<&-
 
168
 
 
169
        IFS=$oldIFS
 
170
}
 
171
 
 
172
# Process command line arguments with GNU getopt.
 
173
# Parameters -  $1 is the command line.
 
174
# No return value.
 
175
proc_CL()
 
176
{
 
177
        local GETOPTTEMP
 
178
        local msg
 
179
        local tmp
 
180
 
 
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 $?
 
184
 
 
185
        eval set -- "$GETOPTTEMP"
 
186
        std_cmd_err_handler $?
 
187
 
 
188
        while true; do
 
189
                case "$1" in
 
190
                -h|--help)
 
191
                        usage
 
192
                        shift
 
193
                        script_exit 0
 
194
                        ;;
 
195
                -n|--from-name)
 
196
                        fromname=$2
 
197
                        shift 2
 
198
                        ;;
 
199
                -N|--to-name)
 
200
                        toname=$2
 
201
                        shift 2
 
202
                        ;;
 
203
                -V|--version)
 
204
                        printf "%s Script version %s\n" $0 $version
 
205
                        printf "%s Package version %s\n" $0 $packageversion
 
206
                        shift
 
207
                        script_exit 0
 
208
                        ;;
 
209
                --)     shift
 
210
                        break
 
211
                        ;;
 
212
                *)      output "Internal error." 1
 
213
                        script_exit 64
 
214
                        ;;
 
215
                esac
 
216
        done
 
217
 
 
218
        # Script does not accept other arguments.
 
219
        if (( $# > 0 )); then
 
220
                output "Invalid argument." 1
 
221
                script_exit 64
 
222
        fi
 
223
 
 
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."
 
227
                output "$msg" 1
 
228
                script_exit 64
 
229
        fi
 
230
}
 
231
 
 
232
 
 
233
########
 
234
# Main #
 
235
########
 
236
 
 
237
proc_config_file
 
238
 
 
239
proc_CL "$@"
 
240
 
 
241
lxc-copy -n $fromname -N $toname
 
242
std_cmd_err_handler $?
 
243
 
 
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 $?
 
255
fi
 
256
 
 
257
# Correct host name on target container.
 
258
lxcu-start.sh -n $toname
 
259
std_cmd_err_handler $?
 
260
 
 
261
lxc-attach -n $toname sudo sed -- -i -e "s/$fromname/$toname/" /etc/hostname
 
262
std_cmd_err_handler $?
 
263
 
 
264
lxc-attach -n $toname sudo sed -- -i -e "s/$fromname/$toname/" /etc/hosts
 
265
std_cmd_err_handler $?
 
266
 
 
267
lxc-stop -n $toname
 
268
std_cmd_err_handler $?
 
269
 
 
270
# And exit.
 
271
script_exit 0
 
272