DEV Community

Cover image for Use Google Drive as a local directory on Linux
Yawar Amin
Yawar Amin

Posted on

Use Google Drive as a local directory on Linux

Photo by Peggy Anke on Unsplash

HAVE you ever wanted to use your Google Drive like a ... well, a normal folder? If you're on Windows or Mac, you're in luck because Google offers a free desktop utility for exactly that purpose.

If you're on Linux however, there is no official utility from Google. There are a couple of paid or freemium options which could do the job, but there's also an open source option, written in OCaml, and since I like OCaml, I'll be talking about that here.

The tool in question is named google-drive-ocamlfuse and it is a single command that, when run, 'mounts' (attaches) your Google Drive as a filesystem (effectively, a directory on your computer).

Google-drive-ocamlfuse is distributed in operating system packages for Ubuntu and a couple of other Linuxes--check the wiki page for details. However, it is not distributed for the OS I use, Fedora, so I will walk through the installation process using opam, the operating system-agnostic OCaml package manager.

The token proxy

But, before we proceed, you need to be aware of something--google-drive-ocamlfuse (let's call it gdfuse for short) uses a proxy ('gaeproxy') deployed on Google App Engine to request access tokens from Google on your behalf. You can check the code here: https://github.com/astrada/google-drive-ocamlfuse/blob/3bbc4d054fbd7a7cdef36c17d88a5316183fd403/src/gaeProxy.ml#L6

It's up to you whether you feel comfortable with this proxy acting on your behalf. In my opinion it's similar to other third-party applications that you grant access to carry out some functions for you. If you decide to revoke the access later, you can do so here: https://myaccount.google.com/permissions?hl=en&pli=1

Install

Having said that, if you decide to go ahead, here's how to install it. First, you'll need the opam package manager, as mentioned above. If you don't already have it, follow the installation instructions to set it up. Important--also need to actually initialize opam before it actually starts working, and switch to a recent OCaml version so that you're reasonably up-to-date.

Once that's done, run the following to get a list of non-OCaml software dependencies needed for gdfuse to work:

opam install depext
opam depext google-drive-ocamlfuse
Enter fullscreen mode Exit fullscreen mode

This will actually offer to run yum install ... and install the packages immediately, however I declined because I prefer to use dnf, Fedora's new package manager that replaces yum:

sudo dnf install fuse-devel libcurl-devel pkgconfig sqlite-devel
Enter fullscreen mode Exit fullscreen mode

(Depext may actually give you a slightly different list than the one I have here, so take care to install the exact packages for your system.)

Then, you can install gdfuse itself:

opam install google-drive-ocamlfuse
Enter fullscreen mode Exit fullscreen mode

Then, authorize it with Google:

google-drive-ocamlfuse
Enter fullscreen mode Exit fullscreen mode

This step launches the authorization page in your browser and prompts you through the process. Once done, gdfuse will report that it authorized:

$ google-drive-ocamlfuse
Access token retrieved correctly.
Enter fullscreen mode Exit fullscreen mode

Now, we need to set up gdfuse so that it automatically mounts Google Drive on system boot and unmounts it on shutdown.

Automated mount/unmount

To set up the automation, we will use systemd, the standard Linux service manager. The trick is to think of gdfuse as a regular Linux service. It will start up on system boot, at which point it will mount the Google Drive; and it will shut down on system shutdown, at which point it will unmount the Drive.

First, we need to create the directory which will serve as the mount point. In other words, the actual directory where all the Google Drive files will appear. I chose:

mkdir ~/GoogleDrive
Enter fullscreen mode Exit fullscreen mode

Next, set up a systemd service definition file for gdfuse with the correct mount/unmount commands:

# ~/.config/systemd/user/gdfuse.service
[Unit]
Description=google-drive-ocamlfuse - mount Google Drive as a filesystem
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/home/YOURNAME/.opam/default/bin/google-drive-ocamlfuse -f /home/YOURNAME/GoogleDrive
ExecStop=/usr/bin/fusermount -u /home/YOURNAME/GoogleDrive

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

(Be sure to replace YOURNAME with your username.)

A few noteworthy points:

  • The After= and Wants= properties are used to ensure that gdfuse only starts up after the network is up. Check the documentation for more details on that.
  • The ExecStart= property makes sure to start gdfuse in foreground mode (with the -f flag), so that systemd can take control of the process.
  • The ExecStop= property ensures the Drive is unmounted on system shutdown.
  • The WantedBy= property ensures this service will actually get started on system boot.

Now, the standard systemd service installation and startup:

systemctl enable --now --user gdfuse
Enter fullscreen mode Exit fullscreen mode

And voila, the service should be started and Google Drive mounted as a filesystem. You can check the service status:

$ systemctl status --user gdfuse
● gdfuse.service - google-drive-ocamlfuse - mount Google Drive as a filesystem
     Loaded: loaded (/home/yawar/.config/systemd/user/gdfuse.service; enabled; vendor preset: disabled)
     Active: active (running) since Wed 2021-02-17 21:13:37 EST; 1h 21min ago
   Main PID: 478354 (google-drive-oc)
      Tasks: 3 (limit: 18878)
     Memory: 14.4M
        CPU: 2.703s
     CGroup: /user.slice/user-1000.slice/user@1000.service/gdfuse.service
             └─478354 /home/yawar/.opam/default/bin/google-drive-ocamlfuse -f /home/yawar/GoogleDrive
Enter fullscreen mode Exit fullscreen mode

And the filesystem:

$ df -h
...
google-drive-ocamlfuse   15G  1.6G   14G  11% /home/yawar/GoogleDrive
Enter fullscreen mode Exit fullscreen mode

And the files themselves:

$ ls ~/GoogleDrive
Enter fullscreen mode Exit fullscreen mode

Now, your Google Drive will be synchronized with any file changes you make in ~/GoogleDrive!

Top comments (3)

Collapse
 
zin profile image
Zin

thank you for this post very helpful, i followed all your commands and instrunctions and it worked but when i restart my fedora, the folder is empty and i should run "systemctl enable --now --user gdfuse" to mount it everytime i power on my pc, i'm missing something to make the mounting auto

Collapse
 
yawaramin profile image
Yawar Amin

Can you try this? superuser.com/a/1028180/111307

Collapse
 
jvph profile image
JVPH

Hmm thunar file manager appears to experience a significant slowdown during its startup when using this method.