-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_podman_directory.sh
executable file
·354 lines (264 loc) · 12.1 KB
/
migrate_podman_directory.sh
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/bash
# Determine toolpath if not set already
relativepath="./" # Define relative path to go from this script to the root level of the tool
if [[ ! -v toolpath ]]; then scriptpath=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ); toolpath=$(realpath --canonicalize-missing ${scriptpath}/${relativepath}); fi
# Load Configuration
source ${toolpath}/config.sh
# Load Functions
source ${toolpath}/functions.sh
# Abort if Script is NOT being Executed as Root
if [ "${EUID}" -ne 0 ]
then echo "Script MUST be run as root"
exit
fi
# User Name
user=${1}
#user=${1-"podman"}
# Source Directory
sourcedir=${2}
#sourcedir=${2-"/home/podman"}
# Destination Directory
destinationdir=${3}
#destinationdir=${3-"/home/podman/containers"}
# Create Destination Directory if Not Existing Yet
mkdir -p ${destinationdir}
chattr -i ${destinationdir}/*
chown -R ${user}:${user} ${destinationdir}
# Storage.conf File Location
# MUST still be available after all things have been unmounted
configrealpath=${4}
#configrealpath=${4-"/zdata/PODMAN/CONFIG"} # Contains storage.conf
# Paths for use in config files
# It's better to avoid using --rbind paths such as /home/podman/containers/storage for graphRoot and similar.
# Less issues when accessing directly /zdata/PODMAN/*
# The mountpoints is still available though
# If SET, the UPPERCASE Dataset values will be used (STORAGE, IMAGES, VOLUMES, ...)
# If UNSET, the LOWERCASE Dataset values will be used (storage, images, volumes, ...)
pathsforuseinconfigfiles=${5-""}
# Make sure that all mountpoints are mounted prior to performing migration
zfs mount -a
mount -a
# Get homedir
homedir=$(get_homedir "${user}")
# Get Systemdconfigdir
systemdconfigdir=$(get_systemdconfigdir "${user}")
# Save current path
currentpath=$(pwd)
# Stop all Running Containers based only on Podman Running Status
mapfile -t runninglist < <( podman ps --all --format="{{.Names}}" )
for container in "${runninglist[@]}"
do
echo "Disable & Stop Systemd Autostart Service for <${container}>"
# Define where service file would be located
#service="container-${container}"
service=$(podman inspect ${container} | jq -r '.[0].Config.Labels."PODMAN_SYSTEMD_UNIT"')
# Disable Service Temporarily
systemd_disable "${user}" "${service}"
# Stop Service
systemd_stop "${user}" "${service}"
done
# Stop all Containers based on Podman Compose file Structure
if [[ -d "${sourcedir}/compose/" ]]
then
for filepath in ${sourcedir}/compose/*
do
# Container is only the basename
container=$(basename ${filepath})
echo "Run podman-compose down for <${container}>"
# Determine Compose Directory
composedir=$(podman inspect ${container} | jq -r '.[0].Config.Labels."com.docker.compose.project.working_dir"')
# Change Directory
cd ${sourcedir}/compose/${container} || exit
# Bring Podman Container down
generic_cmd "${user}" "podman-compose" "down"
# Another Attempt
cd ${composedir} || exit
generic_cmd "${user}" "podman-compose" "down"
done
fi
# Stop simply using "podman" command if there are Containers which do NOT have a Systemd Service (yet) and were NOT generated using podman-compose
mapfile -t remaininglist < <( podman ps --all --format="{{.Names}}" )
for container in "${remaininglist[@]}"
do
echo "Stop Podman Container <${container}> using `podman` Command"
# Stop Container
generic_cmd "${user}" "podman" "stop" "${service}"
done
# Determine which Paths to use in storage.conf files
if [[ -z "${pathsforuseinconfigfiles}" ]]
then
storagepath="${destinationdir}/storage"
imagespath="${destinationdir}/images"
volumespath="${destinationdir}/volumes"
else
storagepath="${pathsforuseinconfigfiles}/STORAGE"
imagespath="${pathsforuseinconfigfiles}/IMAGES"
volumespath="${pathsforuseinconfigfiles}/VOLUMES"
fi
# Make changes to storage.conf
sed -Ei "s|^#? ?graphroot = \".*\"|graphroot = \"${storagepath}\"|g" "${configrealpath}/storage.conf"
sed -Ei "s|^#? ?rootless_storage_path = \".*\"|rootless_storage_path = \"${storagepath}\"|g" "${configrealpath}/storage.conf"
# Using Imagestore gives problems so make sure to Disable it in the Process !
sed -Ei "s|^#? ?imagestore = \".*\"|#imagestore = \"${imagespath}\"|g" "${configrealpath}/storage.conf"
# Make changes to registries.conf
# ...
# Make changes to containers.conf
# Also fix wrong "volumepath" syntax to the correct "volume_path"
sed -Ei "s|^#? ?volumepath = \".*\"|volume_path = \"${volumespath}\"|g" "${configrealpath}/storage.conf"
sed -Ei "s|^#? ?volume_path = \".*\"|volume_path = \"${volumespath}\"|g" "${configrealpath}/storage.conf"
# Unmount all mountpoints
zfs umount -a
umount -a
# Loop over Datasets
# Create Datasets
for dataset in "${datasets[@]}"
do
# Convert dataset name to lowercase mountpoint
lname=${dataset,,}
# Source Path
sourcepath="${sourcedir}/${lname}"
# Destination Path
destinationpath="${destinationdir}/${lname}"
# If dataset does not exist yet, create it
if [[ ! -d "${sourcepath}" ]]
then
mkdir -p "${sourcepath}"
chown -R ${user}:${user} ${sourcedir}/${lname}
fi
# Make it editable
chattr -i ${sourcepath}
# If folder has already been created on destination, attempt to remove it (if empty only !)
if [[ -d "${destinationpath}" ]]
then
# Echo
echo "Destination folder <${destinationpath}> already exists. Attempting to remove (only if EMPTY !)"
# Make it editable
chattr -i ${destinationpath}
# Attempt to Remove it
rmdir "${destinationpath}"
# Check Return Code
if [[ "$?" -ne 0 ]]
then
echo "FAILED to remove Destination folder <${destinationpath}>. Error code of `rmdir` was $?. Possible NON-EMPTY Directory !"
fi
fi
if [[ -d "${sourcepath}" ]]
then
# Move mountpoint if it still exists on the source
mv ${sourcepath} ${destinationpath}
else
# Create a new Directory from scratch
mkdir -p ${destinationpath}
fi
# Give User Ownership
chown -R ${user}:${user} ${destinationpath}
# Require that a partition is mounted there again
chattr +i "${destinationpath}"
# Make changes to /etc/fstab
sed -Ei "s|${sourcepath}|${destinationpath}|g" "/etc/fstab"
done
# Special Cases: "backup" and "restoretmp" are NOT using Local Storage
# Make it modifiable
make_mutable_if_exist "${sourcedir}/backup"
make_mutable_if_exist "${sourcedir}/restoretmp"
make_mutable_if_exist "${destinationdir}/backup"
make_mutable_if_exist "${destinationdir}/restortmp"
# Remove Directories in Source Folder if they still exist
rmdir_if_exist "${sourcedir}/backup"
rmdir_if_exist "${sourcedir}/restoretmp"
# Create Destination Directory if not exist already
mkdir -p "${destinationdir}/backup"
mkdir -p "${destinationdir}/restoretmp"
# Give User Ownership
chown -R ${user}:${user} "${destinationdir}/backup"
chown -R ${user}:${user} "${destinationdir}/restoretmp"
# Make them Immutable again
make_immutable_if_exist "${destinationdir}/backup"
make_immutable_if_exist "${destinationdir}/restoretmp"
# Make changes to /etc/fstab
sed -Ei "s|${sourcedir}/backup|${destinationdir}/backup|g" "/etc/fstab"
sed -Ei "s|${sourcedir}/restoretmp|${destinationdir}/restoretmp|g" "/etc/fstab"
# Load new FSTAB Configuration
systemctl daemon-reload
# Load new FSTAB Configuration as User
systemd_reload "${user}"
# Remount all mountpoints
zfs mount -a
mount -a
# Now All compose.yml files need to be updated
mapfile -t composefilelist < <( find ${destinationdir}/compose -type f \( -name "compose.yml" -o -name "docker-compose.yml" -o -name "container-compose.yml" \) )
# Also add backup & restoretmp to the list
mapfile+=("backup")
mapfile+=("restoretmp")
# Generate Timestamp
timestamp=$(date +"%Y%m%d-%H%M%S")
# For each Compose File
for composefile in "${composefilelist[@]}"
do
# Create backup before replacement
echo "Create Backup of ${composefile} in ${composefile}.backup.${timestamp}"
cp "${composefile}" "${composefile}.backup.${timestamp}"
# For each Dataset
for dataset in "${datasets[@]}"
do
# Convert dataset name to lowercase mountpoint
lname=${dataset,,}
# Absolute Path
originabsolutepath="${sourcedir}/${lname}"
destinationabsolutepath="${destinationdir}/${lname}"
# With trailing /
originabsolutepathwithtrailingslash="${originabsolutepath}/"
destinationabsolutepathwithtrailingslash="${destinationabsolutepath}/"
# Echo
#echo "Absolute Paths: ${originabsolutepath} -> ${destinationabsolutepath}"
#echo "Absolute Paths with Trailing Slash: ${originabsolutepathwithtrailingslash} -> ${destinationabsolutepathwithtrailingslash}"
#echo "Homedir: ${homedir}"
# Relative Path compared to Homedir
originrelativepath=$(realpath --canonicalize-missing ${originabsolutepathwithtrailingslash/${homedir}/""})
destinationrelativepath=$(realpath --canonicalize-missing ${destinationabsolutepathwithtrailingslash/${homedir}/""})
# Strip the additional slash
originrelativepath=$(remove_leading_trailing_slashes "${originrelativepath}")
destinationrelativepath=$(remove_leading_trailing_slashes "${destinationrelativepath}")
#echo "Relative Paths: ${originrelativepath} -> ${destinationrelativepath}"
# Echo
echo "Replace ~/${originrelativepath}/(.*) -> ~/${destinationrelativepath}/(.*)"
echo "Replace ${originabsolutepath}/(.*) -> ${destinationabsolutepath}/(.*)"
# We must both support:
# - ~/<dataset>:SOMETHING
# - /home/${user}:SOMETHING
# Temporary Fix: Fix wrong Indentation due to previous Error in sed expression
#sed -Ei "s|^(\s*)\-\s*?~/${originrelativepath}/(.*)| - ~/${originrelativepath}/\2|g" "${composefile}"
#sed -Ei "s|^(\s*)\-\s*?${originabsolutepath}/(.*)| - ${originabsolutepath}/\2|g" "${composefile}"
### Replace Volumes Section
# Simple Strings
sed -Ei "s|^(\s*)\-\s*?~/${originrelativepath}/(.*)$|\1- ~/${destinationrelativepath}/\2|g" "${composefile}"
sed -Ei "s|^(\s*)\-\s*?${originabsolutepath}/(.*)$|\1- ${destinationabsolutepath}/\2|g" "${composefile}"
# Within Single Quotes
sed -Ei "s|^(\s*)\-\s*?'~/${originrelativepath}/(.*)'(.*?)$|\1- '~/${destinationrelativepath}/\2'\3|g" "${composefile}"
sed -Ei "s|^(\s*)\-\s*?'${originabsolutepath}/(.*)'(.*?)$|\1- '${destinationabsolutepath}/\2'\3|g" "${composefile}"
# Within Double Quotes
sed -Ei "s|^(\s*)\-\s*?\"~/${originrelativepath}/(.*)\"(.*?)$|\1- \"~/${destinationrelativepath}/\2\"\3|g" "${composefile}"
sed -Ei "s|^(\s*)\-\s*?\"${originabsolutepath}/(.*)\"(.*?)$|\1- \"${destinationabsolutepath}/\2\"\3|g" "${composefile}"
#### Replace Secrets Section
# Simple Strings
sed -Ei "s|^(\s*)file:\s*?~/${originrelativepath}/(.*)$|\1file: ~/${destinationrelativepath}/\2|g" "${composefile}"
sed -Ei "s|^(\s*)file:\s*?${originabsolutepath}/(.*)$|\1file: ${destinationabsolutepath}/\2|g" "${composefile}"
# Within Single Quotes
sed -Ei "s|^(\s*)file:\s*?'~/${originrelativepath}/(.*)'(.*?)$|\1file: '~/${destinationrelativepath}/\2'\3|g" "${composefile}"
sed -Ei "s|^(\s*)file:\s*?'${originabsolutepath}/(.*)'(.*?)$|\1file: '${destinationabsolutepath}/\2'\3|g" "${composefile}"
# Within Double Quotes
sed -Ei "s|^(\s*)file:\s*?\"~/${originrelativepath}/(.*)\"(.*?)$|\1file: \"~/${destinationrelativepath}/\2\"\3|g" "${composefile}"
sed -Ei "s|^(\s*)file:\s*?\"${originabsolutepath}/(.*)\"(.*?)$|\1file: \"${destinationabsolutepath}/\2\"\3|g" "${composefile}"
done
done
# Reset podman as user
generic_cmd "${user}" "podman" "system" "reset"
# Remove remaining stuff in storage and images
rm -rf ${sourcedir}/storage/*
rm -rf ${sourcedir}/images/*
rm -rf ${destinationdir}/storage/*
rm -rf ${destinationdir}/images/*
# Regenerate Entries
systemd_reload "${user}"
# Shoud Reboot
echo "You should now Reboot !"