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

Discussion in 'Proxmox VE: Installation and configuration' started by menathor, May 18, 2019.

  1. menathor

    menathor New Member

    Joined:
    May 5, 2019
    Messages:
    11
    Likes Received:
    0
    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!
     
  2. dietmar

    dietmar Proxmox Staff Member
    Staff Member

    Joined:
    Apr 28, 2005
    Messages:
    16,450
    Likes Received:
    307
    See "man pct"

    # pct fstrim <vmid>
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. menathor

    menathor New Member

    Joined:
    May 5, 2019
    Messages:
    11
    Likes Received:
    0
    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
    
    
    
     
  4. menathor

    menathor New Member

    Joined:
    May 5, 2019
    Messages:
    11
    Likes Received:
    0
    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
    
     
    #4 menathor, May 18, 2019
    Last edited: May 18, 2019
  5. LnxBil

    LnxBil Well-Known Member

    Joined:
    Feb 21, 2015
    Messages:
    3,696
    Likes Received:
    331
    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
     
  6. mir

    mir Well-Known Member
    Proxmox Subscriber

    Joined:
    Apr 14, 2012
    Messages:
    3,481
    Likes Received:
    96
    Except if you specify command interpreter on the command line like:
    perl ./test
    bash some_bash_script
     
  7. LnxBil

    LnxBil Well-Known Member

    Joined:
    Feb 21, 2015
    Messages:
    3,696
    Likes Received:
    331
    Yes, sure, but shebang is also ignored in that case :-D
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice