Skip to contentSkip to conversation

Unable to open LUKS after altering partition table

dm-crypt Jim 2 participants 3 messages Apr 2, 2013

lore
Share & tools
lore

Jim

author
#0 lore raw

I made a silly mistake and realize that I probably will not recover this data. My goal is an exercise to better understand dm-crypt, LUKS, and the rest of the stack.

I have, or had, a computer running Ubuntu 12.10 on an SSD with full disk encryption as configured by the standard installer. The drive contains an unencrypted /boot partition (sda1) and a LUKS/dm-crypt encrypted partition (sda5) that was a physical volume for a single volume group. That vg holds the lvs for an ext4 root file system and swap space.

The problem started when I attempted to use gparted to repartition a usb flash drive without changing the selected device from /dev/sda (internal hdd) to /dev/sdb (usb flash drive)! I deleted the existing partitions and told gparted to apply my changes. Of course, I chose to ignore all warning prompts and continue. Instant regret. I left gparted open for a while hoping to find some way to undo my change and rewrite the old partition table. No luck.

It would seem that gparted was somewhat successful in asking the kernel to reload the partition table as /sys/block/sda/ contained evidence of the sda5 partition but none of the primary partitions. I was able to grab the starting position and size from:

$ cat /sys/block/sda/sda5/start
501760
$ cat /sys/block/sda/sda5/size
233938944
$ cat /sys/block/sda/sda5/alignment_offset
0

Apt-get installed TestDisk 6.13 from and it found the /boot partition which I wrote to disk as the new partition table. I then used fdisk and added the sda5 partition. Fdisk's default starting position matched the above which I took to be a positive sign.

A little more info from after the mistake but before rebooting:

$ cryptsetup status /dev/sda5
/dev/mapper/sda5_crypt is active and is in use.
  type: LUKS1
  cipher: aes-xts-plain64
  keysize: 512 bits
  device: /dev/sda5
  offset: 4096 sectors
  size: 233934848 sectors
  mode: read/write
$ cryptsetup status cryptswap1
/dev/mapper/cryptswap1 is active and is in use.
  type: PLAIN
  cipher: aes-cbc-essiv:sha256
  keysize: 256 bits
  device: /dev/mapper/cryptswap1
  offset: 0 sectors
  size: 16572416 sectors
  mode: read/write
$ vgdisplay
  --- Volume group ---
  VG Name ubuntu
  System ID
  Format lvm2
  Metadata Areas 1
  Metadata Sequence No 3
  VG Access read/write
  VG Status resizable
  Max LV 0
  Cur LV 2
  Open LV 2
  Max PV 0
  Cur PV 1
  Act PV 1
  VG Size 111.55 GiB
  PE Size 4.00 MiB
  Total PE 28556
  Alloc PE / Size 28556 / 111.55 GiB
  Free PE / Size 0 / 0
  VG UUID bfEpfv-*omitted*
$ lvdisplay
  --- Logical volume ---
  LV Path /dev/ubuntu/root
  LV Name root
  VG Name ubuntu
  LV UUID TYhvhI-*omitted*
  LV Write Access read/write
  LV Creation host, time ubuntu, 2013-01-09...
  LV Status available
  # open 1
  LV Size 103.64 GiB
  Current LE 26533
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 256
  Block device 252:1

--- Logical volume ---
LV Path /dev/ubuntu/swap_1
LV Name swap_1
VG Name ubuntu
LV UUID S0pUq6-*omitted*
LV Write Access read/write
LV Creation host, time ubuntu, 2013-01-09...
LV Status available

  # open 1
  LV Size 7.90 GiB
  Current LE 2023
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 256
  Block device 252:1
$ fdisk -l /dev/sda

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 23441648 sectors Unites = sectors of 1 * 512 = 512 bytes
Sectors size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier:

Device Boot Start End Blocks ID System
/dev/sda1 * 2048 499711 248832 83 Linux

/dev/sda2        499712    23440703   116970496    5   Extended
/dev/sda5        501760    23440703   116969472   83  Linux

I was able to mount /boot which led me to believe I was on track. It seemed that sda5 was positioned correctly in this recreated partition table so I went ahead and rebooted.

The grub2 menu loaded as usual and then I was prompted for the cryptsetup password. Everything seemed normal but it would not accept my password! I tried dozens of times while being careful with caps,num,fn lock keys. As a sanity check, I loaded the computer with a backup disk that is an out of date dd replicatoin of this SSD and my password was accepted there.

Running the computer from a live cd I have examined the disk further.

$ blkid -p /dev/sda5
/dev/sda5: UUID="9c9e0774-*omitted*" VERSION="1" TYPE="crypto_LUKS" USAGE="crypto" PART_ENTRY_SCHEME="dos" PART_ENTRY_TYPE="0x83" PART_ENTRY_NUMBER="5" PART_ENTRY_OFFSET="501760" PART_ENTRY_SIZE="233938944" PART_ENTRY_DISK="8:0"
$ cryptsetup -v isLuks /dev/sda5
Command successful.

That seems good as the starting position and size exactly match the value from the open volume before rebooting. I don't see why deleting and restoring the partition table would affect the volume. Why does luksOpen no longer like my password?

After repeated failure to unlock the volume at boot time I was dropped to an initramfs prompt where I performed a luksDump. If I messed up on repartitioning I would expect luksDump to complain.

(initramfs) cryptsetup -v luksDump /dev/sda5
Version: 1
Cipher name: aes
Cipher mode: xts-plain64
Hash spec: sha1
Payload offset: 4096
MK bits: 512
MK digest: *omitted*
MK salt: *omitted*
MK iterations: *omitted*
UUID: 9c9e0774-*omitted*

Key Slot 0: ENABLED
Iterations: *omitted*
Salt: *omitted*
Key material offset: 8
Af stripes: 4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED
Command successful.

I may have missed something obvious but reading through the FAQ, searching the list and the web hasn't yielded the answer so I hope one of you can help or offer closure :)

Thanks!

#1 lore raw

Hi,

several things:

1. Never, ever, ever do this type of work without full backup.

2. Once you broke it, never, ever, ever write anything to the device. Make a full binary copy (dd, dd_recue, cat from /dev/sdx, ...) and only work on that. Many people only make their LUKS containers irretrivable when trying to repair them. (At least I suspect that. It is hard to tell, but some people have had success in recovery, while people that try it your way typically do nto get their data back.)

3. The cryptsetup package contains a keyslot-checker tool. (I wrote it, but forgot to do an FAQ entry on it, maybe I will find time this week.) You find it in misc/keyslot_checker/ with instructions. It will find overwritten areas in the active keyslots. It requires installation of the cyrrent cryptsetup libraries (1.6.x) but runs itself without installation.

Arno

Quoted 195 lines · Jim

I made a silly mistake and realize that I probably will not recover this data. My goal is an exercise to better understand dm-crypt, LUKS, and the rest of the stack.

I have, or had, a computer running Ubuntu 12.10 on an SSD with full disk encryption as configured by the standard installer. The drive contains an unencrypted /boot partition (sda1) and a LUKS/dm-crypt encrypted partition (sda5) that was a physical volume for a single volume group. That vg holds the lvs for an ext4 root file system and swap space.

The problem started when I attempted to use gparted to repartition a usb flash drive without changing the selected device from /dev/sda (internal hdd) to /dev/sdb (usb flash drive)! I deleted the existing partitions and told gparted to apply my changes. Of course, I chose to ignore all warning prompts and continue. Instant regret. I left gparted open for a while hoping to find some way to undo my change and rewrite the old partition table. No luck.

It would seem that gparted was somewhat successful in asking the kernel to reload the partition table as /sys/block/sda/ contained evidence of the sda5 partition but none of the primary partitions. I was able to grab the starting position and size from:

$ cat /sys/block/sda/sda5/start
501760
$ cat /sys/block/sda/sda5/size
233938944
$ cat /sys/block/sda/sda5/alignment_offset
0

Apt-get installed TestDisk 6.13 from and it found the /boot partition which I wrote to disk as the new partition table. I then used fdisk and added the sda5 partition. Fdisk's default starting position matched the above which I took to be a positive sign.

A little more info from after the mistake but before rebooting:

$ cryptsetup status /dev/sda5
/dev/mapper/sda5_crypt is active and is in use.
  type: LUKS1
  cipher: aes-xts-plain64
  keysize: 512 bits
  device: /dev/sda5
  offset: 4096 sectors
  size: 233934848 sectors
  mode: read/write
$ cryptsetup status cryptswap1
/dev/mapper/cryptswap1 is active and is in use.
  type: PLAIN
  cipher: aes-cbc-essiv:sha256
  keysize: 256 bits
  device: /dev/mapper/cryptswap1
  offset: 0 sectors
  size: 16572416 sectors
  mode: read/write
$ vgdisplay
  --- Volume group ---
  VG Name ubuntu
  System ID
  Format lvm2
  Metadata Areas 1
  Metadata Sequence No 3
  VG Access read/write
  VG Status resizable
  Max LV 0
  Cur LV 2
  Open LV 2
  Max PV 0
  Cur PV 1
  Act PV 1
  VG Size 111.55 GiB
  PE Size 4.00 MiB
  Total PE 28556
  Alloc PE / Size 28556 / 111.55 GiB
  Free PE / Size 0 / 0
  VG UUID bfEpfv-*omitted*
$ lvdisplay
  --- Logical volume ---
  LV Path /dev/ubuntu/root
  LV Name root
  VG Name ubuntu
  LV UUID TYhvhI-*omitted*
  LV Write Access read/write
  LV Creation host, time ubuntu, 2013-01-09...
  LV Status available
  # open 1
  LV Size 103.64 GiB
  Current LE 26533
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 256
  Block device 252:1

--- Logical volume ---
LV Path /dev/ubuntu/swap_1
LV Name swap_1
VG Name ubuntu
LV UUID S0pUq6-*omitted*
LV Write Access read/write
LV Creation host, time ubuntu, 2013-01-09...
LV Status available

  # open 1
  LV Size 7.90 GiB
  Current LE 2023
  Segments 1
  Allocation inherit
  Read ahead sectors auto
  - currently set to 256
  Block device 252:1
$ fdisk -l /dev/sda

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 23441648 sectors Unites = sectors of 1 * 512 = 512 bytes
Sectors size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier:

Device Boot Start End Blocks ID System
/dev/sda1 * 2048 499711 248832 83 Linux

/dev/sda2        499712    23440703   116970496    5   Extended
/dev/sda5        501760    23440703   116969472   83  Linux

I was able to mount /boot which led me to believe I was on track. It seemed that sda5 was positioned correctly in this recreated partition table so I went ahead and rebooted.

The grub2 menu loaded as usual and then I was prompted for the cryptsetup password. Everything seemed normal but it would not accept my password! I tried dozens of times while being careful with caps,num,fn lock keys. As a sanity check, I loaded the computer with a backup disk that is an out of date dd replicatoin of this SSD and my password was accepted there.

Running the computer from a live cd I have examined the disk further.

$ blkid -p /dev/sda5
/dev/sda5: UUID="9c9e0774-*omitted*" VERSION="1" TYPE="crypto_LUKS"
USAGE="crypto" PART_ENTRY_SCHEME="dos" PART_ENTRY_TYPE="0x83"
PART_ENTRY_NUMBER="5" PART_ENTRY_OFFSET="501760"
PART_ENTRY_SIZE="233938944" PART_ENTRY_DISK="8:0"
$ cryptsetup -v isLuks /dev/sda5
Command successful.

That seems good as the starting position and size exactly match the value from the open volume before rebooting. I don't see why deleting and restoring the partition table would affect the volume. Why does luksOpen no longer like my password?

After repeated failure to unlock the volume at boot time I was dropped to an initramfs prompt where I performed a luksDump. If I messed up on repartitioning I would expect luksDump to complain.

(initramfs) cryptsetup -v luksDump /dev/sda5
Version: 1
Cipher name: aes
Cipher mode: xts-plain64
Hash spec: sha1
Payload offset: 4096
MK bits: 512
MK digest: *omitted*
MK salt: *omitted*
MK iterations: *omitted*
UUID: 9c9e0774-*omitted*

Key Slot 0: ENABLED
Iterations: *omitted*
Salt: *omitted*
Key material offset: 8
Af stripes: 4000
Key Slot 1: DISABLED
Key Slot 2: DISABLED
Key Slot 3: DISABLED
Key Slot 4: DISABLED
Key Slot 5: DISABLED
Key Slot 6: DISABLED
Key Slot 7: DISABLED
Command successful.

I may have missed something obvious but reading through the FAQ, searching the list and the web hasn't yielded the answer so I hope one of you can help or offer closure :)

Thanks!

_______________________________________________
dm-crypt mailing list
[email protected]
http://www.saout.de/mailman/listinfo/dm-crypt

Signature · 6 lines
Arno Wagner,     Dr. sc. techn., Dipl. Inform.,    Email: [email protected]
GnuPG: ID: CB5D9718  FP: 12D6 C03B 1B30 33BB 13CF  B774 E35C 5FA1 CB5D 9718
----
One of the painful things about our time is that those who feel certainty
are stupid, and those with any imagination and understanding are filled
with doubt and indecision. -- Bertrand Russell
author
#2 lore raw
Quoted 8 lines · Arno Wagner

3. The cryptsetup package contains a keyslot-checker tool. (I wrote it, but forgot to do an FAQ entry on it, maybe I will find time this week.) You find it in misc/keyslot_checker/ with instructions. It will find overwritten areas in the active keyslots. It requires installation of the cyrrent cryptsetup libraries (1.6.x) but runs itself without installation.

Hi Arno,

Thanks for the useful tool!

./chk_luks_keyslots -v /dev/sda5

parameters (commandline and LUKS header):
sector size: 512
threshold: 0.900000

- processing keyslot 0: start: 0x001000 end: 0x03f800
low entropy at: 0x010000 entropy: 0.599098
Binary dump:
0x010000 30 30 30 30 30 30 30 00 75 64 65 76 73 65 74 74
0000000.udevsett
0x010010 6C 65 00 75 64 65 76 73 65 74 74 6C 65 20 2D 2D
le.udevsettle --
0x010020 74 69 6D 65 6F 75 74 3D 00 75 64 65 76 61 64 6D
timeout=.udevadm
0x010030 20 73 65 74 74 6C 65 20 2D 2D 74 69 6D 65 6F 75 settle
--timeou
0x010040 74 3D 00 4C 69 62 70 61 72 74 65 64 20 57 61 72
t=.Libparted War
0x010050 6E 69 6E 67 00 4C 69 62 70 61 72 74 65 64 20 49
ning.Libparted I
0x010060 6E 66 6F 72 6D 61 74 69 6F 6E 00 4C 69 62 70 61
nformation.Libpa
0x010070 72 74 65 64 20 45 72 72 6F 72 00 4C 69 62 70 61 rted
Error.Libpa
0x010080 72 74 65 64 20 42 75 67 20 46 6F 75 6E 64 21 00 rted Bug
Found!.
0x010090 46 69 78 00 59 65 73 00 4F 6B 00 52 65 74 72 79
Fix.Yes.Ok.Retry
0x0100a0 00 4E 6F 00 43 61 6E 63 65 6C 00 49 67 6E 6F 72
.No.Cancel.Ignor
0x0100b0 65 00 25 31 20 6F 66 20 25 32 20 72 65 61 64 20 e.%1 of
%2 read
0x0100c0 28 25 33 20 72 65 6D 61 69 6E 69 6E 67 29 00 25 (%3
remaining).%
0x0100d0 31 20 6F 66 20 25 32 20 72 65 61 64 00 25 31 20 1 of %2
read.%1
0x0100e0 6F 66 20 25 32 20 63 6F 70 69 65 64 00 75 73 69 of %2
copied.usi
0x0100f0 6E 67 20 6C 69 62 70 61 72 74 65 64 00 64 65 6C ng
libparted.del
0x010100 65 74 65 20 70 61 72 74 69 74 69 6F 6E 00 73 65 ete
partition.se
0x010110 74 20 70 61 72 74 69 74 69 6F 6E 20 74 79 70 65 t
partition type
0x010120 20 6F 6E 20 25 31 00 6C 69 6E 75 78 2D 73 77 61 on
%1.linux-swa
0x010130 70 00 6C 69 6E 75 78 2D 73 77 61 70 28 76 31 29
p.linux-swap(v1)
0x010140 00 6C 69 6E 75 78 2D 73 77 61 70 28 6E 65 77 29
.linux-swap(new)
0x010150 00 6E 65 77 20 70 61 72 74 69 74 69 6F 6E 20 74 .new
partition t
0x010160 79 70 65 3A 20 25 31 00 30 78 00 45 72 72 6F 72 ype:
%1.0x.Error
0x010170 20 74 72 79 69 6E 67 20 74 6F 20 6F 70 65 6E 20 trying
to open
0x010180 25 31 00 72 65 71 75 65 73 74 65 64 20 73 69 7A
%1.requested siz
0x010190 65 3A 20 25 31 20 28 25 32 29 00 72 65 71 75 65 e: %1
(%2).reque
0x0101a0 73 74 65 64 20 65 6E 64 3A 20 25 31 00 72 65 71 sted end:
%1.req
0x0101b0 75 65 73 74 65 64 20 73 74 61 72 74 3A 20 25 31 uested
start: %1
0x0101c0 00 6E 65 77 20 73 69 7A 65 3A 20 25 31 20 28 25 .new
size: %1 (%
0x0101d0 32 29 00 6E 65 77 20 65 6E 64 3A 20 25 31 00 6E 2).new
end: %1.n
0x0101e0 65 77 20 73 74 61 72 74 3A 20 25 31 00 63 61 6C ew start:
%1.cal
0x0101f0 69 62 72 61 74 65 20 25 31 00 70 61 74 68 3A 20 ibrate
%1.path:

- processing keyslot 1:  keyslot not in use
- processing keyslot 2:  keyslot not in use
- processing keyslot 3:  keyslot not in use
- processing keyslot 4:  keyslot not in use
- processing keyslot 5:  keyslot not in use
- processing keyslot 6:  keyslot not in use
- processing keyslot 7:  keyslot not in use

Looks like the keyslot is hosed unless there's some way that my partitioning is incorrect. However, since the tools recognize the LUKS header I don't think that is the case. Not sure how this happened since I only wrote to the partition table. Any suggestions?

Thank you for your time!