How to write to a Mac OSX read-only filesystem?

11

5

I have DMG I need to mount and write a file to. When I mount it, finder shows the root and all as read-only.

Then I discovered 'mount -w'. But when I run that, I get 'mount: unknown special file or file system.'

How can I mount this disk image as writable, or force a file into it?

themirror

Posted 2010-05-27T17:57:55.133

Reputation: 1 426

Answers

16

Almost all read-only DMGs are actually compressed (UDZ0 -- see the hdiutil(1) man page), so their file format doesn't support simply "flipping a bit" to make them writable.

You could use Disk Utility or

hdiutil imageinfo filename.dmg  

...to see what format your disk image is in. Then you could use Disk Utility or something like

hdiutil convert filename.dmg -format UDSP -o filename.sparseimage  

...to convert it to a read-write format. Note that the conversion does not happen in-place, so you'll have to tell it to put the output file on a filesystem that has enough room for an uncompressed copy of all the data from your .dmg.

The .dmg extension does not guarantee that the image is actually compressed, but that's by far the most likely possibility.

There are several other possibilities available to you with hdiutil. For instance, if your .dmg is actually uncompressed read-only (UDRO), it might be possible to force it to mount read-write. Also, if you want to leave your .dmg compressed but still want to mount it in a writable fashion, you can mount it with a "shadow file"; all writes actually get written to the shadow file.

Update: Other Answers on this Question seem to think .dmg always means UDZ0 which just isn't true. From the hdiutil(1) man page, here are the list of internal formats a .dmg can have (note that a couple of these can have different default filename extensions like .sparseimage, but I'm pretty sure that's not a hard-and-fast rule either).

UDRW - UDIF read/write image  
UDRO - UDIF read-only image  
UDCO - UDIF ADC-compressed image  
UDZO - UDIF zlib-compressed image  
UDBZ - UDIF bzip2-compressed image (OS X 10.4+ only)  
UFBI - UDIF entire image with MD5 checksum  
UDRo - UDIF read-only (obsolete format)  
UDCo - UDIF compressed (obsolete format)  
UDTO - DVD/CD-R master for export  
UDxx - UDIF stub image  
UDSP - SPARSE (grows with content)  
UDSB - SPARSEBUNDLE (grows with content; bundle-backed)  
RdWr - NDIF read/write image (deprecated)  
Rdxx - NDIF read-only image (Disk Copy 6.3.3 format)  
ROCo - NDIF compressed image (deprecated)  
Rken - NDIF compressed (obsolete format)  
DC42 - Disk Copy 4.2 image  

Spiff

Posted 2010-05-27T17:57:55.133

Reputation: 84 656

1Hey this helped alot, thanks! I found a super easy way to do this, take a look at my answer. Would love to hear your thoughts. – M.W. Felker – 2014-09-02T17:45:07.203

1@MaxFelker Glad my suggestion of using a shadow file helped you. Thanks for spelling out one good way of doing it. – Spiff – 2014-09-02T20:22:17.590

Super useful, +1 my dude! – M.W. Felker – 2014-09-03T20:41:08.097

I didn't assume UDZ0, but all of the types except UDRW, and UDSP (maybe UDxx UDSB or UDT0) are read only. I assumed that all dmgs were compressed with SOMETHING, and so not writable... I've never knowingly come across a NON-compressed DMG... – Brian Postow – 2010-05-27T18:51:40.917

@Brian - I use an encrypted UDSP routinely for storing sensitive information on my laptop (passwords, bank info, etc). – KeithB – 2010-05-27T19:57:25.887

5

Here is method I just used for a project and was by far the best way to do this. Found it here:

http://myjeeva.com/best-possible-way-edit-read-only-dmg.html

Attach read-only dmg file using hdiutil

hdiutil attach -owners on /path/to/your.dmg -shadow

Modify your DM. Use the command line to add, edit or delete contents - doing it in the GUI can have weird side effects or permission issues. Additionally, you may need to sudo the commands

Detach mounted drive by below command or Eject it from Finder

# fill disk number at <number>
hdiutil detach /dev/disk<number>

Converting/Writing a shadowed dmg into read-only DMG

hdiutil convert -format UDZO -o /path/to/new.dmg /path/to/your.dmg -shadow

M.W. Felker

Posted 2010-05-27T17:57:55.133

Reputation: 174

I'm on Sierra and this doesn't work for me. It creates the ".shadow" file and mounts the dmg, but the mounted drive is still readonly. I even tried making the .dmg writable and verified that the .shadow file was writeable. – TechSavvySam – 2018-02-03T14:34:10.413

1

if there's enough "space" on the Rd-onry DMG, you can convert to a R/W DMG using Disk Utility, and convert it back to a compressed DMG if you want. Otherwise, you can "expand" the DMG using hdiutil

junh1024

Posted 2010-05-27T17:57:55.133

Reputation: 31

-2

You can't write to a DMG. it makes no sense. You need to copy everything out, edit, and make a new DMG.

Sorry.

Brian Postow

Posted 2010-05-27T17:57:55.133

Reputation: 1 035

That's just not true. You can create read-write .dmg files. Also, you don't need to copy everything out, you can simply convert the disk image to a writable format. See my Answer. – Spiff – 2010-05-27T18:28:17.370

1@Spiff, I suppose, but your solution is EFFECTIVELY the the same as mine, as it has to de-compress, the you add the new file, and re-compress. If it's not in-place, then you're not REALLY writing to the existing DMG... – Brian Postow – 2010-05-27T18:47:06.097

Your answer contains false information. – Brian – 2011-02-10T02:53:03.510

1-1 but if you change the line "You can't write to a DMG" to something that is actually true, like, "You can't write to a readonly DMG" I'll change my vote. Thanks! – Dan Rosenstark – 2012-02-17T03:05:47.030

-2

A regular DMG image is not writable as there's no way to add space to it. The solution as noted by @Brian_Postow is to copy out the entire contents and create a whole new DMG file. If you plan to do this a lot, next time create a sparse image as these are writable.

Chris Nava

Posted 2010-05-27T17:57:55.133

Reputation: 7 009

1That's not exactly true. The .dmg extension does not necessarily mean that it's read-only. See my Answer. – Spiff – 2010-05-27T18:20:58.447

I should have said "default" instead of "regular". Good info on converting them though. – Chris Nava – 2010-05-28T04:07:00.840