How to get the volume name of mounted device from MP code?

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

How to get the volume name of mounted device from MP code?

Post by aivarannamaa » Tue Jul 30, 2019 6:27 am

I'm building a MicroPython file manager for my IDE. In cases when `/flash` is mounted as a flash drive, I need to do file operations via the mounted drive (as this part of the file tree is not writable for MP code).

So far I've used the fixed name ("PYBFLASH") for locating the drive, but it is not reliable as the user can change it.

With CircuitPython I can read the volume name with `storage.getmount('/').label`. Can I do something similar with PyBoard and other devices which present their filesystems as flash drive? Can I check with MP whether and which part of the file tree is mounted? In other words -- can I learn that Pyboard presents `/flash` as a flash drive by inspecting the system from inside?

Are there means for locating matching drive when several Pyboards with same drive name get connected? I'm using `pyserial` package for connecting with the REPL.
Aivar Annamaa
https://thonny.org

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to get the volume name of mounted device from MP code?

Post by jimmo » Tue Jul 30, 2019 12:56 pm

Hi,

Can you clarify what you mean by "as this part of the file tree is not writable for MP code"? Sounds like you have a situation where the MSC filesystem isn't mounted in the MicroPython VFS? How does that happen? My understanding was that MSC filesystem is always the same as boot filesystem.

I'm not aware of a way to query the volume name...But to your other question pyb.usb_mode() returns a string containing MSC, then you know that the boot filesystem is the MSC filesystem. And I guess you could infer from the existence of /flash and /sd and /sd/SKIPSD which one is the boot filesystem?

It'd be good if you can always do fileystem access via the REPL (i.e. the same method as `pyboard.py -f`, rshell and ampy), because then you'll support all boards even if they don't do USB MSC, and because it's much more reliable against corruption than MSC. But from your question I can tell you're already aware of this, so I'm curious as to why it's not at option.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: How to get the volume name of mounted device from MP code?

Post by aivarannamaa » Tue Jul 30, 2019 1:41 pm

Thank you for the reply!

> Can you clarify what you mean by "as this part of the file tree is not writable for MP code"?

I remember that last time when I tried to write under /flash, I got an exception about something being read-only. As I knew that CircuitPython keeps the filesystem read-only for CP code, I inferred that Pyboard does the same.

When I now tested it again, I could write without problems...

Should I worry about OS presenting outdated state via PYBFLASH drive after I modify /flash via MP code? Is `uos.sync()` relevant here? Or should I refresh the drive somehow with OS means?
Aivar Annamaa
https://thonny.org

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: How to get the volume name of mounted device from MP code?

Post by jimmo » Tue Jul 30, 2019 2:34 pm

aivarannamaa wrote:
Tue Jul 30, 2019 1:41 pm
I remember that last time when I tried to write under /flash, I got an exception about something being read-only. As I knew that CircuitPython keeps the filesystem read-only for CP code, I inferred that Pyboard does the same.
Oh interesting - I didn't know that about CircuitPython. Yeah that's definitely not the case for regular MicroPython.
aivarannamaa wrote:
Tue Jul 30, 2019 1:41 pm
Should I worry about OS presenting outdated state via PYBFLASH drive after I modify /flash via MP code? Is `uos.sync()` relevant here? Or should I refresh the drive somehow with OS means?
Yes, but unfortunately there's no really good solution here. You both can't prevent the PC seeing an outdated version of the filesystem, nor prevent it from doing writes based on outdated state. Unfortunately uos.sync doesn't help because you can't force the PC to understand that the MSC device has changed (other than unmounting/remounting). Perhaps your IDE could unmount/remount the USB drive on the host side before/after doing filesystem writes via MP code?

Although hopefully if the user is using your IDE they won't be accessing it using the USB drive? It's mostly harmless in that case?

I realise that it doesn't help you, but you'll see a few people in the forum generally recommending to disable the MSC feature and exclusively use `pyboard.py -f` / rshell / ampy because MSC syncing causes so many problems.

User avatar
aivarannamaa
Posts: 171
Joined: Fri Sep 22, 2017 3:19 pm
Location: Estonia
Contact:

Re: How to get the volume name of mounted device from MP code?

Post by aivarannamaa » Tue Jul 30, 2019 3:29 pm

jimmo wrote:
Tue Jul 30, 2019 2:34 pm
MSC syncing causes so many problems.
Is it also the case when I complement each modification with fsync? I read that in CircuitPython this is supposed to solve (most?) problems:

* https://learn.adafruit.com/welcome-to-c ... ve-it-7-13
* https://atom.io/packages/fsync-on-save
Aivar Annamaa
https://thonny.org

Post Reply