Skip to content

Instantly share code, notes, and snippets.

@svet-b
Created May 10, 2021 19:33
Show Gist options
  • Save svet-b/6453f9eee38791cfb42f779343be82c6 to your computer and use it in GitHub Desktop.
Save svet-b/6453f9eee38791cfb42f779343be82c6 to your computer and use it in GitHub Desktop.
Mount and edit SD card image with ext4 partitions on Mac using Docker

Go to the directory with the image you want to work with. Run a Docker container with that directory mounted:

docker run -it --privileged -v /dev:/dev -v (pwd):/tmp --workdir /tmp ubuntu bash

(replace (pwd) with "$PWD" if using bash instead of fish)

See this thread for an explanation of why --privileged and -v /dev:/dev are required. The need for the latter can be avoided with this alrternative workaround.

In the container, assuming that $IMAGE_FILE is the name of your image file:

losetup -Pf --show $IMAGE_FILE

will output the name of the loop device with which the image is associated, e.g. /dev/loop0.

ls /dev/loop0*

should show all the partitions contained in the image file.

You can mount these and access the files with e.g.

mkdir p1
mount /dev/loop0p1 p1

Note that the partition contents will only be accessible inside the Docker container, and not on the host (i.e. across Docker's volume map).

@YSaxon
Copy link

YSaxon commented Feb 5, 2024

Thanks!

I would note that you can very easily copy everything to the host, by running this from the docker

mkdir image_files
cp -r p1/* image_files/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment