Search code examples
python-3.xglibudisks

How to mount a device read-only with Python3 and Udisks2?


I need to (temporary) mount a device read-only with Python3 and the Udisks2 API. What is the correct GLib.Variant for this?

I've created the below script to test with a USB pen drive. It assumes /dev/sdc1 as this device.

#! /usr/bin/env python3

MYDEVICE = '/dev/sdc1'

import gi
gi.require_version('UDisks', '2.0')
from gi.repository import UDisks, GLib

read_only = GLib.Variant('a{sv}', {'read-only': GLib.Variant('b', True)})

client = UDisks.Client.new_sync(None)
manager = client.get_object_manager()
for obj in manager.get_objects():
    block = obj.get_block()
    if not block: continue
    device_path = block.get_cached_property("Device").get_bytestring().decode('utf-8')
    if device_path == MYDEVICE:
        fs = obj.get_filesystem()
        mount_points = fs.call_mount_sync(read_only, None)
        print((mount_points))

This will mount the device but it is not mounted read-only.

I would have expected it to mount the device as read-only or at least throw an error if the read-only option is not valid.


Solution

  • Use GLib.Variant('a{sv}', {'options': GLib.Variant('s', 'ro')}) to pass the standard ro option through to mount.

    See the Mount() documentation for udisks’ D-Bus interface.