0

I am trying to mount a apfs partition in linux.

So far I have been able to compile the apfs-fuse build system, and apfs-fuse seems to be working, if I have a working USB drive with a proper apfs partition as follows

/dev/sda
    /dev/sda1
    /dev/sda2

In this case, I can use sudo ./apfs-fuse /dev/sda2 /mnt/apfs to mount the apfs partition, and it works.

However, lets say I have a image of a drive from a backup, I am having trouble mounting the partition.


Heres what I did so far to see if I can mount a partition in backup with a damaged drive (deleted partition table) using losetup. I am assuming that I am able to find the beginning and end sectors here.

I created a new file containing 2 partitions using

sudo dd if=/dev/zero of=backup.img bs=1M count=100

sudo fdisk backup.img and partitioned backup.img into two partitions, after which sudo fdisk -lu backup.img shows as follows

Device   Boot  Start   End   Sectors   Size Id   Type
backup.img1       2048  104447  102400    50M 83   Linux
backup.img2     104448  204799  100352    49M 83   Linux

The partitions here are ext3 and ext4 partitions as I am just testing if I can mount partitions with a non-existant partion table using losetup

Then I tried this

sudo losetup -r -o 1048576 /dev/loop0 backup.img works, after which I can do sudo mount /dev/loop0 /mnt/test.

However, I am assuing that I have a raw image with no partition table, so I have to manually provide the partition table. Also I dont want to mess with the existing backup as it could damage the disk further.

sudo losetup -o 1048576 --sizelimit 52428288 /dev/loop0 backup.img works.

However, when I try to mount the loop device using

sudo mount /dev/loop0 /mnt/test, it fails saying

mount: /mnt/test: wrong fs type, bad option, bad superblock on /dev/loop0, missing codepage or helper program or other error

Why does losetup not recognize the partition when I provide the sizelimit?

Is there something I am missing here?

  • 1
    Why the -o 40000 in the otherwise very sensible losetup call? Also, does `file sda2.img` agree this is a file system containing image? – Marcus Müller Aug 31 '23 at 07:23
  • thats the offset for the start of the partition of apfs – CarriMegrabyan Aug 31 '23 at 20:51
  • the question is when I use `losetup`, does it just try to replicate a system similar to the `/dev/sda2` when I mount the usb drive, or is it doing something different? `400000` is just approximate – CarriMegrabyan Aug 31 '23 at 20:55
  • "just approximate" will never do, why do you do that?. Loser up does nothing - it just takes a file (and optionally, an offset into that for) and tells the kernel "make a block device backed by this file". So, if that file contains an image of a partition with a file system on it, that can than be mounted. If not, then not. What does your sda2.img contain? – Marcus Müller Aug 31 '23 at 21:49
  • i apologize.. i just put 400000 here, that doesnt mean I am not using exact numbers when running the commands. I will edit the question above – CarriMegrabyan Sep 01 '23 at 05:49
  • 1
    Bit confused, a raw image of a while disk would always contain the partition table – Marcus Müller Sep 01 '23 at 07:48
  • I modified my tests.. would you mind checking the question above? I am assuming the partition table no longer exists and is damaged – CarriMegrabyan Sep 02 '23 at 18:43
  • I’m voting to close this question because doing brain surgery on an APFS disk is inappropriate for a Q&A site. – waltinator Sep 02 '23 at 20:00

1 Answers1

0

There's nothing wrong in theory with the commands you are using to setup the loop device. I use something similar in build scripts to generate images.

My guess is that you accidentally formatted your test image incorrectly, perhaps you made the ext file system larger than it should have been.

Loop devices are just block devices pointing to a section of a file and they know nothing about the partition table. Likewise partitions are nothing more than a section on the disk with a table entry describe where on disk they are.

The easiest way to get block devices for your partitions is to use the partition table which should be there on any disk.

You can actually ask Linux to add loop partitions for you if the image already has a partition table:

loop_device=$(losetup --show -f my image.img)
partx -a $loop_device

This will create a set of devices such as /dev/loop2 /dev/loop2p1 /dev/loop2p2. This avoids you messing around with calculating offsets.

Philip Couling
  • 17,591
  • 5
  • 42
  • 82