How about LittleFS.begin()

I’m experimenting with LittleFS.
What does the function ‘LittleFS.begin()’ realy do?
PlatformIO gives me this:

  bool fs::LittleFSFS::begin(
  	bool formatOnFail = false, 
  	const char* basePath = "/littlefs",     <== default values?
  	uint8_t maxOpenFiles = (uint8_t)10U, 
  	const char* partitionLabel = "spiffs"
  )

From this bit of information I understand that:
1.
bool formatOnFail = false, => LittleFS will format the complete ‘disk’ and all data will belost.
2.
basePath = “/littlefs” => this is a default value that’s used when this parameter is omitted.
3.
maxOpenFiles = (uint8_t)10U, => ibid
4.
partitionLabel = “spiffs” => ibid.
=> Also, that when there is no NAME = ‘spiffs’ key*value combination
=> in the partiontable the whole thing will not work.
Stated otherwise:
The partitiontable.csv must have a corresponding value, like so:

# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  20480,
otadata,  data, ota,     ,  	  8192,
app0,     app,  ota_0,   , 		  1966080,
spiffs,   data, spiffs,  ,		  2097152,
coredump, data, coredump,,		  65536,

Can you please confirm or deny this?

When .begin() is called, it first looks to mount an existing LittleFS filesystem (from the address derived in the partition table for the littelfs/“spiffs” section). When mounting fails, because e.g. the filesystem is corrupted or has never been formatted, it will format it in the LitteFS format, if formatOnFail = true. After that, mounting the freshly formatted filesystem should work and .begin() will most likely return true. If formatOnFail = false, and mounting fails, then false is returned and no data is erased.

In a way, .begin() is just a fancy wrapper around the underlying functions lfs_mount(), lfs_format() (source) and integrating the filepaths into Espressif’s “Virtual File System” (VFS).

Everything else seems to be correct.

1 Like

Thank you Max,
That clears the air with respect to the ‘formatOnFail’ part.
Now, as I understand it: ‘basePath’ can have a value that I - the programmer - can set. And that goes as well for ‘partitionLabel’.
Only: the value for ‘partitionLable’ MUST be the same as the value in the partition tabel; where Name ‘xyz’ corresponds with partitionLabel = ‘xyz’
Am I right?

Yes. If you’re using default partition tables, you should not need any of these extra parameters, except the formatOnFail.

So, if you want to always have an initialized (formatted if needed) filesystem, LittleFS.begin(true); is the right choice.

Max,
Again thank you. You make me happy.