fstrim doesn't work in containers (any OS) - workarounds?

menathor

New Member
May 5, 2019
11
0
1
29
Hi again

I've been trying all day to get fstrim working inside unprivileged LXC containers with no luck. Initially I thought it was just a Debian/Ubuntu issue, but I've tried multiple OS templates and it always results in: FITRIM ioctl failed: Operation not permitted. FYI it works fine in privileged containers, but as people have pointed out they're not as secure.

Can anyone help me get this working, or tell me if there's a workaround? I've switched to lvm-thin provisioning so I need to trim container disks to free up deleted space.

Or is it just not supported in unprivileged containers at the moment? I found a similar issue at linuxcontainers.org. One of the maintainers said:

"Unfortunately I expect it’s simply the kernel refusing an unprivileged user requesting a TRIM operation on a block device.

Unless you’re using a privileged container, I don’t expect you’ll get away from that…

One option would be to have root on the host run fstrim against all /dev/rbd* devices, effectively running TRIM against all running containers.

Otherwise, you’d need to wait until we have a way to catch such system calls in userspace (there’s ongoing kernel work to allow that), at which point we could have LXD catch that particular ioctl and replay it as real root."



If doing it inside containers won't work, is a way of triming disk images externally from the host?

Thanks!
 

menathor

New Member
May 5, 2019
11
0
1
29
From what I can tell, it can only be done from the host at this time... The script I'm using is below. Tested and it works. I'm running it as a weekly cronjob

Thanks to http://wiki.csnu.org/index.php/Proxmox_:_TRIM_sous_LXC and https://forum.proxmox.com/threads/lxc-lvm-thin-discard-fstrim.34393/#post-168569 for the scripts.

Mods, could we get this added to the lvmthin documentation page?


Code:
### Trim Proxmox host and all unprivileged LXCs on node ###

#!/bin/bash

### Path to FSTRIM
FSTRIM=/sbin/fstrim

### Static list of volumes to trim
# Proxmox host
TRIMVOLS="/"

### Trim volumes
# Trim static list
for i in $TRIMVOLS; do
  echo "Trimming $i"
  $FSTRIM -v $i 2>&1 | logger -t "fstrim [$$]"
done

# Trim all LXC containers
for i in $(pct list | awk '/^[0-9]/ {print $1}'); do
  echo "Trimming CT $i"
  $FSTRIM -v /proc/$(lxc-info -n $i -p | awk '{print $2}')/root 2>&1 | logger -t "fstrim [$$]"
done
 

menathor

New Member
May 5, 2019
11
0
1
29
Thanks @dietmar ! You posted while I was in the middle of my reply so I only just saw this :) Makes things a lot eaiser. Here's a tidied up script using PCT in case anyone finds it useful

Code:
### Trim Proxmox host volumes and all LXCs on node ###
# Run as weekly cron job
# !/bin/bash

# Script colors
green=`tput setaf 2`
reset=`tput sgr0`

# Path to FSTRIM
FSTRIM=/sbin/fstrim

# List of host volumes to trim separated by spaces
# eg TRIMVOLS="/mnt/a /mnt/b /mnt/c"
TRIMVOLS="/"

## Trim all LXC containers ##
echo "${green}LXC CONTAINERS${reset}"
for i in $(pct list | awk '/^[0-9]/ {print $1}'); do
  echo "Trimming Container $i"
  pct fstrim $i 2>&1 | logger -t "pct fstrim [$$]"
done
echo ""

## Trim host volumes ##
echo "${green}HOST VOLUMES${reset}"
for i in $TRIMVOLS; do
  echo "Trimming $i"
  $FSTRIM -v $i 2>&1 | logger -t "fstrim [$$]"
done
 
Last edited:

LnxBil

Well-Known Member
Feb 21, 2015
4,217
408
83
Germany
The shebang in the third line is ignored. Shebang has to go in the first line.

Here is an example that it is not working:

Code:
root@proxmox ~ > ls -l test
-rwxr-xr-x 1 root root 104 Mai 19 09:14 test
root@proxmox ~ > cat test
#not working perl
#!/usr/bin/perl

use strict;
use warnings;

my $test = "Hello World\n";
print $test;

1;

root@proxmox ~ > ./test
./test: line 4: use: command not found
./test: line 5: use: command not found
./test: line 7: my: command not found
./test: line 10: 1: command not found
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE and Proxmox Mail Gateway. We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get your own in 60 seconds.

Buy now!