You are not logged in.

#1 2020-02-08 15:43:59

IceHand
Member
From: Germany
Registered: 2007-05-06
Posts: 53
Website

[SOLVED] Power off all external usb drives (e.g. on shutdown)

I was looking at several solutions for powering off all mounted usb drives and none worked quite as I wanted. If anyone is having the same problem: I wrote the below bash script which uses udisksctl and works reliably for me. It can be run any time you want (e.g. after a backup script or before a shutdown) and does not need to be run as root. This will only work for usb drives auto-mounted on /run/media/
I haven't tested it with usb drives with more than one partition.

#!/bin/bash
# Unmount and power off any user mounted drives.

mapfile -t ArrayUnmount < <(mount | grep "on /run/media/" | cut -c 1-9)
mapfile -t ArrayPoweroff < <(mount | grep "on /run/media/" | cut -c 1-8)

if [ ${#ArrayUnmount[@]} -gt 0 ]
then
  echo "Writing files/cache to disks"
  sync

  for i in "${ArrayUnmount[@]}"; do
    echo "Unmounting USB disks"
    udisksctl unmount -b "$i"
  done

  for i in "${ArrayPoweroff[@]}"; do
    echo "Powering off USB disks"
    udisksctl power-off -b "$i"
  done
fi

Last edited by IceHand (2020-02-08 19:28:36)

Offline

Board footer

Powered by FluxBB