Files
infix/board/common/rootfs/usr/sbin/container
T
Joachim Wiberg 256c462c0c confd: add support for read-only containers
This creates a container without a writable layer.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2024-02-25 19:49:27 +01:00

241 lines
5.0 KiB
Bash
Executable File

#!/bin/sh
all=""
env=""
port=""
log()
{
logger -I $PPID -t container -p local1.notice -- "$*"
}
# shellcheck disable=SC2086
create()
{
name=$1
image=$2
shift 2
if [ -z "$name" ] || [ -z "$image" ]; then
echo "Usage:"
echo " container create NAME IMAGE"
exit 1
fi
args="$args --restart=no --systemd=false --tz=local --privileged --replace --quiet"
# args="$args --log-opt tag=$name"
pidfn=/run/container:${name}.pid
log "---------------------------------------"
log "Got name: $name image: $image"
log "Got networks: $network"
if [ -n "$network" ]; then
for srv in $dns; do
args="$args --dns=$srv"
done
for domain in $search; do
args="$args --dns-search=$domain"
done
else
network="--net none"
fi
# --syslog --log-level info
log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $ro $hostname $entrypoint $env $network $port $args $image $*\""
set -x
# shellcheck disable=SC2048
if podman create --name "$name" --conmon-pidfile="$pidfn" $ro $hostname $entrypoint $env $network $port $args "$image" $*; then
log "Successfully created container $name from $image"
rm -f "/run/containers/env/${name}.env"
initctl show "container:$name" |grep -q manual:yes || initctl -bq start "container:$name"
initctl -nbq cond set container:$name
exit 0
fi
log "Error: failed creating container $name, please check the configuration."
exit 1
}
delete()
{
name=$1
image=$2
if [ -z "$name" ]; then
echo "Usage:"
echo " container delete NAME"
exit 1
fi
podman kill -s KILL "$name" >/dev/null 2>&1
podman rm -vif "$name" 2>&1
log "Container $name has been removed."
}
usage()
{
cat <<EOF
usage:
container [opt] cmd [arg]
options:
-a, --all Show all, of something
--dns NAMESERVER Set nameserver(s) when creating a container
--dns-search LIST Set host lookup search list when creating container
-e, --env FILE Environment variables when creating container
--entrypoint Disable container image's ENTRYPOINT, run cmd + arg
-f, --force Force operation, e.g. remove
-h, --help Show this help text
--hostname NAME Set hostname when creating container
--net NETWORK Set network(s) when creating container
-p, --publish PORT Publish ports when creating container
Syntax: [[ip:][hostPort]:]containerPort[/protocol]
--read-only Do not create a writable layer
-s, --simple Show output in simplified format
commands:
create NAME IMAGE NET Create container NAME using IMAGE with networks NET
delete NAME Kill and remove container NAME
help Show this help text
list [image] List names (only) of containers or images
remove IMAGE Remove an (unused) container image
restart NAME Restart a crashed container
run NAME CMD Execute a command (interactively) in container
show [image] Show containers or container images
start NAME Start a container
stop NAME Stop a container
EOF
}
while [ "$1" != "" ]; do
case $1 in
-a | --all)
all="-a"
;;
--dns)
shift
dns="$dns $1"
;;
--dns-search)
shift
search="$search $1"
;;
-e | --env)
shift
env="$env --env-file=$1"
;;
--entrypoint)
entrypoint="--entrypoint=\"\""
;;
-f | --force)
force="-f"
;;
-h | --help)
usage
exit 0
;;
--hostname)
shift
hostname="--hostname $1"
;;
--net)
shift
network="$network --net $1"
;;
-p | --publish)
shift
port="$port -p $1"
;;
--read-only)
ro="--read-only=true"
;;
-s | --simple)
simple=true
;;
*)
break
;;
esac
shift
done
cmd=$1
if [ -n "$cmd" ]; then
shift
fi
case $cmd in
attach)
podman exec -it "$1" "$2"
;;
create)
log "Got create args: $*"
create "$@"
;;
delete)
delete "$@"
;;
help)
usage
;;
ls | list)
cmd=$1
shift
case $cmd in
image*)
podman images $all --format "{{.Repository}}:{{.Tag}}"
;;
*)
podman ps $all --format "{{.Names}}"
;;
esac
;;
pull)
podman pull "$@"
;;
remove)
podman rmi $all $force -i "$1"
;;
run)
echo "Starting container $1 :: use Ctrl-p Ctrl-q to exit"
podman run -it --rm "$@"
;;
show)
cmd=$1
shift
case $cmd in
image*)
if [ -n "$simple" ]; then
podman images $all --format "{{.Names}} {{.Size}}" \
| sed 's/\[\(.*\)\] /\1 /g' \
| awk '{ printf "%-60s %s %s\n", $1, $2, $3}'
else
podman images $all
fi
;;
*)
if [ -n "$simple" ]; then
podman ps $all --format "{{.ID}} {{.Names}} {{.Image}}" \
| awk '{ printf "%s %-30s %s\n", $1, $2, $3}'
else
podman ps $all
fi
;;
esac
;;
start)
initctl -bq start "container:$1"
;;
restart)
initctl -bq restart "container:$1"
;;
stop)
initctl -bq stop "container:$1" || podman kill "$1"
;;
*)
usage
exit 1
;;
esac