Skip to content

Commit

Permalink
Migrate existing containers when Podman is updated
Browse files Browse the repository at this point in the history
This was triggered by changes to rootless Podman containers in
podman-1.3.0 [1]. Containers created with version 1.2.0 or older need
to be migrated.

[1] containers/podman#2935
  • Loading branch information
debarshiray committed May 20, 2019
1 parent 39806d9 commit 90151da
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions toolbox
Expand Up @@ -1122,6 +1122,73 @@ list_containers()
)


migrate()
(
configuration_directory="$HOME/.config/toolbox"
migrate_stamp="$configuration_directory/podman-system-migrate"

runtime_directory="$XDG_RUNTIME_DIR"/toolbox
migrate_lock="$runtime_directory"/migrate.lock

echo "$base_toolbox_command: checking if 'podman system migrate' exists" >&3

if ! $prefix_sudo podman system --help 2>&3 | grep "Migrate containers" >/dev/null 2>&3; then
echo "$base_toolbox_command: migration not needed: 'podman system migrate' doesn't exist" >&3
return 0
fi

if ! version=$($prefix_sudo podman version --format "{{.Version}}" 2>&3); then
echo "$base_toolbox_command: unable to migrate containers: Podman version couldn't be read" >&2
return 1
fi

if ! mkdir --parents "$configuration_directory" 2>&3; then
echo "$base_toolbox_command: unable to migrate containers: configuration directory not created" >&2
return 1
fi

# shellcheck disable=SC2174
if ! mkdir --mode 700 --parents "$runtime_directory" 2>&3; then
echo "$base_toolbox_command: unable to migrate containers: runtime directory not created" >&2
return 1
fi

exec 5>"$migrate_lock"
if ! flock 5 2>&3; then
echo "$base_toolbox_command: unable to migrate containers: migration lock not acquired" >&3
return 1
fi

if [ -f "$migrate_stamp" ] 2>&3; then
if grep "$version" "$migrate_stamp" >/dev/null 2>&3; then
echo "$base_toolbox_command: migration not needed: $version is unchanged" >&3
return 0
fi

if ! version_old=$(printf "%s\n" "$version" \
| cat "$migrate_stamp" - 2>&3 \
| sort --version-sort 2>&3 \
| head --lines 1 2>&3); then
echo "$base_toolbox_command: unable to migrate containers: Podman versions couldn't be sorted" >&2
return 1
fi

if [ "$version" = "$version_old" ] 2>&3; then
echo "$base_toolbox_command: migration not needed: $version is old" >&3
return 0
fi
fi

if ! $prefix_sudo podman system migrate >/dev/null 2>&3; then
echo "$base_toolbox_command: unable to migrate containers" >&2
return 1
fi

echo "$version" >"$migrate_stamp"
return 0
)


remove_containers()
(
ids=$1
Expand Down Expand Up @@ -1602,6 +1669,10 @@ if [ -f /run/.containerenv ] 2>&3; then
esac
fi

if ! migrate; then
exit 1
fi

case $op in
create )
while has_prefix "$1" -; do
Expand Down

0 comments on commit 90151da

Please sign in to comment.