| 1 |
#! /bin/bash |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
set -eux |
| 5 |
|
| 6 |
dev=$1 |
| 7 |
|
| 8 |
need_unmount="" |
| 9 |
|
| 10 |
maybe_unmount () { |
| 11 |
if [ -n "$need_unmount" ]; then |
| 12 |
umount "$need_unmount" |
| 13 |
rmdir "$need_unmount" |
| 14 |
fi |
| 15 |
} |
| 16 |
|
| 17 |
trap maybe_unmount EXIT INT QUIT |
| 18 |
|
| 19 |
mp=$(findmnt -no TARGET "$dev" | cat) |
| 20 |
if [ -z "$mp" ]; then |
| 21 |
mp=$(mktemp -d) |
| 22 |
need_unmount=$mp |
| 23 |
mount "$dev" "$mp" -o ro |
| 24 |
fi |
| 25 |
|
| 26 |
# A filesystem is empty if it only has directories in it. |
| 27 |
|
| 28 |
first=$(find "$mp" -not -type d | head -1) |
| 29 |
info=$(stat -f -c '{ "unit": %S, "free": %f, "total": %b }' "$mp") |
| 30 |
|
| 31 |
if [ -z "$first" ]; then |
| 32 |
echo yes |
| 33 |
else |
| 34 |
echo "$info" |
| 35 |
fi |