| 1 | #!/bin/bash -f |
| 2 | # SPDX-License-Identifier: GPL-2.0 |
| 3 | # |
| 4 | # Copyright (c) 2024 Oracle. All Rights Reserved. |
| 5 | # Author: Darrick J. Wong <[email protected]> |
| 6 | # |
| 7 | |
| 8 | OPTS="" |
| 9 | USAGE="Usage: xfs_property [-V] [mountpoint|device|file] [list [-v]|get name...|set name=value...|remove name...]" |
| 10 | |
| 11 | # Try to find a loop device associated with a file. We only want to return |
| 12 | # one loopdev (multiple loop devices can attach to a single file) so we grab |
| 13 | # the last line and return it if it's actually a block device. |
| 14 | try_find_loop_dev_for_file() { |
| 15 | local x="$(losetup -O NAME -j "$1" 2> /dev/null | tail -n 1)" |
| 16 | test -b "${x}" && echo "${x}" |
| 17 | } |
| 18 | |
| 19 | while getopts "V" c |
| 20 | do |
| 21 | case $c in |
| 22 | V) xfs_io -p xfs_info -V |
| 23 | status=$? |
| 24 | exit ${status} |
| 25 | ;; |
| 26 | *) echo "${USAGE}" 1>&2 |
| 27 | exit 2 |
| 28 | ;; |
| 29 | esac |
| 30 | done |
| 31 | set -- extra "$@" |
| 32 | shift $OPTIND |
| 33 | |
| 34 | if [ $# -lt 2 ]; then |
| 35 | echo "${USAGE}" 1>&2 |
| 36 | exit 2 |
| 37 | fi |
| 38 | |
| 39 | target="$1" |
| 40 | shift |
| 41 | subcommand="$1" |
| 42 | shift |
| 43 | |
| 44 | db_args=() |
| 45 | io_args=() |
| 46 | |
| 47 | case "$subcommand" in |
| 48 | "list") |
| 49 | vparam= |
| 50 | if [ $# -eq 1 ] && [ "$1" = "-v" ]; then |
| 51 | vparam=" -v" |
| 52 | fi |
| 53 | db_args+=('-c' "attr_list -Z${vparam}") |
| 54 | io_args+=('-c' "listfsprops${vparam}") |
| 55 | ;; |
| 56 | "get"|"remove"|"set") |
| 57 | for arg in "$@"; do |
| 58 | db_args+=('-c' "attr_${subcommand} -Z ${arg/=/ }") |
| 59 | io_args+=('-c' "${subcommand}fsprops ${arg}") |
| 60 | done |
| 61 | ;; |
| 62 | *) |
| 63 | echo "${USAGE}" 1>&2 |
| 64 | exit 2 |
| 65 | esac |
| 66 | |
| 67 | # See if we can map the arg to a loop device |
| 68 | loopdev="$(try_find_loop_dev_for_file "${target}")" |
| 69 | test -n "${loopdev}" && target="${loopdev}" |
| 70 | |
| 71 | # If we find a mountpoint for the device, do a live query; otherwise try |
| 72 | # reading the fs with xfs_db. |
| 73 | if mountpt="$(findmnt -t xfs -f -n -o TARGET "${target}" 2> /dev/null)"; then |
| 74 | exec xfs_io -p xfs_property "${io_args[@]}" "${mountpt}" |
| 75 | else |
| 76 | exec xfs_db -p xfs_property -x -c 'path /' "${db_args[@]}" "${target}" |
| 77 | fi |