Creating a Named Pipe on a Read Only Filesystem in Linux

I recently ran into a problem with a remote Linux install that caused the root filesystem to keep remounting itself as read-only in an attempt to keep from eating itself.  Because the filesystem was read-only, I couldn’t change any permissions, install any ssh certificates into different accounts, reset passwords or any of the other standard ways I would get at the data I needed.  Likewise, because this was a remote system, I couldn’t just boot into a different environment to read the data (there was also the fear that the drive might fail or that there might be data loss upon performing the diagnostics needed to fix the volume).  The solution I came up with was using a named pipe over ssh to funnel data.  This worked really well, but took a bit of looking around to figure out how to make it happen.

Creating a Ram Drive in Linux

To create the named pipe I had to have a filesystem I could write to.  Without the ability to add any writable physical media (diskette, usb storage, etc.) it seemed like my best option was a ram drive.  After a little searching, this turned out to be much easier than I anticipated it would be.

I found this post incredibly helpful: Ram device with TmpFS

First I identified a directory that could be used as the mount point.  I just looked for a conveniently located empty directory, though it needn’t actually be empty, you just won’t be able to access its contents while the ramdrive is mounted.

The command I used is as follows:

mount -t tmpfs -o size=1m tmpfs <mountpoint>

The command has to have root permissions, so if you’re not running as root you’ll need to throw ‘sudo’ on the front end of the command.  As should be intuitive, you can change the size as desired, though it can be negligible in size for just holding a named pipe, as that has zero size on disk.  The mountpoint is just the path to the place you want to mount the ram drive.

Creating the Named Pipe

It would be hard for this part to be any easier.  Navigate to the ram drive so you have read access and run:

mkfifo <nameOfPipe>

The name of the pipe can of course be whatever valid file name you like.

Putting it to Use

For those interested, I was moving data off of the system by running:

tar -cp /path/to/data | gzip -9c > <namedPipe>

Then, on the machine I wanted to dump the data to, this was run:

ssh user@host ‘cat /path/to/namedPipe’ | pv -trab > file.tgz

The first command packages all the data in the given directory (and its subdirectories) up into a tar archive.  That data is fed through gzip to compress it before piping it over to the named pipe.

On the receiving end, we’re dumping the contents of the pipe over an ssh connection to a file, by way of the pv command to monitor the flow of data.

The pv command is a fantastic tool.  It gives you data about total data transfered, average and instantaneous transfer rates, among other useful data.  Few systems I’ve seen come with this tool installed, so you’ll probably have to install it if you want to use it.


Leave a Reply