{"id":368,"date":"2025-09-10T09:51:49","date_gmt":"2025-09-10T01:51:49","guid":{"rendered":"https:\/\/zhaoyanqi.cn\/?p=368"},"modified":"2026-01-14T16:51:17","modified_gmt":"2026-01-14T08:51:17","slug":"%e9%80%9a%e8%bf%87%e5%ae%b9%e5%99%a8upperdir%e7%9a%84snapshots-id%e5%8f%b7%e6%89%be%e5%88%b0%e5%af%b9%e5%ba%94pod","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=368","title":{"rendered":"\u901a\u8fc7\u5bb9\u5668upperdir\u7684snapshots id\u53f7\u627e\u5230\u5bf9\u5e94pod"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u8282\u70b9\u7684\u786c\u76d8\u4e00\u76f4\u5728\u5199\u5165\u3002\u6000\u7591\u662f\u67d0\u4e2a\u5e94\u7528\u7684\u6301\u7eed\u5728\u5199\u5165\u65e5\u5fd7\u6216\u8005\u522b\u7684\u4ec0\u4e48\u3002\u9700\u8981\u5b9a\u4f4d\u5230\u5177\u4f53\u7684pod\u3002\u8fd9\u65f6\u5019\u53ef\u4ee5\u5148\u5728\u8282\u70b9\u4e0a\u901a\u8fc7du\u547d\u4ee4\u627e\u5230\u627e\u5230\u6301\u7eed\u589e\u5927\u7684\u6587\u4ef6\uff0c\u4ece\u800c\u627e\u5230\u5bf9\u5e94\u5bb9\u5668upperdir\u7684snapshots id\u53f7\u3002\u7136\u540e\u901a\u8fc7\u4e0b\u9762\u7684\u811a\u672c\u627e\u5230\u5177\u4f53pod<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u811a\u672c\u5982\u4e0b\uff0c\u65b0\u5efa\u4e00\u4e2afind_pod_by_snapshot.sh\u6587\u4ef6\uff0c\u5e76\u4e14\u7ed9\u4e88\u6267\u884c\u6743\u9650<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env bash\n# \u7528\u6cd5\uff1a\n#   .\/find_pod_by_snapshot.sh 11285\n#   .\/find_pod_by_snapshot.sh \/var\/lib\/containerd\/io.containerd.snapshotter.v1.overlayfs\/snapshots\/11285\/fs\n# \u4ec5\u4f9d\u8d56\uff1abash\u3001grep\u3001sed\u3001kubectl\nset -euo pipefail\n\nROOT=\"${CONTAINERD_SNAP_ROOT:-\/var\/lib\/containerd\/io.containerd.snapshotter.v1.overlayfs\/snapshots}\"\nARG=\"${1:?Usage: $0 &lt;snapshot-id | upperdir-path&gt;}\"\n\n# \u89e3\u6790 upperdir\nif &#91;&#91; \"$ARG\" = \/* ]]; then\n  UP=\"$ARG\"\nelse\n  UP=\"$ROOT\/$ARG\/fs\"\nfi\necho \"Upperdir: $UP\"\n\n# 1) \u627e\u5bb9\u5668ID\uff08\u53ef\u80fd\u591a\u4e2a\uff09\nmapfile -t CIDS &lt; &lt;(\n  grep -R -- \"$UP\" \/proc\/*\/mountinfo 2&gt;\/dev\/null \\\n    | sed -nE 's#.*\/k8s\\.io\/(&#91;0-9a-f]{64})\/rootfs.*#\\1#p' \\\n    | sort -u\n)\nif ((${#CIDS&#91;@]}==0)); then\n  echo \"\u26a0\ufe0f  \u672a\u5728\u672c\u673a\u627e\u5230\u4f7f\u7528\u8be5 upperdir \u7684\u5bb9\u5668\u6302\u8f7d\uff1b\u5bb9\u5668\u53ef\u80fd\u5df2\u9000\u51fa\uff0c\u6216\u8fd9\u4e0d\u662f\u8fd0\u884c\u8282\u70b9\u3002\"\n  exit 4\nfi\nprintf '\u627e\u5230 %d \u4e2a\u5bb9\u5668\uff1a\\n' \"${#CIDS&#91;@]}\"; printf '  - %s\\n' \"${CIDS&#91;@]}\"\n\n# 2) \u9009\u62e9 kubeconfig\uff08\u4f18\u5148\u73af\u5883\u53d8\u91cf\uff0c\u5176\u6b21\u5e38\u89c1\u8def\u5f84\uff09\nKCFG=\"${KUBECONFIG:-}\"\nif &#91;&#91; -z \"${KCFG}\" ]]; then\n  for f in \"$HOME\/.kube\/config\" \"\/root\/.kube\/config\" \"\/etc\/kubernetes\/kubelet.conf\" \"\/var\/lib\/kubelet\/kubeconfig\" \"\/etc\/rancher\/k3s\/k3s.yaml\" \"\/var\/lib\/rancher\/k3s\/agent\/kubelet.kubeconfig\"; do\n    &#91;&#91; -r \"$f\" ]] &amp;&amp; { KCFG=\"$f\"; break; }\n  done\nfi\ncommand -v kubectl &gt;\/dev\/null 2&gt;&amp;1 || { echo \"\u274c \u672a\u627e\u5230 kubectl\"; exit 5; }\n&#91;&#91; -n \"$KCFG\" ]] || { echo \"\u274c \u672a\u627e\u5230\u53ef\u8bfb\u7684 kubeconfig\"; exit 6; }\nexport KUBECONFIG=\"$KCFG\"\necho \"\u4f7f\u7528 kubeconfig: $KUBECONFIG\"\n\n# 3) \u62c9\u4e00\u6b21\u5168\u96c6\u7fa4\u5bb9\u5668ID\u6e05\u5355 \u2192 \u5199\u5165\u4e34\u65f6\u6587\u4ef6\uff08\u907f\u514d subshell\/\u8d4b\u503c\u5751\uff09\nJSONPATH='{range .items&#91;*]}{.metadata.namespace}{\"\\t\"}{.metadata.name}{\"\\t\"}{range .status.initContainerStatuses&#91;*]}{.name}{\"\\t\"}{.containerID}{\"\\n\"}{end}{range .status.containerStatuses&#91;*]}{.name}{\"\\t\"}{.containerID}{\"\\n\"}{end}{range .status.ephemeralContainerStatuses&#91;*]}{.name}{\"\\t\"}{.containerID}{\"\\n\"}{end}{end}'\nTMP=$(mktemp)\ntrap 'rm -f \"$TMP\"' EXIT\nkubectl get pods -A -o jsonpath=\"$JSONPATH\" &gt; \"$TMP\"\n\n# Debug\uff1a\u770b\u770b kubectl \u5b9e\u9645\u8fd4\u56de\u4e86\u591a\u5c11\u884c\nLINES=$(wc -l &lt; \"$TMP\" || echo 0)\necho \"kubectl \u8fd4\u56de $LINES \u884c\u53ef\u5339\u914d\u8bb0\u5f55\"\n\necho\necho \"\u6620\u5c04\u7ed3\u679c\uff1a\"\necho \"NAMESPACE\/POD &#91;CONTAINER]    containerID\"\necho \"----------------------------------------\"\n\n# 4) \u5bf9\u6bcf\u4e2a CID \u76f4\u63a5 grep \u547d\u4e2d\uff08\u5148\u5168\u957f\uff0c\u518d 12 \u4f4d\u524d\u7f00\uff09\nfor CID in \"${CIDS&#91;@]}\"; do\n  if grep -Fq \"$CID\" \"$TMP\"; then\n    grep -F \"$CID\" \"$TMP\" | while IFS=$'\\t' read -r ns pod ctr contid; do\n      &#91;&#91; -z \"${ns:-}\" || -z \"${pod:-}\" || -z \"${ctr:-}\" ]] &amp;&amp; continue\n      printf \"%s\/%s &#91;%s]    %s\\n\" \"$ns\" \"$pod\" \"$ctr\" \"$contid\"\n    done\n  else\n    SHORT=\"${CID:0:12}\"\n    if grep -Fq \"$SHORT\" \"$TMP\"; then\n      grep -F \"$SHORT\" \"$TMP\" | while IFS=$'\\t' read -r ns pod ctr contid; do\n        &#91;&#91; -z \"${ns:-}\" || -z \"${pod:-}\" || -z \"${ctr:-}\" ]] &amp;&amp; continue\n        printf \"%s\/%s &#91;%s]    %s\\n\" \"$ns\" \"$pod\" \"$ctr\" \"$contid\"\n      done\n    else\n      echo \"(\u96c6\u7fa4\u4fa7\u672a\u5339\u914d\u5230)    containerd:\/\/$CID\"\n    fi\n  fi\ndone\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u8282\u70b9\u7684\u786c\u76d8\u4e00\u76f4\u5728\u5199\u5165\u3002\u6000\u7591\u662f\u67d0\u4e2a\u5e94\u7528\u7684\u6301\u7eed\u5728\u5199\u5165\u65e5\u5fd7\u6216\u8005\u522b\u7684\u4ec0\u4e48\u3002\u9700\u8981\u5b9a\u4f4d\u5230\u5177\u4f53\u7684pod\u3002\u8fd9\u65f6\u5019\u53ef\u4ee5\u5148\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=368"}],"version-history":[{"count":3,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/368\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}