#!/bin/bash #================================================= # LXD HELPERS #================================================= ynh_lxc_stop () { local container_to_stop=$1 # (We also use timeout 30 in front of the command because sometime lxc # commands can hang forever despite the --timeout >_>...) timeout 30 lxc stop --timeout 15 $container_to_stop 2>/dev/null # If the command times out, then add the option --force if [ $? -eq 124 ]; then timeout 30 lxc stop --timeout 15 $container_to_stop --force 2>/dev/null fi } ynh_lxc_snapshot_exists () { local snapname=$1 lxc list --format json \ | jq -e --arg LXC_NAME $LXC_NAME --arg snapname $snapname \ '.[] | select(.name==$LXC_NAME) | .snapshots[] | select(.name==$snapname)' \ >/dev/null } ynh_lxc_snapshot_load () { local snapname=$1 log_debug "Loading snapshot $snapname ..." # Remove swap files before restoring the snapshot. CLEAN_SWAPFILES ynh_lxc_stop $LXC_NAME lxc restore $LXC_NAME $snapname lxc start $LXC_NAME _LXC_START_AND_WAIT $LXC_NAME } ynh_lxc_reset () { # If the container exists if lxc info $LXC_NAME >/dev/null 2>/dev/null; then # Remove swap files before deletting the continer CLEAN_SWAPFILES fi ynh_lxc_stop $LXC_NAME if lxc info $LXC_NAME >/dev/null 2>/dev/null; then local current_storage=$(lxc list $LXC_NAME --format json --columns b | jq '.[].expanded_devices.root.pool') swapoff "$(lxc storage get $current_storage source)/containers/$LXC_NAME/rootfs/swap" 2>/dev/null fi lxc delete $LXC_NAME --force 2>/dev/null }