Move snap packages to another location/directory

Change snaps installation directory

This is a common issue that many ask for. It seems snap developers are not intending to solve it soon, so some solutions were suggested.

The first solution that was strongly refused by the community,but I didn't try, is to use symbolic links to link the directories that lead to snaps. This method seems not to work as the community replies says that apparmom does not work with symbolic links.

The Second solution is mount --bind the directory /val/lib/snapd/snaps which works fine for me with the already installed applications but failed to install new applications because I was moving the directory to another partition. That gave me an error about hard linking the snap application with the cache directory which is located in /var/lib/snapd/cache. I don't know if this solution may work if the location are in the same partition or not; but this is the reason why I am moving the snap directory to free some space from partition to another one.

Third and working option is to move the /var/lib/snapd directory as a whole then mount --bind to it from another location and that worked for me and here is the steps.

Just a small note, you already know, after the rsync is done in the below steps, you can backup the data inside /var/lib/snapd to another location until the whole process is successfully done then you can remove the backup data if you want to free more space but keep /var/lib/snapd directory itself even free, as it is used by the mount point.

##############################################################################
# Take Care this section may break the System !!!
##############################################################################
##Move snap folder to Home instead of root.
#Create the directory : you can change the location
mkdir -p /home/$USER/snap/snapd

#Copy the data
sudo rsync -avzP /var/lib/snapd/  /home/$USER/snap/snapd/

#Do backups
sudo mv /var/lib/snapd /var/lib/snapd.bak
sudo cp /etc/fstab /etc/fstab.bak

#Change fstab (Change $USER with your name or change the path totally)
echo "/home/$USER/snap/snapd /var/lib/snapd none bind 0 0" | sudo tee -a /etc/fstab

#remount fstab Or reboot.
sudo mkdir /var/lib/snapd
sudo mount -a

if ls  /var/lib/snapd/ | grep snaps
then
    echo "Re-mounting snapd folder is done successfully. !!!!"
    sudo rm -rf /var/lib/snapd.bak
else
    echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "
    echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "
    echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "
    echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "
    echo "WARNING : Re-mounting snapd folder failed, please revert !!!!! "

    # Trying to revert automatically
    sudo cp /etc/fstab.bak /etc/fstab

    sudo mount -a
    sudo umount /var/lib/snapd

    sudo mv /var/lib/snapd.bak /var/lib/snapd

    echo "Files located at ~/snap/snapd should be removed, but are kept for
    recovery until you, manually reboot the system and make sure the service
    is running correctly. Then you can manually remove the folder ~/snap/snapd
    !!!!!!!!!!!!!!, you should do that manually."

fi
##############################################################################
# Take care the previous section may break the System !!!
##############################################################################

References

  1. Custom disk location for certain snaps issue
  2. ask ubuntu Question (Not best solution) Link
  3. Suggestion to use bind mount instead of symbolic links in moving snap directories Link
  4. Differences between bind mount and symbolic links Link
  5. Make bind mount permenant

I finally found a way.

  • First move the snap package to desired location. Let snap_app.snap as the app you want to move. Move the package from /var/lib/snapd/snaps/snap_app.snap to ~/snaps/snap_app.snap.
  • Then you have to change mount option in /etc/systemd/system/snap-snap_app-90.mount,number in the file name may be vary. It has directive What=/snap_app_loaction under [Mount]. Change it to your new snap package location.
  • Finally you have to symlink the snap package to /var/lib/snapd/snaps/.
    Ex: ln -s ~/snaps/snap_app.snap /var/lib/snapd/snaps/snap_app.snap

Snap package is originally mount in to /snap/snap_app directory. you can check it with mount. Reboot your machine and you are all done.

Tags:

18.04

Snap