How to bind mount root filesystem with MicroPython Docker container? #9743
Replies: 4 comments 2 replies
-
You could look in mpremote at class RemoteFS You can use the logic there to mount a file system you completely control into the micropython file system. You can: |
Beta Was this translation helpful? Give feedback.
-
The unix port just mounts / from the host VFS at / in the MicroPython VFS. So the best you can do is to ensure that there is a directory /flash in the docker container. In your bind mount, I think you want If you want to just have /flash and nothing else, then I think the best alternative is to build the unix port without We should maybe make "mounting the host VFS" a command line option to the unix port... (see unix/main.c, search for |
Beta Was this translation helpful? Give feedback.
-
Well I am actually making my own file system appear as '/' and another file system appear as '/flash' flashdev = mounting.RAMBlockDev(sysgen.ramblk, sysgen.fake_blocks)
os.VfsLfs2.mkfs(flashdev) # cpython fails here
# Create fake_flash as a file system, and build directory tree
fake_flash = os.VfsLfs2(flashdev)
fake_flash.mkdir('hot')
fake_flash.mkdir('hot/system')
fake_flash.mkdir('hot/bureau')
# copy all normal files i.e. not directories
for stat in os.ilistdir('../system'):
if stat[1] & sysgen.flagmask == sysgen.fileflag:
with open('../system/' + stat[0], 'r') as fin:
with fake_flash.open('hot/system/' + stat[0], 'w') as fout:
fout.write(fin.read()) # reading whole file in one chunk simple but bad for memory
with open('../bureau/unitsapi.py', 'r') as fin:
with fake_flash.open('hot/bureau/unitsapi.py', 'w') as fout:
fout.write(fin.read()) # reading whole file in one chunk simple but bad for memory
# fake_flash is ready. Make it unwriteable, and put it into mounts
flashdev.read_only = True At this point you could simply: os.umount('/')
os.mount(fake_flash, '/') RAMBlockDev is taken from the documentation on file system, except I added a read_only class RAMBlockDev:
"""block device is mounted as a directory in the file system"""
read_only = False
def __init__(self, block_size, num_blocks):
self.block_size = block_size
self.num_blocks = num_blocks
self.mem_alloc = bytearray(block_size * num_blocks)
def __str__(self):
return f'<{self.__class__.__name__} blksize={self.block_size}, blocks={self.num_blocks}>'
def readblocks(self, block_num, buf, offset=0):
addr = block_num * self.block_size + offset
for i in range(len(buf)):
buf[i] = self.mem_alloc[addr + i]
def writeblocks(self, block_num, buf, offset=None):
if self.read_only:
raise VolumeReadOnly()
if offset is None:
# do erase, then write
for i in range(len(buf) // self.block_size):
self.ioctl(6, block_num + i)
offset = 0
addr = block_num * self.block_size + offset
for i in range(len(buf)):
self.mem_alloc[addr + i] = buf[i]
def ioctl(self, op, arg):
if op == 4: # block count
return self.num_blocks
if op == 5: # block size
return self.block_size
if op == 6: # block erase
return int(self.read_only) # if read_only return 1. 0=success |
Beta Was this translation helpful? Give feedback.
-
Thanks for all the help and suggestions. After messing around with various options, I finally settled on this:
This bind mounts the two directories I wanted and the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a Docker container with the MicroPython Linux port running that I'd like to use for automated code tests. However, I'm currently stuck at figuring out how to bind mount a Linux directory to simulate microcontroller flash storage for the container.
At the REPL prompt, when I do
os.listdir()
, I see the entire root filesystem of the Linux host where I'm running the container. What I'd like to do is limit the contents of MicroPython's / to particular directory on the Linux host (e.g. ~/flash) or a Docker volume to more closely match the microcontroller environment.In other words, given a directory /home/dave/flash, with a single file, main.py, I'd like to have
os.listdir('/')
show only main.py.I tried a bind mount, like this:
But, I get the error from Docker:
I also tried:
docker run -v ${PWD}/flash:/ -it --rm micropython/unix:latest
, but that just gave me an extra directory called /flash.How can I make a Linux directory or Docker volume appear as the root of MicroPython's filesystem?
Beta Was this translation helpful? Give feedback.
All reactions