如何安全地从桌面移除(卸载)外部硬盘?

如何安全地从桌面移除(卸载)外部硬盘?

系统规格:

  • Ubuntu 16.10
  • 统一
  • Nautilus 文件浏览器
  • 外置 1TB USB 硬盘

目的:

我设想单击桌面上的外部硬盘图标,然后通过上下文菜单项来安全地将其删除。

相关信息:

我可以通过打开文件浏览器并点击侧边栏中的卸载图标来安全地移除相关驱动器。但这违背了目的,因为我想将其实现到桌面的上下文菜单中,以便更快地访问。

在此处输入图片描述

答案1

必须将下面提供的脚本放入~/.local/share/nautilus/scripts并更改其可执行权限(通过命令行chmod +x或在 Nautilus 中右键单击)。该脚本执行驱动器的卸载和关闭,以便安全弹出。

也可在GitHub

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import subprocess
import urllib.parse
import dbus


def get_dbus( bus_type, obj, path, interface, method, arg):
    """ utility: executes dbus method on specific interface"""
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    method = proxy.get_dbus_method(method, interface)
    if arg:
        return method(arg)
    else:
        return method()

def get_dbus_property( bus_type, obj, path, iface, prop):
    """ utility:reads properties defined on specific dbus interface"""
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    aux = 'org.freedesktop.DBus.Properties'
    props_iface = dbus.Interface(proxy, aux)
    try:
        props = props_iface.Get(iface, prop)
        return props
    except:
        return None

def get_mountpoint(dev_path):
    try:
        contents = [
            'system', 'org.freedesktop.UDisks2', dev_path,
            'org.freedesktop.UDisks2.Filesystem', 'MountPoints'
        ]
        data = get_dbus_property(*contents)[0]

    except:
        return None
    else:
        if len(data) > 0:
            return ''.join([chr(byte) for byte in data])


def find_partition(selected):

    contents = [
        'system', 'org.freedesktop.UDisks2', '/org/freedesktop/UDisks2',
        'org.freedesktop.DBus.ObjectManager', 'GetManagedObjects', None
    ]
    objects = get_dbus(*contents)
    for item in objects:
        try:
            if 'block_devices' in str(item):
                mountpoint = get_mountpoint(item)
                if not mountpoint:
                    continue
                mountpoint = mountpoint.replace('\x00', '')
                mp = str(type(mountpoint))
                if selected == mountpoint:
                    return  '/dev/' + item.split('/')[-1]

        except Exception as e:
            sys.stderr.write(e.__repr__())
            subprocess.call(['zenity','--error','--text',str(e)])

def main():

    uri = os.getenv("NAUTILUS_SCRIPT_SELECTED_URIS")
    uri_decoded = urllib.parse.unquote(uri)
    mountpath = uri_decoded.replace('file://','').strip()

    part = find_partition(mountpath)

    try:
        unmount = ['udisksctl', 'unmount', '-b', part ]
        subprocess.check_output(unmount, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        subprocess.call([ 'zenity', '--error','--text', e.output.decode()])

    # Try to power off the drive ( this is for ejecting USBs)
    # If it's a hard-drive with more than one partition in use, disk
    # won't power-off
    try:
        poweroff = ['udisksctl', 'power-off', '-b',part]
        subprocess.check_output(poweroff, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        pass


if __name__ == '__main__' : 
    try:
        main()
    except Exception as e:
        sys.stderr.write(e)
        subprocess.call(['zenity','--error','--text',str(e)])

相关内容