Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Windows 誤格式化 Linux SWAP 分區?一條命令恢復

編輯:Linux 教學

現象

雙系統 (Windows + Linux) 環境下,Windows 磁盤管理誤將 Linux SWAP 分區 (如 /dev/sda7) 格式化為 NTFS/FAT32,導致 Linux 啟動報錯 FAILED to activate swap、進入緊急模式或休眠失效。

原理

SWAP 分區本質是文件系統簽名缺失的原始塊設備。Linux 內核通過 mkswap 寫入 SWAP 簽名 (魔數 SWAPSPACE2/SWAP-SPACE、UUID、版本、頁面大小),swapon 掛載啟用。Windows 格式化僅覆蓋了前幾個扇區的簽名,數據區無損 (原本也無數據),重寫簽名即可恢復。

恢復步驟 (Live USB / 急救模式 / 正常啟動後均可)

# 1. 確認 SWAP 分區設備路徑
lsblk -f | grep swap
# 或
blkid | grep swap
# 輸出示例:/dev/sda7: UUID="xxxx" TYPE="swap" PARTLABEL="swap"

# 2. 重寫 SWAP 簽名 (生成新 UUID)
mkswap /dev/sda7
# 輸出:Setting up swapspace version 1, size = 8 GiB (8589930496 bytes)
#       no label, UUID=aaaa-bbbb-cccc-dddd

# 3. 啟用 SWAP
swapon /dev/sda7

# 4. 驗證
swapon --show
free -h

# 5. 關鍵:更新 /etc/fstab 中的 UUID
# 獲取新 UUID
blkid /dev/sda7 | grep -oP 'UUID="\K[^"]+'
# 編輯 /etc/fstab,將舊 UUID 替換為新 UUID
# UUID=aaaa-bbbb-cccc-dddd  none  swap  sw  0  0

# 6. 更新 initramfs (含 resume=UUID 參數的發行版)
update-initramfs -u      # Debian/Ubuntu
dracut --force           # Fedora/RHEL
mkinitcpio -P            # Arch
# 如內核命令行含 resume=UUID=xxxx,需同步更新 /etc/default/grub → GRUB_CMDLINE_LINUX → update-grub

進階:保留原 UUID (避免改 fstab/grub/initramfs)

mkswap -U <原UUID> /dev/sda7
# 從備份/舊 fstab/blkid 歷史記錄獲取原 UUID

預防措施

  • Windows 磁盤管理 → 右鍵 SWAP 分區 → 「更改驅動器號和路徑」 → 移除駕駛器號 (隱藏)。
  • 使用 diskpart 設置分區屬性 GPT_ATTRIBUTE_PLATFORM_REQUIRED (0x0000000000000001) 防止誤操作。
  • Linux 安裝時將 SWAP 放在 LVM 邏輯卷或文件 (/swapfile),避免裸分區暴露給 Windows。

總結

誤格式化 SWAP 分區 = 簽名丟失。mkswap /dev/sdX && swapon /dev/sdX 30 秒恢復,核心後續動作是同步更新 /etc/fstab、GRUB resume 參數、initramfs 中的 UUID。雙系統用戶請務必在 Windows 隱藏非 NTFS 分區駕駛器號。

Copyright © Windows教程網 All Rights Reserved