3

For the tl;dr, skip to the bottom.

I have a dual-boot setup with Mac OS X at the beginning of my drive and three GNU/Linux partitions at the end of the drive. I don't have the relevant machine on hand to give an fdisk dump, but here's a rough overview of the original disk layout:

/dev/sda1: EFI System Partition
/dev/sda2: Macintosh HD (main OS X partition)
/dev/sda3: Recovery HD (part of OS X's recovery system)
/dev/sda4: /
/dev/sda5: /home inside a LUKS container
/dev/sda6: swapspace inside a LUKS container

I'm reinstalling GNU/Linux on this system and as a part of that, I want to make / encrypted. However, that requires a separate /boot. In order to create that partition, I resized Macintosh HD and moved Recovery HD back to the end of Macintosh HD. I also took the opportunity to make my / larger. So the end result is a disk that looks like this:

/dev/sda1: EFI System Partition
/dev/sda2: Macintosh HD (main OS X partition) (smaller than before)
/dev/sda3: Recovery HD (part of OS X's recovery system)
/dev/sda4: /boot
/dev/sda5: / (larger than before, and now inside a LUKS container)
/dev/sda6: /home inside a LUKS container
/dev/sda7: swapspace inside a LUKS container

tl;dr: Here's the problem: due to the order in which I configured things, I now have a cryptsetup-created LUKS container in the middle of /dev/sda5. Because it's in the middle, nothing can find it to unlock/mount/recover it. How can I move my cryptcontainer to the beginning of the partition? Probably this involves two distinct steps (actually finding the thing on disk, then using that information to move it), but I'm not sure. Googling's turned up nothing.

3

LUKS has a distinct header, you could find possible offsets with grep:

# grep -a -b --only-matching 'LUKS' /dev/sda5
1832480940:LUKS
1959072314:LUKS
2019974297:LUKS

For each possible offset you find, you could create a loop device and see if it's a valid LUKS device or not:

# losetup --find --show --offset 1832480940 --read-only /dev/sda5
/dev/loop3
# cryptsetup luksOpen /dev/loop3 luksloop3
...mount -o ro, ls, backup, umount...
# cryptsetup luksClose luksloop3
# losetup -d /dev/loop3

Once you have found the correct offset, you can move it to the beginning of the partition using dd. This is dangerous, if you get the offset wrong or cancel the operation, the data is lost. So you should make a backup first in any case.

# dd if=/dev/sda5 of=/dev/sda5 bs=1M iflag=skip_bytes skip=1832480940

See if it worked:

# cryptsetup luksOpen /dev/sda5 luks

Restore from backup otherwise.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.