0

Last night, I messed up my LineageOS 16 install on my Galaxy S5 and had to reinstall it, starting essentially from scratch. I wanted to see if there was any way I could decrypt the 128 GB microSD card I used as adopted storage and found this tutorial. I got the key from a backup made using TWRP and tried it on Linux Mint 20 MATE (booting from USB), but it didn't work. The following command gave me an error:

dmsetup create crypt1 --table "0 `blockdev --getsize /media/mint/4B2E-1F13` crypt aes-cbc-essiv:sha256 [16-BYTE HEX KEY] 0 /media/mint/4B2E-1F13 0"

blockdev: ioctl error on BLKGETSIZE: Inappropriate ioctl for device
/dev/mapper/control: open failed: Permission denied
Failure to communicate with kernel device-mapper driver.
Incompatible libdevmapper 1.02.167 (2019-11-30) and kernel driver (unknown version).

Doing it with sudo gave me this error:

blockdev: ioctl error on BLKGETSIZE: Inappropriate ioctl for device
Invalid format on line 1 of table.
Command failed.

I couldn't even mount the SD card with Termux when after putting it back into my phone. Is my data unrecoverable? Do I have the wrong key? Thanks.

  • /media/mint/4B2E-1F13 must be a directory. You need to provide the block device path to dmsetup. It must be something like /dev/sdb or /dev/sdc. Use df to see available block devices. Or see kernel log using dmesg when connecting SD card to PC. // Btw your question is about Linux, not Android. – Irfan Latif Jul 22 at 0:51
  • Duplicate of stackoverflow.com/q/16701326/9165920 – Irfan Latif Jul 22 at 0:53
  • @IrfanLatif I changed /media/mint/4B2E-1F13 to /dev/mmcblk0p1 (my SD card according to df -h) and the error messages were the same except the BLKGETSIZE lines were replaced with blockdev: cannot open /dev/mmcblk0p1: Permission denied. The same thing happens when inputting /dev/mmcblk0 instead. – Pete Jul 22 at 1:18
  • 1
    no need to nest blockdev in backticks just type value of partition size directly and use proper path to block device – alecxs Jul 22 at 3:01
  • @Pete block devices cannot be read without root. Run from a root shell or use sudo with dmsetup as well as blockdev (both are separate commands). Or write partition size directly as alecxs said to avoid using blockdev in subshell. Provide encrypted partition (mmcblk0p1), not whole SD card (mmcblk0). – Irfan Latif Jul 22 at 7:24
1

Good news: trying the following command successfully decrypted my SD card without any errors:

sudo dmsetup create crypt1 --table "0 `sudo blockdev --getsize /dev/mmcblk0p2` crypt aes-cbc-essiv:sha256 [16-BYTE HEX KEY] 0 /dev/mmcblk0p2 0"

A couple of minor issues:

  • sudo mount -t /dev/mmcblk0p2/crypt1 /media/mint/sd returned the error mount: /media/mint/sd: can't find in /etc/fstab. I can view the content of the SD card in the file browser running as admin, however.
  • Copying the files over to my laptop's Windows 10 SSD, some files and directories didn't get copied due to the presence of invalid characters in their names (mainly asterisks and colons in file names and a period in a folder name). Not sure how to deal with these...

Thanks for all your help.

| improve this answer | |
  • 1
    -t expects a filesystem type. Learning by trials is a good thing but having a brief look at manuals and existing web resources saves a lot of hassle: man7.org/linux/man-pages/man8/mount.8.html – Irfan Latif Jul 22 at 12:28
  • Oh yeah, I forgot about the ext4 part in poqdavid's answer in the question I linked lol. Thanks for the catch. I'm still not sure how to deal with files and directories with invalid filenames for Windows. It seems like only ext file systems can support those filenames, and Windows can only read that with 3rd-party programs. The other filesystems that Windows can use (exFAT and FAT32) don't support those filenames, so I don't think I can copy those files to flash drives formatted as either of those. – Pete Jul 22 at 13:04
  • No way to force filesystems change their behavior. Such limitations are deep rooted in their design. For backup/restore purpose archive such files using tar, zip etc. Or batch rename such files if possible using sed, mv etc. – Irfan Latif Jul 22 at 17:06
  • you can use tar --transform to convert file names during extraction to fat32 file system based on whitelist charset – alecxs Jul 23 at 17:17
  • transform="--transform='s/[^][A-Za-z0-9 !#$%&'$'\047''()+,.\/;=@'$'\136''_\`{}~'$'\177''-]/_/g'"; tar -c /media/mint/sd | tar $transform -vxC /media/mint/targetdrive – alecxs Jul 23 at 17:38

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