Tag Archives: loopback

Dropbox on Linux with encrypted home directory

Dropbox dropped support for all files systems except ext4 on Linux. This is a problem for everybody using an encrypted home directory since those are usually mounted via ecryptfs or similar. The following workaround makes use of an ext4 formatted file system image that is mounted via a loopback device upon login.

Step 1: create a mount point and make it read-only if not mounted

mkdir remote/Dropbox
sudo chattr +i remote/Dropbox

Change the “remote” part of the paths if you prefer to place your Dropbox folder at a different location.

Step 2: create an image file and format it

Change 10G to something that corresponds to the space that you need in order to sync your Dropbox files:

truncate -s 10G remote/.Dropbox.ext4

Alternative way of creating the image using dd:

dd if=/dev/zero of=remote/.Dropbox.ext4 count=10240 bs=1048576

Format the image with ext4:

mkfs.ext4 -F remote/.Dropbox.ext4

Step 3: create an entry in /etc/fstab

/home/<username>/remote/.Dropbox.ext4 /home/<username>/remote/Dropbox ext4 user,noauto,rw,loop,x-gvfs-hide 0 0

We use the parameter x-gvfs-hide to avoid showing the folder in sidebar of the file manager.

Step 4: use a systemd user service to mount the Dropbox image upon user login

Create the file .config/systemd/user/mountdropbox.service with the following content:

[Unit]
Description=Mounts a Dropbox ext4 image in the home directory of a user
After=home-username.mount
Requires=home-username.mount

[Service]
ExecStart=/bin/mount %h/remote/Dropbox
ExecStop=/bin/umount %h/remote/Dropbox
RemainAfterExit=yes

[Install]
WantedBy=default.target

Now lets enable and start the service. Slashes must be replaced by dashes if used as systemd parameters.

systemctl --user enable mountdropbox.service
systemctl --user start mountdropbox.service

After executing the commands above the service will be started (i.e., the Dropbox folder will be mounted) automatically upon user login.

Note for Ubuntu 18.04 or newer: for some reason systemd seems to “forget” the user service after reboot. Please check the comments of this blog post for a solution to this.

We also make sure that we own the mounted folder:

sudo chown <username>:<username> remote/Dropbox

Step 5: tell Dropbox to move your files to the new location

You can use the Dropbox GUI to move the Dropbox folder location to the newly created one.

The way how Dropbox detects the file system in use is a bit awkward as it does not let you choose the mount point itself as Dropbox folder location, instead you need to sync to a subfolder. So if you mount point is remote/Dropbox you need to sync to remote/Dropbox/<username> or something like that as Dropbox does not believe that the mount point is an ext4 file system.

Optional: grow the Dropbox image if you need more space

On the command line run the following commands (change 15G to something appropriate for your needs, but never change it to a lower size than your existing image as this cuts of the image which may lead to data loss):

dropbox stop
systemctl --user stop mountdropbox.service
truncate -s 15G remote/.Dropbox.ext4
fsck.ext4 -f remote/.Dropbox.ext4
resize2fs remote/.Dropbox.ext4
systemctl --user start mountdropbox.service
dropbox start