2

I have a memory filesystem in Python, created in the following fashion:

import fs
mem_fs = fs.open_fs('mem://')
mem_fs.makedirs('/dir1')
with mem_fs.open('/dir1/file1', 'w') as file1:
    file1.write('test')

I want to mount this filesystem onto a directory in my OS filesystem (e.g., /home/user/mem_dir). I can create an object that would reference OS filesystem:

os_fs = fs.open_fs('/home/user/mem_dir')

But then I have no idea how to go about mounting mem_fs onto os_fs. I tried using MountFS class, but it only creates a virtual filesystem. I need to create a mount point in a way that other external applications (e.g., nautilus) would be able to see it and copy files to/from there. Any feedback would be appreciated.

Proto Ukr
  • 492
  • 4
  • 13
  • Why not just mount a `ramfs` volume provided by the kernel? – Klaus D. Jan 27 '20 at 23:36
  • @Klaus do you mean using _mount_ command? I'd like to be able to do everything inside Python since this is just a tiny portion of my overall code. – Proto Ukr Jan 27 '20 at 23:44
  • You could look into something like this: https://stackoverflow.com/a/29156997/6744133 – Oli Jan 28 '20 at 00:42

1 Answers1

1

I had the same requirement, ans i've managed it this way

from fs.tempfs import TempFS
tmp = TempFS(identifier='_toto', temp_dir='tmp/ramdisk/')

it does mount create a directory, with an arbitrary name suffixed by _toto

tmp/ramdisk ❯❯❯ ls
tmpa1_4azgi_toto

which is totaly available as a standard filesystem in the host as in your python code

tmp/ramdisk/tmpa1_4azgi_toto ❯❯❯ mkdir test                                                                    
tmp/ramdisk/tmpa1_4azgi_toto ❯❯❯ ls                                                                            
test

 >>> tmp.listdir('/')
['test']

it look quite magic as it does not appear at all in the mounted host's filesystem

 ❯❯❯ df -ah | grep -E '(ramdisk|tmp)'                                              
tmpfs                       785M    1,7M  783M   1% /run
tmpfs                       3,9G    195M  3,7G   5% /dev/shm
tmpfs                       5,0M    4,0K  5,0M   1% /run/lock
tmpfs                       3,9G       0  3,9G   0% /sys/fs/cgroup
tmpfs                       785M     36K  785M   1% /run/user/1000

and it does totally disepear when your code end, or when you call

>>> tmp.close()

tmp/ramdisk ❯❯❯ ls
tmp/ramdisk ❯❯❯

Cheers

Dharman
  • 30,962
  • 25
  • 85
  • 135
Cyril Gratecos
  • 291
  • 2
  • 6
  • 1
    This might not have the desired effect. While "tmpfs" in Linux means a ramdisk, i.e. a filesystem stored in memory, fs.tempfs seems to just be referring to an on-disk filesystem that is temporary. Edit: it turns out that whether /tmp is a ramdisk or a normal disk is dependent on the OS. – interoception Aug 03 '21 at 16:53
  • This is not in-memory. If I close the python session the directory remains, and if i reboot the directory is still there. – user3496060 Oct 17 '21 at 21:44