vfs — virtual filesystem control¶
The vfs module contains functions for creating filesystem objects and
mounting/unmounting them in the Virtual Filesystem.
Filesystem mounting¶
Some ports provide a Virtual Filesystem (VFS) and the ability to mount multiple
“real” filesystems within this VFS. Filesystem objects can be mounted at either
the root of the VFS, or at a subdirectory that lives in the root. This allows
dynamic and flexible configuration of the filesystem that is seen by Python
programs. Ports that have this functionality provide the mount() and
umount() functions, and possibly various filesystem implementations
represented by VFS classes.
- vfs.mount(fsobj: Any, mount_point: str, *, readonly: bool = False) None¶
Mount the filesystem object fsobj at the location in the VFS given by the mount_point string. fsobj can be a a VFS object that has a
mount()method, or a block device. If it’s a block device then the filesystem type is automatically detected (an exception is raised if no filesystem was recognised). mount_point may be'/'to mount fsobj at the root, or'/<name>'to mount it at a subdirectory under the root.If readonly is
Truethen the filesystem is mounted read-only.During the mount process the method
mount()is called on the filesystem object.Will raise
OSError(EPERM)if mount_point is already mounted.
- vfs.mount() List[Tuple[Any, str]]
With no arguments to
mount(), return a list of tuples representing all active mountpoints.The returned list has the form [(fsobj, mount_point), …].
- vfs.umount(mount_point: str | Any) None¶
Unmount a filesystem. mount_point can be a string naming the mount location, or a previously-mounted filesystem object. During the unmount process the method
umount()is called on the filesystem object.Will raise
OSError(EINVAL)if mount_point is not found.
- class vfs.VfsFat(block_dev: AbstractBlockDev)¶
Create a filesystem object that uses the FAT filesystem format. Storage of the FAT filesystem is provided by block_dev. Objects created by this constructor can be mounted using
mount().- static mkfs(block_dev: AbstractBlockDev) None¶
Build a FAT filesystem on block_dev.
- class vfs.VfsRom(buffer: bytes | bytearray | memoryview)¶
Create a filesystem object that uses the ROMFS read-only filesystem format.
buffermust be an object supporting the buffer protocol (bytes,bytearrayormemoryview) that contains a valid ROMFS image.Objects created by this constructor can be mounted using
mount().See Working with ROMFS for full details, including how to build and deploy ROMFS images with mpremote.
- vfs.rom_ioctl(op: int, *args: Any) Any¶
Low-level interface for accessing the read-only memory (ROM) partition(s) of the device. The supported operations are:
Call
Behaviour
rom_ioctl(1)Return the number of available ROM partitions.
rom_ioctl(2, id)Return partition
idas amemoryview.rom_ioctl(3, id, length)Erase the first
lengthbytes of partitionidin preparation for writing. Returns the minimum write alignment in bytes.rom_ioctl(4, id, offset, buf)Write
bufto partitionidat byteoffset.rom_ioctl(5, id)Finalise a write sequence to partition
id(flushes caches, etc.).These operations are normally invoked indirectly by mpremote when deploying a ROMFS image; most applications do not need to call them directly.
- class vfs.VfsPosix(root: str | None = None)¶
Create a filesystem object that accesses the host POSIX filesystem. If root is specified then it should be a path in the host filesystem to use as the root of the
VfsPosixobject. Otherwise the current directory of the host filesystem is used.Note
VfsPosixis only available on the MicroPython Unix port; it is not present in OpenMV Cam firmware.
Block devices¶
A block device is an object which implements the block protocol. This enables a
device to support MicroPython filesystems. The physical hardware is represented
by a user defined class. The AbstractBlockDev class is a template for
the design of such a class: MicroPython does not actually provide that class,
but an actual block device class must implement the methods described below.
A concrete implementation of this class will usually allow access to the
memory-like functionality of a piece of hardware (like flash memory). A block
device can be formatted to any supported filesystem and mounted using os
methods.
See Working with filesystems for example implementations of block devices using the two variants of the block protocol described below.
Simple and extended interface¶
There are two compatible signatures for the readblocks and writeblocks
methods (see below), in order to support a variety of use cases. A given block
device may implement one form or the other, or both at the same time. The second
form (with the offset parameter) is referred to as the “extended interface”.
Some filesystems require more control over write operations – for example, writing to sub-block regions without erasing – and need the block device to support the extended interface.
- class vfs.AbstractBlockDev¶
Documentation template for the block-device protocol. MicroPython does not actually expose this class — it is shown here only to document the methods a user-defined block-device class must implement. Constructor arguments are entirely up to the implementation (typically things like flash bus, chip-select pin, sector size, etc.).
- readblocks(block_num: int, buf: bytearray) None¶
- readblocks(block_num: int, buf: bytearray, offset: int) None
Read bytes from the device into buf. Two overloads expose the simple and extended interfaces.
Simple form (
readblocks(block_num, buf)): reads whole blocks starting at block index block_num.len(buf)must be a multiple of the block size, and the number of blocks read islen(buf) // block_size.Extended form (
readblocks(block_num, buf, offset)): readslen(buf)bytes – not necessarily a whole number of blocks – starting at byteoffsetwithin block block_num. Use this form when the filesystem needs sub-block read access.
- writeblocks(block_num: int, buf: bytes) None¶
- writeblocks(block_num: int, buf: bytes, offset: int) None
Write bytes from buf to the device.
Simple form (
writeblocks(block_num, buf)): writes whole blocks starting at block index block_num.len(buf)must be a multiple of the block size, and the number of blocks written islen(buf) // block_size. The implementation is responsible for erasing each destination block first if the underlying hardware requires it.Extended form (
writeblocks(block_num, buf, offset)): writeslen(buf)bytes – not necessarily a whole number of blocks – starting at byteoffsetwithin block block_num. Only the bytes being written may change; the caller is responsible for ensuring affected blocks have been erased via a priorioctl(6, block_num)call. Implementations of this form must never implicitly erase a block, even whenoffsetis zero.
- ioctl(op: int, arg: int) int | None¶
Control the block device and query its parameters. The operation to perform is given by op which is one of the following integers:
1 – initialise the device (arg is unused)
2 – shutdown the device (arg is unused)
3 – sync the device (arg is unused)
4 – get a count of the number of blocks, should return an integer (arg is unused)
5 – get the number of bytes in a block, should return an integer, or
Nonein which case the default value of 512 is used (arg is unused)6 – erase a block, arg is the block number to erase
As a minimum
ioctl(4, ...)must be intercepted; filesystems that use the extended interface additionally requireioctl(6, ...). The need for the other operations is hardware-dependent.Before any call to
writeblocks(block, ...)a filesystem that uses the extended interface issuesioctl(6, block)so the driver can erase the block first if the hardware requires it. A driver may instead interceptioctl(6, block)and return 0 (success), taking on the responsibility for detecting when erasure is needed itself.Unless otherwise stated
ioctl(op, arg)can returnNone. Consequently an implementation can ignore unused values ofop. Whereopis intercepted, the return value for operations 4 and 5 are as detailed above. Other operations should return 0 on success and non-zero for failure, with the value returned being anOSErrorerrno code.