1. Introduction

DMG (Apple Disk Image) files support compression usually have the .dmg extension and are primarily associated with macOS systems. Still, we might need to access, utilize, or manipulate .dmg files within a Linux environment.

In this tutorial, we’ll explore various methods for unlocking the contents of .dmg files in a Linux system.

2. Using dmg2img

dmg2img is a command-line tool in Linux to convert DMG files into the relatively more accessible IMG format. Hence, it enables access and mounting via loopback devices in the Linux environment.

2.1. Installation

First, let’s install dmg2img via apt-get and sudo:

$ sudo apt-get install dmg2img

At this point, we should have access to dmg2img.

2.2. DMG Conversion

Once installed, we convert a .dmg file to the IMG format:

$ dmg2img sample.dmg output.img

dmg2img v1.6.7 (c) vu1tur ([email protected])

sample.dmg --> output.img

decompressing:
opening partition 0 ...             100.00%  ok
opening partition 1 ...             100.00%  ok
opening partition 2 ...             100.00%  ok
opening partition 3 ...             100.00%  ok
opening partition 4 ...             100.00%  ok
opening partition 5 ...             100.00%  ok
opening partition 6 ...             100.00%  ok
opening partition 7 ...             100.00%  ok

Archive successfully decompressed as output.img

In this case, we convert the sample.dmg file to the output.img IMG file. During the process, dmg2img decompresses partitions within the .dmg file. Upon completion, dmg2img confirms the successful creation of the output file.

2.3. Mount Resulting IMG

Then, we can use the losetup command to associate the image file with a loop device:

$ sudo losetup -Pf output.img

In this case, we used -P to scan the partition table in the disk image and create loop devices for each partition found. Moreover, we used -f to find the first available loop device automatically.

In summary, we associate the image with the next available loop device.

Now, we find the appropriate loop device partition:

$ sudo ls /dev/loop*p*
/dev/loop8p1

As we can see, loop8p1 is created by the losetup command.

After obtaining the loop device path, we mount the converted image:

$ sudo mkdir /mnt/loop8p1
$ sudo mount /dev/loop8p1 /mnt/loop8p1

Now, we can use the resulting mount point as we would any other directory with read-only access.

2.4. IMG File Content Access

Thus, we can view the contents of the mount point:

$ ls /mnt/loop8p1
 Applications                  Documentation                      Resources
 Installer                     Licenses                           Uninstaller
 Support

Now, we have access to the contents of the original .dmg file.

3. Using 7z

7z is a file archiver that compresses and extracts various archive formats with high efficiency and strong compression capabilities. Furthermore, it’s commonly used for creating, extracting, and managing compressed files on multiple platforms.

Notably, sometimes 7z might struggle with .dmg files due to unsupported encryption, compression, file corruption, variant differences, or limitations within the tool.

3.1. Installation

First, let’s install 7z:

$ sudo apt-get install p7zip-full

So, we should have the 7z command to work with.

3.2. DMG Extraction

After installing, we extract the .dmg file:

$ 7z x sample.dmg 

...

Scanning the drive for archives:
1 file, 53699718 bytes (52 MiB)

Extracting archive: sample.dmg
--
Path = sample.dmg
Type = Dmg
Physical Size = 53699718
Method = Zero2 ZLIB CRC
Blocks = 143
...

Everything is Ok
Folders: 140
Files: 257
Size:       135212179
Compressed: 53699718

As we can see, the output displays information about the archive, including its size, creation, and modification dates. Here, we used x to extract the file with the full path.

3.3. DMG File Content Access

Thus, we can navigate the extracted directory:

$ ls sample
 Applications   sample.app  '[HFS+ Private Data]'

As a result, we successfully extracted contents from the .dmg file to the sample directory. Unlike our previous method, we can freely modify contents in the resulting directory, but the changes aren’t written back to the original DMG file.

4. Using guestmount

guestmount is a command-line tool that enables mounting disk images such as .dmg files to directories. Furthermore, it utilizes libguestfs to create a temporary overlay to protect the original data while providing read-only or read-write access to the mounted content.

4.1. Installation

First, let’s install the libguestfs-tools package:

$ sudo apt-get install libguestfs-tools

Thus, we have the guestmount command to work with.

4.2. Mount DMG File

Now, we mount the .dmg file to a new mount point:

$ sudo mkdir /mnt/dmg_mount
$ sudo guestmount -a sample.dmg -m /dev/sda1 --rw /mnt/dmg_mount

Let’s break down the code:

  • -a denotes the option for specifying the disk image
  • -m indicates the partition within the disk image to mount
  • –rw specifies that the mounted disk image will be in read-write mode

After mounting the .dmg file, we can access the mounted directory and check the content. Moreover, we might need to explore and identify the appropriate partition within the .dmg file to mount. Hence, we can try other partition identifiers like /dev/sda2, /dev/sda3, and so on.

5. Using guestfish

guestfish is a powerful command-line tool also provided by the libguestfs library. It’s designed to enable users to access, modify, and manipulate disk images and virtual machine filesystems directly from the command line.

5.1. Installation

First, let’s install guestfish:

$ sudo apt-get install libguestfs-tools

Once installed, we run guestfish:

$ guestfish

Welcome to guestfish, the guest filesystem shell for
editing virtual machine filesystems and disk images.

Type: ‘help’ for help on commands
      ‘man’ to read the manual
      ‘quit’ to quit the shell

><fs>

As we can see, we entered the guestfish environment.

5.2. Interact With DMG File

Then, we add the .dmg file:

><fs> add sample.dmg

In this case, we added sample.dmg to the guestfish session.

Next, we start interacting with the added disk image:

><fs> run
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00

At this point, we can perform various operations within the context of that disk image.

5.3. Listing Available Filesystems

Then, we list the available filesystems:

><fs> list_filesystems
/dev/sda1: hfsplus

In this case, /dev/sda1 is formatted as an HFS+ filesystem and available within the mounted disk image.

5.4. Mounting Identified Filesystem

Now, we mount the identified filesystem:

><fs> mount /dev/sda1 /

We mount the identified HFS+ filesystem from /dev/sda1 to the root directory / within the guestfish environment.

5.5. DMG File Content Access

Finally, we can list the contents of the root directory:

><fs> ls /
.DS_Store
.HFS+ Private Directory Data
.background
.fseventsd
Applications
sample.app

Moreover, we can use get or put to respectively copy files from our local machine into the mounted disk image or retrieve files from the mounted disk image.

6. Using darling-dmg

darling-dmg is a part of the Darling project, designed to enable users to mount .dmg files directly under Linux. This capability eliminates the need to manually extract .dmg files and mount the HFS+ filesystem.

6.1. Dependency Installation

First, let’s install dependencies:

$ sudo apt-get install libfuse-dev libbz2-dev libicu-dev libxml2-dev libssl-dev libz-dev pkg-config cmake g++

Now, we should be ready to install Darling.

6.2. Repository Installation

Then, we clone the darling-dmg repository via git and navigate to the example directory:

$ git clone https://github.com/darlinghq/darling-dmg.git
$ cd darling-dmg

After cloning the repository, we run CMake:

$ cmake .

This command examines the source code and generates necessary build files in the current directory.

Now, we compile and install darling-dmg via make:

$ make
$ sudo make install

Here, we compile the darling-dmg code using the make utility and install the resulting binaries and related files onto the system.

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/x86_64-linux-gnu

In this case, we append a directory to the LD_LIBRARY_PATH environment variable, allowing the system to locate dynamically linked libraries required by darling-dmg.

6.3. Direct DMG Mount

At this point, we can mount a .dmg file:

$ darling-dmg Telegram.dmg mounted_dir
Skipping partition of type Primary GPT Header
Skipping partition of type Primary GPT Table
Skipping partition of type Apple_Free
Using partition #3 of type Apple_HFS
Everything looks OK, disk mounted

In this instance, we verify the disk image, identify the partition, and mount it. As we can see, the output confirms that the darling-dmg tool has successfully identified and mounted the appropriate partition from the .dmg file onto the specified directory.

7. Conclusion

In this article, we’ve explored various methods to access and work with .dmg files within a Linux environment.

Through tools like dmg2img, we can convert .dmg files into the IMG format and mount them using loopback devices, facilitating access to their contents. However, the versatility of 7z might encounter limitations with .dmg files due to encryption or variant differences. Other approaches are utilities such as guestmount and guestfish. Finally, we saw the darling-dmg utility, which enables direct mounts.

These diverse methods cater to different user needs, providing a comprehensive range of options to effectively handle .dmg files in a Linux system.

Comments are closed on this article!