-
Notifications
You must be signed in to change notification settings - Fork 2
/
inspect.docker
executable file
·681 lines (611 loc) · 20.7 KB
/
inspect.docker
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
#! /usr/bin/env bash
declare trace="${TRACE:-}"
# shellcheck disable=SC2034
declare debug="${DEBUG:-}"
set -eu
set -o pipefail
# If NO_REPO_MASKS is set in the environment then propagate it here so that we
# are able to inspect a container with no masked repos without further hacking!
export NO_REPO_MASKS
[[ -n "${trace:-}" ]] && set -o xtrace
cd "$( dirname "$( readlink -e "${0}" )" )" || exit 1
# Set by common/vars.sh, sourced below...
#
#declare build_name=''
# shellcheck disable=SC1091
[[ ! -s common/vars.sh ]] || . common/vars.sh
# shellcheck disable=SC2034 # Set from common/vars.sh
[[ -n "${__COMMON_VARS_INCLUDED:-}" ]] || {
echo >&2 'FATAL: Inclusion of common defaults failed'
exit 1
}
# shellcheck disable=SC2154
declare IMAGE="${build_name}:latest"
# shellcheck disable=SC1091
[[ -s common/run.sh ]] && . common/run.sh >/dev/null
# Provide a '--name' to prevent the container being removed on exit.
# Useful use-cases:
#for s in $(
# podman image ls |
# grep '^localhost.*service' |
# cut -d'/' -f 2- |
# sed 's/\s\+/:/g' |
# cut -d':' -f 1-2
#); do
# ./inspect.docker --remove --image "${s}" \
# 'ls -d /var/db/pkg/*/*systemd* /var/db/pkg/*/*udev*'
#done
if [[ -n "${*:-}" ]]; then
if grep -Eq -- ' -(h|-help) ' <<<" ${*:-} "; then
output >&2 "Usage: $( basename "${0}" ) [--image=<image>]" \
'[--name=<name>] [--remove] [--mount=<...>]' \
'[--memory-reservation=<size>] [--memory-limit=<size>]' \
'[--swap-limit=<size>] [--device=<...>] [commands]'
exit 0
fi
fi
declare _output=''
# shellcheck disable=SC2154
if ! [[ -x "$( type -pf "${_command}" )" ]]; then
echo >&2 "FATAL: Cannot locate binary '${_command}'"
exit 1
elif ! _output="$( "${_command}" info 2>&1 )"; then
if [[ "${_command}" == 'podman' ]]; then
echo >&2 "FATAL: Unable to successfully execute" \
"'${_command}' - do you need to run '${_command}" \
"machine start' or re-run '$( basename "${0}" )' as" \
"'root'?"
else
echo >&2 "FATAL: Unable to successfully execute" \
"'${_command}' - do you need to re-run" \
"'$( basename "${0}" )' as 'root'?"
fi
exit 1
elif (( $( id -u ) )) && grep -Fq -- 'rootless: false' <<<"${_output}"; then
echo >&2 "FATAL: Please re-run '$( basename "${0}")' as user 'root'"
exit 1
fi
unset _output
declare -a args=() container_args=() mount=() device=()
# declared in _docker_setup
#declare image='' name=''
_docker_parse() {
local arg='' next=''
for arg in "${@:-}"; do
if [[ -n "${next:-}" ]]; then
if [[ "${next}" == 'name' ]]; then
name="${arg}"
print "Setting container name to '${name}' in $( basename -- "${0#"-"}" )"
elif [[ "${next}" == 'image' ]]; then
image="${arg}"
elif [[ "${next}" == 'mount' ]]; then
mount+=( "${arg}" )
elif [[ "${next}" = 'device' ]]; then
device+=( "${arg}" )
fi
next=''
# Name components may contain lowercase characters, digits and
# separators. A separator is defined as a period, one or two
# underscores, or one or more dashes. A name component may not start or
# end with a separator.
#
elif grep -Eq -- '^-(n|-name)(=[a-z0-9]+(([.]|[_]{1,2}|[-]+)[a-z0-9]+)*)?$' <<<"${arg}"; then
if [[ "${arg}" == *=* ]]; then
name="$( echo "${arg}" | cut -d'=' -f 2- )"
print "Setting container name to '${name}' in $( basename -- "${0#"-"}" )"
else
next='name'
fi
# ... a tag name may contain lowercase and uppercase characters,
# digits, underscores, periods and dashes. A tag name may not start
# with a period or a dash and may contain a maximum of 128 characters.
#
elif grep -Eq -- '^-(i|-image)(=[a-z0-9]+(([.]|[_]{1,2}|[-]+)[a-z0-9]+)*(:[a-zA-Z0-9_][a-zA-Z0-9_.-]*)?)?$' <<<"${arg}"; then
if [[ "${arg}" == *=* ]]; then
image="$( echo "${arg}" | cut -d'=' -f 2- )"
else
next='image'
fi
elif grep -Eq -- '^-(m|-mount)(=.*)?$' <<<"${arg}"; then
if [[ "${arg}" == *=* ]]; then
mount+=( "$( echo "${arg}" | cut -d'=' -f 2- )" )
else
next='mount'
fi
elif grep -Eq -- '^-(d|-device)(=.*)?$' <<<"${arg}"; then
if [[ "${arg}" == *=* ]]; then
device+=( "$( echo "${arg}" | cut -d'=' -f 2- )" )
else
next='device'
fi
elif grep -Eq -- '^-(r|-rm|-remove)$' <<<"${arg}"; then
rm=1
else
args+=( "${arg}" )
fi
done
if [[ -n "${next:-}" ]]; then
warn "Missing value for argument '--${next}'"
fi
(( ${#device[@]} )) && DOCKER_DEVICES="${device[*]}"
for arg in "${args[@]:-}"; do
container_args+=( "${arg}" )
done
export name image DOCKER_DEVICES
export -a mount device container_args
unset arg next
} # _docker_parse
# shellcheck disable=SC2120
docker_run() {
local -a mirrormountpoints=()
local -a mirrormountpointsro=()
local -a runargs=()
local -a files=()
local -A mountpoints=()
local -A mountpointsro=()
local -i skipped=0
local -i runrc=0
local mp='' src='' # cwd=''
local default_repo_path='' rw_repo_path=''
local default_distdir_path='' default_pkgdir_path=''
#${arr[@]+"${arr[@]}"}
(( 0 == ${#args[@]} )) && args=( 'sh' )
# shellcheck disable=SC2207
runargs=(
$(
# shellcheck disable=SC2015
if
[[ "$( uname -s )" != 'Darwin' ]] &&
(( $( nproc ) > 1 )) &&
$_command info 2>&1 |
grep -q -- 'cpuset'
then
echo "--cpuset-cpus 1-$(( $( nproc ) - 1 ))"
fi
)
--init
--name "${name}"
--privileged
#--network slirp4netns
# Some code such as podman's go code tries to fetch packages from
# IPv6-addressable hosts...
--network host
--pids-limit 1024
$( add_arg rm --%% )
--ulimit nofile=1024:1024
)
# shellcheck disable=SC2206,SC2207
runargs+=(
--entrypoint '/bin/sh'
${DOCKER_DEVICES:-}
$( add_arg ACCEPT_KEYWORDS --env %% )
$( add_arg FEATURES --env %% )
$( add_arg TERM --env %% )
$( add_arg USE --env %% )
--interactive
--tty
${DOCKER_EXTRA_MOUNTS:-}
${DOCKER_VOLUMES:-}
)
if [[ -r /proc/cgroups ]] && grep -q -- '^memory.*1$' /proc/cgroups &&
[[ -n "${PODMAN_MEMORY_RESERVATION:-}" || -n "${PODMAN_MEMORY_LIMIT}" || -n "${PODMAN_SWAP_LIMIT}" ]]
then
# shellcheck disable=SC2207
runargs+=(
$( add_arg PODMAN_MEMORY_RESERVATION --memory-reservation "${PODMAN_MEMORY_RESERVATION:-}" )
$( add_arg PODMAN_MEMORY_LIMIT --memory "${PODMAN_MEMORY_LIMIT:-}" )
$( add_arg PODMAN_SWAP_LIMIT --memory-swap "${PODMAN_SWAP_LIMIT:-}" )
)
fi
# If 'portageq' is not available, then ensure that all of the variables
# referenced immedaitely prior are set so that it never needs to be
# called.
#
# For inspect.docker, mount additional repos read-write so that digest
# generation, for example, isn't an extended and laborious task.
#
if ! type -pf portageq >/dev/null 2>&1; then
default_repo_path='/var/db/repos/gentoo'
rw_repo_path='/var/db/repos/srcshelton'
default_distdir_path='/var/cache/portage/dist'
default_pkgdir_path="/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}"
if [ ! -d /var/db/repos/gentoo ] && [ -d /var/db/repo/gentoo ]; then
default_repo_path='/var/db/repo/gentoo'
rw_repo_path='/var/db/repo/srcshelton'
fi
[ -d "${rw_repo_path:-}" ] ||
rw_repo_path=''
fi
if [ -n "${PKGDIR_OVERRIDE:-}" ]; then
default_pkgdir_path="${PKGDIR_OVERRIDE}"
fi
# shellcheck disable=SC2046,SC2206,SC2207
mirrormountpointsro=(
# We need write access to be able to update eclasses...
#/etc/portage/repos.conf
"${default_repo_path:-"$( # <- Syntax
portageq get_repo_path "${EROOT:-"/"}" $(
portageq get_repos "${EROOT:-"/"}"
)
)"}"
/etc/locale.gen # FIXME: Commented in common/run.sh?
/usr/src # Used to affect kernel-builds, now seems fine?
)
# shellcheck disable=SC2206
mirrormountpoints=(
#/var/cache/portage/dist
"${default_distdir_path:-"$( portageq distdir )"}"
${rw_repo_path:-}
'/var/log/portage'
)
#ENV PKGDIR="${PKGCACHE:-"/var/cache/portage/pkg"}/${ARCH:-"amd64"}/${PKGHOST:-"docker"}"
#local PKGCACHE="${PKGCACHE:="/var/cache/portage/pkg"}"
#local PKGHOST="${PKGHOST:="docker"}"
local PKGDIR="${PKGDIR:="${default_pkgdir_path:-"$( portageq pkgdir )"}"}"
# Allow use of 'ARCH' variable as an override...
print "Using architecture '${ARCH:-"${arch}"}' ..."
mountpoints["${PKGDIR}"]="/var/cache/portage/pkg/${ARCH:-"${arch}"}/${PKGHOST:-"docker"}"
mountpointsro['/etc/portage/repos.conf']='/etc/portage/repos.conf.host'
[ -S /var/run/syslog-ng/log ] && mountpoints['/var/run/syslog-ng/log']='/dev/log'
cwd="$( dirname "$( readlink -e "${BASH_SOURCE[$(( ${#BASH_SOURCE[@]} - 1 ))]}" )" )"
print "Volume/mount base directory is '${cwd}'"
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.accept_keywords"]='/etc/portage/package.accept_keywords'
if [ -s "${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.accept_keywords.${ARCH:-"${arch}"}" ]; then
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.accept_keywords.${ARCH:-"${arch}"}"]="/etc/portage/package.accept_keywords/${ARCH:-"${arch}"}"
fi
declare mask_file=''
if [[ -n "${arch:-}" ]] &&
[[ -f "${cwd}/${base_dir:+"${base_dir}/"}etc/portage/profile/use.mask.${arch}" ]]
then
mask_file="${cwd}/${base_dir:+"${base_dir}/"}etc/portage/profile/use.mask.${arch}"
fi
[[ -z "${mask_file:-}" ]] &&
mask_file="${cwd}/${base_dir:+"${base_dir}/"}etc/portage/profile/use.mask"
if [ -s "${mask_file}" ]; then
mountpointsro["${mask_file}"]='/etc/portage/profile/use.mask'
fi
unset mask_file
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/profile/package.use.mask"]='/etc/portage/profile/package.use.force'
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/profile/package.use.mask"]='/etc/portage/profile/package.use.mask'
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.license"]='/etc/portage/package.license'
mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.use.build"]='/etc/portage/package.use'
#mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.unmask"]='/etc/portage/package.unmask'
#mountpointsro["${cwd}/${base_dir:+"${base_dir}/"}etc/portage/package.unmask/package.unmask"]='/etc/portage/package.unmask/host.unmask'
#if [ -s "/lib/modules/$( uname -r )/build/arch/x86/boot/vmlinux.bin" ]; then
# mountpointsro["/lib/modules/$( uname -r )/build/arch/x86/boot/vmlinux.bin"]="/boot/vmlinux-$( uname -r )"
#fi
if [ -s "/lib/modules/$( uname -r )/vmlinux-$( uname -r )" ]; then
mirrormountpointsro+=( "/lib/modules/$( uname -r )/vmlinux-$( uname -r )" )
fi
local mps=''
for mps in ${mirrormountpointsro[@]+"${mirrormountpointsro[@]}"}; do
[ -n "${mps:-}" ] || continue
for mp in ${mps}; do
src="$( readlink -e "${mp}" )" || print "readlink() for mirrored read-only mountpoint '${mp}' failed: ${?}"
if [ -z "${src:-}" ]; then
warn "$( basename "${0}" ) skipping mountpoint '${mp}'"
: $(( skipped = skipped + 1 ))
continue
fi
runargs+=( --mount "type=bind,source=${src},destination=${mp}${docker_readonly:+",${docker_readonly}"}" )
done
done
for mps in ${mirrormountpoints[@]+"${mirrormountpoints[@]}"}; do
[ -n "${mps:-}" ] || continue
for mp in ${mps}; do
src="$( readlink -e "${mp}" )" || print "readlink() for mirrored mountpoint '${mp}' failed: ${?}"
if [ -z "${src:-}" ]; then
warn "$( basename "${0}" ) skipping mountpoint '${mp}'"
: $(( skipped = skipped + 1 ))
continue
fi
runargs+=( --mount "type=bind,source=${src},destination=${mp}" )
done
done
for mps in ${mountpointsro[@]+"${!mountpointsro[@]}"}; do
[ -n "${mps:-}" ] || continue
for mp in ${mps}; do
src="$( readlink -e "${mp}" )" || print "readlink() for read-only mountpoint '${mp}' failed: ${?}"
if [ -z "${src:-}" ]; then
warn "$( basename "${0}" ) skipping mountpoint '${mp}' -> '${mountpointsro[${mp}]}'"
: $(( skipped = skipped + 1 ))
continue
fi
runargs+=( --mount "type=bind,source=${src},destination=${mountpointsro[${mp}]}${docker_readonly:+",${docker_readonly}"}" )
done
done
for mps in ${mountpoints[@]+"${!mountpoints[@]}"}; do
[ -n "${mps:-}" ] || continue
for mp in ${mps}; do
src="$( readlink -e "${mp}" )" || print "readlink() for mountpoint '${mp}' failed (do you need to set 'PKGDIR'?): ${?}"
if [ -z "${src:-}" ]; then
warn "$( basename "${0}" ) skipping mountpoint '${mp}' -> '${mountpoints[${mp}]}'"
: $(( skipped = skipped + 1 ))
continue
fi
runargs+=( --mount "type=bind,source=${src},destination=${mountpoints[${mp}]}" )
done
done
if [ $(( skipped )) -ge 1 ]; then
warn "${skipped} mount-points not connected to container"
sleep 5
fi
runargs+=( --mount 'type=tmpfs,destination=/run,ro=false,tmpfs-size=10M,tmpfs-mode=0755' )
if (( ${#mount[@]} )); then
for mp in "${mount[@]}"; do
if [[ -n "${mp:-}" ]]; then
print "Adding command line mount option '--mount ${mp}' ..."
runargs+=( --mount "${mp}" )
fi
done
fi
unset src mps mp
if [ -n "${DOCKER_VERBOSE:-}" ]; then
output
[ -n "${DOCKER_VARS:-}" ] && output "VERBOSE: DOCKER_VARS is '${DOCKER_VARS}'"
local arg='' next=''
for arg in "${runargs[@]}"; do
case "${next}" in
mount)
arg="$(
sed -r \
-e 's/^type=/type: /' \
-e 's/,(src|source)=/\tsource: /' \
-e 's/,(dst|destination)=/\tdestination: /' \
-e 's/, ro=true$/\tRO/' \
<<<"${arg}"
)"
output "VERBOSE: Mount point '${arg}'"
;;
volume)
output "VERBOSE: Volume '${arg}'"
;;
esac
if [[ "${arg}" =~ ^--(mount|volume)$ ]]; then
next="${arg#"--"}"
else
next=''
fi
done | column -t -s $'\t'
unset next arg
output
fi
(
if (( debug )); then
local arg=''
print "Starting container with command '$_command container run \\"
for arg in "${runargs[@]}"; do
case "${arg}" in
--*) print " ${arg} \\" ;;
*) print " ${arg} \\" ;;
esac
done
print " ${image}${*:+" \\"}"
for arg in "${@:-}"; do
[[ -n "${arg:-}" ]] && print " ${arg} \\"
done
print "'"
unset arg
if touch inspect.docker.debug.log; then
cat >inspect.docker.debug.log <<-EOF
#! /bin/sh
set -eux
EOF
printf >>inspect.docker.debug.log '%s container run \\\n' "${_command}"
for arg in "${runargs[@]}"; do
printf >>inspect.docker.debug.log ' %s \\\n' "${arg}"
done
unset arg
printf >>inspect.docker.debug.log ' %s \\\n' "${image}"
# Start at $1 as $0 is the command itself...
local -i i=1
for (( ; i < ${#} ; i++ )); do
printf >>inspect.docker.debug.log ' %s \\\n' "${!i:-}"
done
# At this point i == ${#}...
printf >>inspect.docker.debug.log ' %s\n' "${!i:-}"
unset i
fi
fi
# shellcheck disable=SC2086
$_command \
${DOCKER_VARS:-} \
container run \
"${runargs[@]}" \
"${image}" -c "${args[@]}"
)
runrc=${?}
for mp in \
${mirrormountpointsro[@]+"${mirrormountpointsro[@]}"} \
${mirrormountpoints[@]+"${mirrormountpoints[@]}"} \
${mountpointsro[@]+"${!mountpointsro[@]}"} \
${mountpoints[@]+"${!mountpoints[@]}"}
do
[ -n "${mp:-}" ] || continue
files+=( "${mp}" )
done
for mp in ${mountpointsro[@]+"${!mountpointsro[@]}"}; do
[ -n "${mp:-}" ] || continue
mp="${mountpointsro[${mp}]}" || continue
files+=( "${cwd}/${base_dir:+"${base_dir}"}${mp}" )
done
for mp in ${mountpoints[@]+"${!mountpoints[@]}"}; do
[ -n "${mp:-}" ] || continue
mp="${mountpoints[${mp}]}" || continue
files+=( "${cwd}/${base_dir:+"${base_dir}"}${mp}" )
done
files+=( /etc/portage/package.accept_keywords/arm64 )
for mp in "${files[@]}"; do
src="$( readlink -e "${mp}" )"
if [ -n "${src:-}" ]; then
if [[ -f "${src}" && "$( stat -c '%s %u %a' "${src}" )" == '0 0 700' ]]; then
warn "Artefact '${src}' detected - removing"
rm -f "${src}"
fi
fi
done
unset files
for oom in oom "${cwd}/${base_dir:+"${base_dir}/"}oom"; do
src="$( readlink -e "${oom}" )"
if [ -n "${src:-}" ]; then
if [[ -f "${src}" && "$( stat -c '%s %u %a' "${src}" )" == '0 0 644' ]]; then
warn "Artefact '${src}' detected - removing"
rm -f "${src}"
fi
fi
done
# shellcheck disable=SC2086
return ${runrc}
} # docker_run
docker_trap() {
trap '' INT
podman ${@+"${@}"}
trap - INT
} # docker_trap
main() {
local arg=''
local -a main_args=()
# N.B. _docker_parse does most of the argument-parsing...
if [[ -n "${*:-}" ]]; then
for arg in "${@:-}"; do
case "${arg:-}" in
--memory-reservation=*)
PODMAN_MEMORY_RESERVATION="${arg#*"="}"
;;
--memory-limit=*)
PODMAN_MEMORY_LIMIT="${arg#*"="}"
;;
--swap-limit=*)
PODMAN_SWAP_LIMIT="${arg#*"="}"
;;
*)
main_args+=( "${arg}" )
;;
esac
done
set -- "${main_args[@]:-}"
#print "args: (${#}) '${*:-}'"
fi
unset main_args arg
if [[ -z "${NO_MEMORY_LIMITS:-}" ]]; then
# Tiny
#: "${PODMAN_MEMORY_RESERVATION:="256m"}"
#: "${PODMAN_MEMORY_LIMIT:="512m"}"
#: "${PODMAN_SWAP_LIMIT:="1g"}"
# Small
#: "${PODMAN_MEMORY_RESERVATION:="512m"}"
#: "${PODMAN_MEMORY_LIMIT:="1g"}"
#: "${PODMAN_SWAP_LIMIT:="2g"}"
# Medium
#: "${PODMAN_MEMORY_RESERVATION:="1g"}"
#: "${PODMAN_MEMORY_LIMIT:="2g"}"
#: "${PODMAN_SWAP_LIMIT:="4g"}"
# Large
#: "${PODMAN_MEMORY_RESERVATION:="2g"}"
#: "${PODMAN_MEMORY_LIMIT:="4g"}"
#: "${PODMAN_SWAP_LIMIT:="8g"}"
# Extra-Large
#: "${PODMAN_MEMORY_RESERVATION:="4g"}"
#: "${PODMAN_MEMORY_LIMIT:="8g"}"
#: "${PODMAN_SWAP_LIMIT:="16g"}"
#
#: "${PODMAN_MEMORY_RESERVATION:="4g"}"
#: "${PODMAN_MEMORY_LIMIT:="6g"}"
#
# See comments in common/vars.sh...
#
: "${PODMAN_MEMORY_RESERVATION:="2g"}"
: "${PODMAN_MEMORY_LIMIT:="6g"}"
: "${PODMAN_SWAP_LIMIT:="${PODMAN_MEMORY_LIMIT}"}"
# FIXME: Assume that PODMAN_{SWAP_LIMIT,MEMORY_{LIMIT,RESERVATION}}
# have the same order of magnitude...
local -i swp=0 ram=0 changed=0
local unit='' divider=''
case "${PODMAN_MEMORY_LIMIT^^}" in
*M)
unit='m'
divider='1024'
;;
*G)
unit='g'
divider='1024 / 1024'
;;
esac
if [[ -n "${unit:-}" ]]; then
# shellcheck disable=SC2004
eval swp=$(( ( $( grep -m 1 'SwapTotal:' /proc/meminfo | awk '{ print $2 }' ) + 16 ) / ${divider} ))
# shellcheck disable=SC2004
eval ram=$(( $( grep -m 1 'MemTotal:' /proc/meminfo | awk '{ print $2 }' ) / ${divider} ))
# shellcheck disable=SC2295
if (( ram < ${PODMAN_MEMORY_LIMIT%[${unit,,}${unit^^}]} )) || (( ( ram + swp ) < ${PODMAN_SWAP_LIMIT%[${unit,,}${unit^^}]} )); then
output >&2 "INFO: Host resources (rounded down to nearest 1${unit^^}iB):"
output >&2 " RAM: ${ram}${unit^^}"
output >&2 " Swap: ${swp}${unit^^}"
output >&2 'INFO: Original memory limits:'
# shellcheck disable=SC2295
output >&2 " Soft limit: ${PODMAN_MEMORY_RESERVATION%[${unit,,}${unit^^}]}${unit^^}"
# shellcheck disable=SC2295
output >&2 " Hard limit: ${PODMAN_MEMORY_LIMIT%[${unit,,}${unit^^}]}${unit^^}"
# shellcheck disable=SC2295
output >&2 " RAM + Swap: ${PODMAN_SWAP_LIMIT%[${unit,,}${unit^^}]}${unit^^}"
fi
# shellcheck disable=SC2295
if (( ram < ${PODMAN_MEMORY_LIMIT%[${unit,,}${unit^^}]} )); then
PODMAN_MEMORY_RESERVATION="$(( ram - 1 ))${unit,,}"
PODMAN_MEMORY_LIMIT="$(( ram ))${unit,,}"
#PODMAN_SWAP_LIMIT="$(( ram + swp ))${unit,,}"
if (( ram <= 1 )); then
PODMAN_SWAP_LIMIT="$(( ram * 2 ))${unit,,}"
else
PODMAN_SWAP_LIMIT="$(( ram + $(
awk -v ram="${ram}" 'BEGIN{ print int( sqrt( ram ) + 0.5 ) }'
) ))${unit,,}"
fi
changed=1
fi
# shellcheck disable=SC2295
if (( ( ram + swp ) < ${PODMAN_SWAP_LIMIT%[${unit,,}${unit^^}]} )); then
#PODMAN_SWAP_LIMIT="$(( ram + swp ))${unit,,}"
if (( ram <= 1 )); then
PODMAN_SWAP_LIMIT="$(( ram * 2 ))${unit,,}"
else
PODMAN_SWAP_LIMIT="$(( ram + $(
awk -v ram="${ram}" 'BEGIN{ print int( sqrt( ram ) + 0.5 ) }'
) ))${unit,,}"
fi
changed=1
fi
if (( changed )); then
output >&2 'NOTE: Changed memory limits based on host configuration:'
# shellcheck disable=SC2295
output >&2 " Soft limit: ${PODMAN_MEMORY_RESERVATION%[${unit,,}${unit^^}]}${unit^^}"
# shellcheck disable=SC2295
output >&2 " Hard limit: ${PODMAN_MEMORY_LIMIT%[${unit,,}${unit^^}]}${unit^^}"
# shellcheck disable=SC2295
output >&2 " RAM + Swap: ${PODMAN_SWAP_LIMIT%[${unit,,}${unit^^}]}${unit^^}"
output >&2
fi
fi
unset changed ram swp divider unit
fi
declare rm=''
_docker_setup
_docker_parse ${@+"${@}"}
image="${image:-"${IMAGE}"}"
# shellcheck disable=SC2001 # POSIX sh compatibility
name="${name:-"$( echo "${image}" | sed 's|[:/]|_|g' )-inspect"}"
if [ -n "${rm:-}" ]; then
docker_trap rm --volumes "${name}" 2>/dev/null || :
fi
local -i rc=0
output >&2 "Launching container '${name}' from image '${image}' ..."
#set -o xtrace
docker_run "${container_args[@]:-}" || rc=${?}
#if [ -n "${rm:-}" ]; then
# docker_trap rm --volumes "${name}"
#fi
return ${rc}
} # main
main "${@:-}"
exit ${?}
# vi: set colorcolumn=80 foldmarker=()\ {,}\ \ #\ foldmethod=marker syntax=bash: