|
93 | 93 | } |
94 | 94 |
|
95 | 95 | pool_remove() { |
96 | | - sudo dmsetup remove "${dm_device}-snap-"* || true |
97 | | - sudo dmsetup remove \ |
98 | | - "${dm_device}" \ |
99 | | - "${dm_device}_tdata" "${dm_device}_tmeta" || true |
100 | | - sudo lvremove -f "$dm_device" |
| 96 | + # Find and remove individual snapshots with retry logic |
| 97 | + for snapshot in $(sudo dmsetup ls | grep "^$(basename ${dm_device})-snap-" | awk '{print $1}' | sort -r); do |
| 98 | + echo "Attempting to remove snapshot: $snapshot" |
| 99 | + local retries=5 |
| 100 | + while [ $retries -gt 0 ]; do |
| 101 | + if sudo dmsetup remove "$snapshot" 2>/dev/null; then |
| 102 | + echo "Successfully removed snapshot: $snapshot" |
| 103 | + break |
| 104 | + else |
| 105 | + echo "Snapshot $snapshot busy, waiting... (retries left: $retries)" |
| 106 | + sleep 2 |
| 107 | + retries=$((retries - 1)) |
| 108 | + fi |
| 109 | + done |
| 110 | + |
| 111 | + if [ $retries -eq 0 ]; then |
| 112 | + echo "Warning: Failed to remove snapshot $snapshot after retries" |
| 113 | + # Force remove by checking what's using it |
| 114 | + sudo dmsetup info "$snapshot" 2>/dev/null || true |
| 115 | + sudo lsof "$snapshot" 2>/dev/null || true |
| 116 | + sudo dmsetup remove --force "$snapshot" || true |
| 117 | + fi |
| 118 | + done |
| 119 | + |
| 120 | + # Remove the thin pool with retries |
| 121 | + local pool_retries=5 |
| 122 | + while [ $pool_retries -gt 0 ]; do |
| 123 | + if sudo dmsetup remove "${dm_device}" 2>/dev/null; then |
| 124 | + echo "Successfully removed thin pool: ${dm_device}" |
| 125 | + break |
| 126 | + else |
| 127 | + echo "Thin pool ${dm_device} busy, waiting... (retries left: $pool_retries)" |
| 128 | + sleep 2 |
| 129 | + pool_retries=$((pool_retries - 1)) |
| 130 | + fi |
| 131 | + done |
| 132 | + |
| 133 | + if [ $pool_retries -eq 0 ]; then |
| 134 | + echo "Warning: Failed to remove thin pool after retries" |
| 135 | + sudo dmsetup info "${dm_device}" 2>/dev/null || true |
| 136 | + sudo lsof "${dm_device}" 2>/dev/null || true |
| 137 | + fi |
| 138 | + |
| 139 | + # Clean up metadata and data devices |
| 140 | + sudo dmsetup remove "${dm_device}_tdata" 2>/dev/null || true |
| 141 | + sudo dmsetup remove "${dm_device}_tmeta" 2>/dev/null || true |
| 142 | + |
| 143 | + # Finally remove the logical volume |
| 144 | + sudo lvremove -f "$dm_device" 2>/dev/null || true |
101 | 145 | } |
102 | 146 |
|
103 | 147 | pool_reset() { |
104 | 148 | if [ -e "${dm_device}" ]; then |
| 149 | + # Wait for containerd to finish any pending cleanup operations |
| 150 | + echo "Waiting for containerd to finish cleanup operations..." |
| 151 | + |
| 152 | + # Use containerd client to clean up any remaining snapshots |
| 153 | + if command -v ctr >/dev/null 2>&1; then |
| 154 | + echo "Cleaning up containerd snapshots..." |
| 155 | + # List and remove any active snapshots using containerd API |
| 156 | + for snap in $(ctr --address /run/firecracker-containerd/containerd.sock snapshots list -q 2>/dev/null || true); do |
| 157 | + echo "Removing containerd snapshot: $snap" |
| 158 | + ctr --address /run/firecracker-containerd/containerd.sock snapshots remove "$snap" 2>/dev/null || true |
| 159 | + done |
| 160 | + |
| 161 | + # Give containerd time to release device references |
| 162 | + sleep 2 |
| 163 | + fi |
| 164 | + |
| 165 | + # Sync filesystem and drop caches to help release any remaining references |
| 166 | + sync |
| 167 | + echo 1 > /proc/sys/vm/drop_caches 2>/dev/null || true |
| 168 | + |
105 | 169 | pool_remove |
106 | 170 | fi |
107 | 171 | pool_create |
|
0 commit comments