LoginSignup
0
0

【2024年02月版】Linuxで暗号化ディスクの復旧【初心者向け】

Posted at

はじめに

LUKS で暗号化を行っているディスクのヘッダが破損した時、バックアップから復旧する手順のメモ

LUKSで暗号化ディスクの作成については、以下の記事を参照
【2024年02月版】Linuxでディスク暗号化・LUKS・cryptsetup【初心者向け】

環境

  • macOS上のPrallelsのUbuntu22.04
$ uname -a
Linux **** 5.15.0-94-generic #104-Ubuntu SMP Tue Jan 9 15:26:57 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux
$ cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"
  • ディスク構成は、HDD2台で、1台はOS、もう1台が今回暗号化するディスク(/dev/sdb)
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
  :
  :
sda           8:0    0    64G  0 disk 
├─sda1        8:1    0     1G  0 part /boot/efi
├─sda2        8:2    0     2G  0 part /boot
└─sda3        8:3    0  60.9G  0 part 
  └─vg0-lv0 253:0    0  60.9G  0 lvm  /
sdb           8:16   0     2G  0 disk 
  • マウント先は、/mnt/crypt とする

手順

マウントポイントの作成

$ sudo mkdir -p /mnt/crypt

/dev/sdb の暗号化

$ sudo cryptsetup luksFormat /dev/sdb

WARNING!
========
This will overwrite data on /dev/sdb irrevocably.

Are you sure? (Type 'yes' in capital letters): YES
Enter passphrase for /dev/sdb: 
Verify passphrase: 
$ sudo cryptsetup open /dev/sdb cryptdisk
Enter passphrase for /dev/sdb: 

ext4 でフォーマットしてマウント

$ sudo mkfs.ext4 /dev/mapper/cryptdisk
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 520192 4k blocks and 130048 inodes
Filesystem UUID: b84e1f4c-ebb8-4afd-bc11-6de8f5689b87
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

$ sudo mount /dev/mapper/cryptdisk /mnt/crypt

テストファイルを作成

$ sudo dd if=/dev/urandom of=/mnt/crypt/testfile.dat bs=1M count=1024 status=progress
975175680 bytes (975 MB, 930 MiB) copied, 2 s, 487 MB/s
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 2.20231 s, 488 MB/s

テストファイルのハッシュを取得

$ sudo md5sum /mnt/crypt/testfile.dat
6398561e46a1f4dc7fdeda4bef6a3feb  /mnt/crypt/testfile.dat

ヘッダのバックアップを取得

$ sudo cryptsetup luksHeaderBackup /dev/sdb --header-backup-file ~/header_backup

アンマウントして、マッピングの解除

$ sudo umount /mnt/crypt
$ sudo cryptsetup close cryptdisk

/dev/sdb のヘッダを破損させる

$ sudo dd if=/dev/urandom of=/dev/sdb bs=1M count=1
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00944773 s, 111 MB/s

ヘッダの破損で、マッピングできないことを確認

$ sudo cryptsetup open /dev/sdb cryptdisk
Device /dev/sdb is not a valid LUKS device.

バックアップのヘッダから修復

$ sudo cryptsetup open /dev/sdb cryptdisk
Device /dev/sdb is not a valid LUKS device.
$ sudo cryptsetup luksHeaderRestore /dev/sdb --header-backup-file ~/header_backup

WARNING!
========
Device /dev/sdb does not contain LUKS2 header. Replacing header can destroy data on that device.

Are you sure? (Type 'yes' in capital letters): YES

マッピングできるようになるので、マッピングしてマウント

$ sudo cryptsetup open /dev/sdb cryptdisk
$ sudo mount /dev/mapper/cryptdisk /mnt/crypt

テストファイルのハッシュを取得して確認

  • md56398561e46a1f4dc7fdeda4bef6a3feb かどうか確認する
$ sudo md5sum /mnt/crypt/testfile.dat
6398561e46a1f4dc7fdeda4bef6a3feb  /mnt/crypt/testfile.dat

さいごに

かんたんでしたね

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0