]> git.proxmox.com Git - zfsonlinux.git/blame - debian/tree/zfsutils-linux/usr/lib/zfs-linux/trim
trim: clean up, fix
[zfsonlinux.git] / debian / tree / zfsutils-linux / usr / lib / zfs-linux / trim
CommitLineData
dceb3ed0 1#!/bin/sh -u
f17f9da9
SI
2
3# directly exit successfully when zfs module is not loaded
4if ! [ -d /sys/module/zfs ]; then
5 exit 0
6fi
7
8# [auto] / enable / disable
9PROPERTY_NAME="org.debian:periodic-trim"
10
11get_property () {
12 # Detect the ${PROPERTY_NAME} property on a given pool.
13 # We are abusing user-defined properties on the root dataset,
14 # since they're not available on pools https://github.com/openzfs/zfs/pull/11680
15 # TODO: use zpool user-defined property when such feature is available.
16 pool="$1"
dceb3ed0 17 zfs get -H -o value "${PROPERTY_NAME}" "${pool}" 2>/dev/null
f17f9da9
SI
18}
19
20trim_if_not_already_trimming () {
21 pool="$1"
22 if ! zpool status "${pool}" | grep -q "trimming"; then
dceb3ed0
SI
23 # This will error on HDD-only pools: doesn't matter
24 zpool trim "${pool}"
f17f9da9
SI
25 fi
26}
27
dceb3ed0
SI
28# Walk up the kernel parent names:
29# this will catch devices from LVM &a.
68b54bb6 30get_transp () {
dceb3ed0
SI
31 dev="$1"
32 while pd="$(lsblk -dnr -o PKNAME "$dev")"; do
33 if [ -z "$pd" ]; then
34 break
35 else
36 dev="/dev/$pd"
37 fi
38 done
39 lsblk -dnr -o TRAN "$dev"
68b54bb6
SI
40}
41
dceb3ed0
SI
42pool_is_nvme_only () {
43 pool="$1"
44 # get a list of devices attached to the specified pool
45 zpool list -vHP "${pool}" | \
46 awk -F'\t' '$2 ~ "^/dev/" {print $2}' | \
47 while read -r dev
48 do
49 [ "$(get_transp "$dev")" = "nvme" ] || return
50 done
f17f9da9
SI
51}
52
53# TRIM all healthy pools that are not already trimming as per their configs.
54zpool list -H -o health,name 2>&1 | \
55 awk -F'\t' '$1 == "ONLINE" {print $2}' | \
dceb3ed0 56while read -r pool
f17f9da9
SI
57do
58 # read user-defined config
dceb3ed0
SI
59 ret=$(get_property "${pool}") || continue
60 case "${ret}" in
61 disable);;
62 enable) trim_if_not_already_trimming "${pool}" ;;
63 -|auto) pool_is_nvme_only "${pool}" && trim_if_not_already_trimming "${pool}" ;;
64 *) cat > /dev/stderr <<EOF
f17f9da9
SI
65$0: [WARNING] illegal value "${ret}" for property "${PROPERTY_NAME}" of ZFS dataset "${pool}".
66$0: Acceptable choices for this property are: auto, enable, disable. The default is auto.
67EOF
dceb3ed0 68 esac
f17f9da9 69done