Sign up Log in
n/oracle-cloud · 42m ago · 36 views
u/mengnianxiaoyaoSilver

Oracle Cloud Ubuntu IPv6 fix

IPv6 on Oracle Cloud Ubuntu instances is problematic—for example, it works right after creation, but after a reboot it just disappears and can't be used. The IPv6 repair provided by the kejilion script says it completed, but in practice it doesn't work. Based on AI analysis and my own testing, the result is: OCI Ubuntu does not have global IPv6 by default: the VCN router RA only advertises the subnet /64, the autonomous flag A=0, and there is no DHCPv6. Neither SLAAC nor DHCPv6 is available—you can only configure it statically on the VNIC.

Here's the fix:

  • Get the address from IMDS at http://169.254.169.254/opc/v2/vnics/ with the request header "Authorization: Bearer Oracle", using ipv6Addresses[0], ipv6SubnetCidrBlock, and macAddr.
  • Don't modify /etc/netplan/50-cloud-init.yaml (cloud-init will rewrite it). Instead create /etc/netplan/60-ipv6-<iface>.yaml: addresses: ["<address>/64"] + accept-ra: true, then netplan generate && netplan apply. The default route is provided by RA.
#!/bin/bash
# ============================================================
# OCI IPv6 初始化 / 修复脚本 v3(Ubuntu + netplan,无 python 依赖)
#
# 只适用 OCI 的 Ubuntu:需要 IMDS(169.254.169.254)拿地址、netplan 写配置。
# 地址数据全部走 IMDS 的 vnic 子资源(纯文本),不再解析整段 JSON,
# 因此不需要 python3 / jq,只用到 curl(没有 curl 时退回 wget)。
#
# 会显式区分失败原因:
#   - 不是 OCI / IMDS 被拦截        -> 退出码 1
#   - 没有 netplan(Debian 等)     -> 退出码 1
#   - VNIC 没分到 IPv6              -> 退出码 2(先去控制台分配)
#   - 地址写在别的 netplan 文件但没生效 -> 退出码 3
#   - 写完 apply 后地址仍未生效     -> 退出码 3
#
# “已配置”看实时地址(ip -6 addr),不看文件文本,所以被 cloud-init 覆盖、
# 文件在但没 apply 的机器也能重新修好。
#
# 支持:一块网卡多个 IPv6、多块 VNIC、--dry-run 预览、--force 强制重写。
# 用法:sudo bash oci-ipv6-init.sh [--dry-run|--force]
# 日志:/var/log/oci-ipv6-init.log
# ============================================================
set -uo pipefail

DRY=0
FORCE=0
for a in "$@"; do
  case "$a" in
    --dry-run) DRY=1 ;;
    --force) FORCE=1 ;;
    -h|--help) sed -n '2,24p' "$0"; exit 0 ;;
    *) echo "未知参数: $a" >&2; exit 1 ;;
  esac
done

if [ "$(id -u)" -ne 0 ]; then
  exec sudo -E bash "$0" "$@"
fi

exec > >(tee -a /var/log/oci-ipv6-init.log) 2>&1
log() { echo "[oci-ipv6] $*"; }
echo "=== $(date -Is) oci-ipv6-init start (dry-run=$DRY force=$FORCE) ==="

IMDS="http://169.254.169.254/opc/v2"
AUTH="Authorization: Bearer Oracle"

# ---------- 1. 环境判定 ----------
if ! command -v netplan >/dev/null 2>&1; then
  log "错误: 没有 netplan(Debian/Alpine/非 Ubuntu 系)。请用系统的网络配置方式手动加静态 IPv6。"
  exit 1
fi

if command -v curl >/dev/null 2>&1; then
  get() { curl -sf -m 5 -H "$AUTH" "$IMDS/$1"; }
elif command -v wget >/dev/null 2>&1; then
  get() { wget -q -T 5 -O - --header="$AUTH" "$IMDS/$1"; }
else
  log "错误: 既没有 curl 也没有 wget,无法读取 IMDS。"
  exit 1
fi

# ---------- 2. 读 VNIC 上的 IPv6(纯文本子资源,不解析 JSON) ----------
region=""
for _ in 1 2 3 4 5 6 7 8 9 10; do
  region=$(get "instance/region") && [ -n "$region" ] && break
  sleep 2
done
if [ -z "$region" ]; then
  log "错误: 169.254.169.254 不是 OCI 元数据服务(非 OCI 机器、或到 IMDS 的访问被拦截/走了代理)。本脚本只适用于 OCI。"
  exit 1
fi
log "OCI 实例,区域 $region"

plan=""
n=0
while [ "$n" -lt 16 ]; do
  mac=$(get "vnics/$n/macAddr") || break
  mac=$(printf '%s' "$mac" | tr -d '[:space:]' | tr 'A-F' 'a-f')
  case "$mac" in
    [0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]:[0-9a-f][0-9a-f]) ;;
    *) break ;;
  esac

  plen=64
  cidr=$(get "vnics/$n/ipv6SubnetCidrBlock" 2>/dev/null || true)
  case "$cidr" in
    */*) plen=${cidr##*/} ;;
  esac
  case "$plen" in
    ''|*[!0-9]*) plen=64 ;;
  esac

  addrs=$(get "vnics/$n/ipv6Addresses/" 2>/dev/null | grep -oE '"[^"]+"' | tr -d '"' | tr '\n' ' ')
  if [ -n "$addrs" ]; then
    plan="${plan}${mac} ${plen} ${addrs}
"
  fi
  n=$((n + 1))
done

if [ -z "$plan" ]; then
  log "错误: 这台机器的 VNIC 上没有 IPv6 地址。先在 OCI 控制台给 VNIC 分配 IPv6,再运行本脚本。"
  exit 2
fi
log "IMDS 中的 IPv6 分配:"
printf '%s' "$plan" | sed 's/^/    /'

# ---------- 3. 按网卡写入 ----------
changed=0
fail=0
while read -r mac plen addrs; do
  [ -z "${mac:-}" ] && continue

  iface=$(ip -o link show | awk -v mac="$mac" 'index(tolower($0), "link/ether " mac) { sub(/:$/, "", $2); print $2; exit }')
  if [ -z "$iface" ]; then
    log "错误: 找不到 MAC $mac 对应的网卡(VNIC 未挂上/热插拔中)。"
    fail=1
    continue
  fi
  log "匹配到网卡 $iface(MAC $mac),IPv6 前缀 /$plen"

  live=$(ip -6 addr show dev "$iface" scope global 2>/dev/null | awk '{print $2}')
  missing=""
  for a in $addrs; do
    case "$a" in *:*) ;; *) continue ;; esac
    echo "$live" | grep -qxF "${a}/${plen}" || missing="$missing $a"
  done
  if [ -z "$missing" ] && [ "$FORCE" -eq 0 ]; then
    log "$iface 上 IPv6 已生效,无需处理"
    continue
  fi
  [ -n "$missing" ] || missing="$addrs"
  log "待配置:$missing"

  for a in $missing; do
    other=$(grep -lsF "$a" /etc/netplan/*.yaml 2>/dev/null | grep -v "60-ipv6-${iface}.yaml" | tr '\n' ' ')
    [ -z "$other" ] && continue
    if [ "$FORCE" -eq 0 ]; then
      log "错误: $a 已写在 $other 里但没有生效。先 netplan generate && netplan apply,或加 --force 让本脚本接管。"
      fail=1
      missing=""
      break
    fi
    log "警告: $a 也出现在 $other,--force 继续接管(可能与该文件重复定义)"
  done
  [ -z "$missing" ] && continue

  file="/etc/netplan/60-ipv6-${iface}.yaml"
  content="network:
  version: 2
  ethernets:
    ${iface}:
      accept-ra: true
      addresses:"
  for a in $addrs; do
    case "$a" in *:*) ;; *) continue ;; esac
    content="${content}
        - \"${a}/${plen}\""
  done

  if [ "$DRY" -eq 1 ]; then
    log "--dry-run 将写入 $file:"
    printf '%s\n' "$content" | sed 's/^/    /'
    changed=1
    continue
  fi

  printf '%s\n' "$content" >"$file"
  chmod 600 "$file"
  log "已写入 $file"
  changed=1
done <<<"$plan"

# ---------- 4. 应用与校验 ----------
if [ "$changed" -eq 1 ] && [ "$DRY" -eq 0 ]; then
  if ! netplan generate; then
    log "错误: netplan generate 失败,检查上面的报错。"
    exit 3
  fi
  if ! netplan apply; then
    log "错误: netplan apply 失败,检查上面的报错。"
    exit 3
  fi
  sleep 2
fi

if [ "$DRY" -eq 0 ]; then
  log "--- 校验 ---"
  bad=0
  while read -r mac plen addrs; do
    [ -z "${mac:-}" ] && continue
    iface=$(ip -o link show | awk -v mac="$mac" 'index(tolower($0), "link/ether " mac) { sub(/:$/, "", $2); print $2; exit }')
    [ -z "$iface" ] && continue
    live=$(ip -6 addr show dev "$iface" scope global 2>/dev/null | awk '{print $2}')
    for a in $addrs; do
      case "$a" in *:*) ;; *) continue ;; esac
      if echo "$live" | grep -qxF "${a}/${plen}"; then
        log "OK   $iface $a/$plen"
      else
        log "FAIL $iface $a/$plen 未生效"
        bad=1
      fi
    done
  done <<<"$plan"
  if [ "$bad" -eq 1 ] || [ "$fail" -eq 1 ]; then
    log "存在失败项。常见原因:VNIC 未挂载、地址与其它 netplan 文件冲突、cloud-init 还在跑。"
    exit 3
  fi
fi

log "--- 当前全局 IPv6 ---"
ip -6 addr show scope global
log "--- 默认路由 ---"
ip -6 route show default
echo "=== $(date -Is) oci-ipv6-init done ==="

Translated by AI from 简体中文

1
1
Log into join the discussion.

1 replies

Sort order
  1. vmvm Silver 8m ago Edited

    Bookmarking this for later, but damn, I still can't get an Oracle account approved 😭

    Translated by AI from 简体中文

    0