summaryrefslogtreecommitdiff
path: root/src/btrfs-utils/btrfs-truncate
blob: 00758441f01073cf68013842b3423379fd1b6271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/sh
[ "$(id -u)" = 0 ] || exec sudo "$0" "$@"

btrfs_truncate()
{
  btrfs filesystem show --raw "$1" |
    while read devid d size s used u path p
    do
      [ "$devid $size $used $path" = 'devid size used path' ] || continue

      case "$p" in
        /dev/loop*) dev=$(losetup -n -l -O back-file "$p") ;;
        *) dev=$p ;;
      esac

      if [ -f "$dev" ]
      then
        [ "$s" = "$(stat -c %s "$dev")" ] || truncate -s "$s" "$dev" || break
      fi
    done
}

if [ $# = 0 ]
then
  echo 'Usage: $0 <btrfs mountpoint> [...]' >&2
  exit 1
fi

for fs
do
  btrfs_truncate "$fs" || exit
done