Skip to content

Instantly share code, notes, and snippets.

@salewski
Last active July 4, 2023 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salewski/008f75c4a7c572168630fa0fec995959 to your computer and use it in GitHub Desktop.
Save salewski/008f75c4a7c572168630fa0fec995959 to your computer and use it in GitHub Desktop.
cron/anacron-suitable fstrim(8) invocation
#!/bin/bash -
# SPDX-FileCopyrightText: <text>
# © 2023 Alan D. Salewski <ads@salewski.email>
# </text>
#
# SPDX-License-Identifier: GPL-2.0-or-later
# This file has been saved as a GitHub Gist here:
#
# https://gist.github.com/salewski/008f75c4a7c572168630fa0fec995959
# fstrim.cron: Wrapper around the fstrim(8) command, suitable for including in
# a crontab.
#
# On a Debian-based system, the root user can copy this file into one of the
# existing system-level directories for periodic invocation:
#
# # cp -pi /path/to/fstrim.cron /etc/cron.monthly/LOCAL-fstrim
# OR:
# # cp -pi /path/to/fstrim.cron /etc/cron.weekly/LOCAL-fstrim
#
# From there, cron(8) (or possibly anacron(8), if that is installed) will
# invoke the fstrim(8) program to attempt to discard (or "trim") blocks which
# are not used by the filesystem. Such an operation is useful for solid state
# drives (SSDs), which are likely configured at the file system level to /not/
# issue discard/TRIM commands to the underlying block device when block are
# freed.
#
# On Debian-based systems, the 'fstrim' command is provided by the
# 'util-linux' package.
#
# See also:
#
# * ext4(5) -- 'discard' mount option
#
# * /etc/lvm/lvm.conf -- 'devices.issue_discards' option
#
# * cryptsetup(5) -- 'discard' option for /etc/crypttab entries
#
# * wikipedia page: "Trim (computing)"
# https://en.wikipedia.org/wiki/Trim_(computing)
#declare -r prog='fstrim.cron'
declare -r fstrim_prog='/sbin/fstrim'
# This will attempt to issue discard/TRIM commands for all blocks which are
# not used by the currently mounted file systems.
#
"${fstrim_prog}" --all "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment