[SOLVED] Recovering a deleted partition table of a LUKS encrypted flash drive
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Recovering a deleted partition table of a LUKS encrypted flash drive
I have a flash drive encrypted with dm-crypt and LUKS. A while back I was messing around with it using testdisk and ended up deleting the partition table stored in the MBR.
AFAIK, there were two partitions stored on this USB, the LUKS partition and the encrypted partition. I've tried to relocate the partitions using testdisk, configured to search with various common CHS values, but I haven't found any partitions on the USB. This doesn't make sense to me as the partition headers shouldn't be encrypted right?
When this flash drive was working, whenever I'd plug it in to my Ubuntu machine, a GUI requesting a password to decrypt the drive would open. It doesn't anymore, which leads me to believe that because of the missing table, it's not booting to the flash drive's partition that it should be.
When I run this command,
Code:
if=/dev/sdf | hexdump -C | grep LUKS
I get the following output:
Code:
0001ea00 4c 55 4b 53 ba be 00 01 61 65 73 00 00 00 00 00 |LUKS....aes.....|
Also, cryptsetup doesn't even recognize it as being encrypted...
Code:
sudo cryptsetup luksOpen /dev/sdb test
Code:
Device /dev/sdb is not a valid LUKS device.
I'm completely lost as to how I'm supposed to recover the partition table. Is it even possible? Do you guys have any tools/recommendations on how I should go about it?
Last edited by b92kvmomim; 05-17-2017 at 06:47 PM.
As you were "messing around" with this, the data obviously has no value, so why not just scrub it and start again ?.
Yes, I am serious. If you didn't consider it worthwhile protecting, why should anyone else ?.
In all likelihood there was only one partition on the device. If not, hopefully it was first. Simply allocate a partition over the entire device then try the luksopen.
As you were "messing around" with this, the data obviously has no value, so why not just scrub it and start again ?.
Yes, I am serious. If you didn't consider it worthwhile protecting, why should anyone else ?.
In all likelihood there was only one partition on the device. If not, hopefully it was first. Simply allocate a partition over the entire device then try the luksopen.
I guess I'm an idiot for not backing up an image of the drive before writing to the MBR. Didn't know what I was doing at the time. It's kind of important that I don't scrub the drive because I need to recover some deleted files on it.
Is there an easy way of allocating a partition without wiping the drive? I'll be googling around in the meantime.
The partition table is entirely contained within that sector you printed above. It only defines the extent of the partition - so long as you only define primary partition(s) you will not affect any data on the drive. Use fdisk/[g]parted/whatever.
If you used the entire device initially (rather than a partition) for your luks container your ability to access your data is probably already gone.
0001ea00 4c 55 4b 53 ba be 00 01 61 65 73 00 00 00 00 00 |LUKS....aes.....|
Also, cryptsetup doesn't even recognize it as being encrypted...
Code:
sudo cryptsetup luksOpen /dev/sdb test
Code:
Device /dev/sdb is not a valid LUKS device.
Which device is it, "/dev/sdb" or "/dev/sdf"? Anyway, your LUKS partition starts at sector 245 (0x0001ea00 / 512). Use fdisk to create a partition starting at that sector and extending to the end of the device, and you should then be able to luksOopen partition 1 of that device.
Which device is it, "/dev/sdb" or "/dev/sdf"? Anyway, your LUKS partition starts at sector 245 (0x0001ea00 / 512). Use fdisk to create a partition starting at that sector and extending to the end of the device, and you should then be able to luksOopen partition 1 of that device.
Woops, /dev/sdb. The /dev/sdf was copied pasted from a stackoverflow post.
I will try this out.
However, syg00 says:
Quote:
Originally Posted by syg00
The partition table is entirely contained within that sector you printed above. It only defines the extent of the partition - so long as you only define primary partition(s) you will not affect any data on the drive. Use fdisk/[g]parted/whatever.
If you used the entire device initially (rather than a partition) for your luks container your ability to access your data is probably already gone.
So it seems that in my case I did not use the entire device as my LUKS container then.
Last edited by b92kvmomim; 05-17-2017 at 10:54 PM.
Which device is it, "/dev/sdb" or "/dev/sdf"? Anyway, your LUKS partition starts at sector 245 (0x0001ea00 / 512). Use fdisk to create a partition starting at that sector and extending to the end of the device, and you should then be able to luksOopen partition 1 of that device.
Worked. It now prompts me to enter in the password when the USB plugs in.
However, it outputs this: Error unlocking /dev/sdc1: Command-line `cryptsetup luksOpen "/dev/sdc1" "luks-45fee85d-06c9-46c7-8c7a-39479056f3f7" ' exited with non-zero exit status 5: Device luks-45fee85d-06c9-46c7-8c7a-39479056f3f7 already exists.
I will try mounting via the command line.
EDIT:
Removed mappings and it mounts!
THANK YOU GUYS.
@rknichols, can you explain how this works? I was under the impression that there's supposed to be a LUKS partition separated from the encrypted partition. My other hard drive also encrypted with LUKS, with a screwed up partition table, has a LUKS partition separate from the encrypted one. On boot, GRUB boots into that partition, which prompts for a password, and then, from what I believe, it mounts the encrypted partition. Is this not how the flash drive is structured?
Last edited by b92kvmomim; 05-17-2017 at 11:22 PM.
I was under the impression that there's supposed to be a LUKS partition separated from the encrypted partition. My other hard drive also encrypted with LUKS, with a screwed up partition table, has a LUKS partition separate from the encrypted one. On boot, GRUB boots into that partition, which prompts for a password, and then, from what I believe, it mounts the encrypted partition. Is this not how the flash drive is structured?
I really don't understand what you mean. A LUKS partition is an encrypted partition. The partition has a LUKS header at the beginning, and that is followed by encrypted data (which is usually a filesystem, but could be anything). Yes, you can have an encrypted partition that is not LUKS, but you have to know what you are doing to set that up, and unlocking such a partition requires not only the key but also all the other parameters of the cipher.
You define a luks container on a partition - then you mkfs. Might be confusing.
From the OP questions, I suspect a (unencrypted) /boot was defined prior to the luks container. After all, sector 245 is a bit oddball.
Indeed! That's why testdisk wasn't finding it. Even when you go into the "Options" menu and select "Align partitions: no", it still looks for structures only on megabyte or "cylinder" boundaries. You have to select a partition table type of "None" to get around that, and then of course you can't have it write out the partitioning it discovers. That behavior, plus testdisk's unwavering devotion to CHS geometry, are my biggest complaints about it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.