Use Google Drive as a local directory on Linux
Tags: ocaml, google, systemd
Published: 2021-02-18 03:49:30

---
title: Use Google Drive as a local directory on Linux
published: true
description: 
tags: ocaml,google,systemd
cover_image: https://user-images.githubusercontent.com/6997/108302470-0b04b380-7172-11eb-8b94-a981dbaf4581.jpg
---
<span>Photo by <a href="https://unsplash.com/@instagramfotografin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Peggy Anke</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span>

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](https://support.google.com/drive/answer/2374987) 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](/yawaramin/ocaml-for-business-1dol), I'll be talking about that here.

The tool in question is named [google-drive-ocamlfuse](https://astrada.github.io/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](https://github.com/astrada/google-drive-ocamlfuse/wiki/Installation) for details. However, it is not distributed for the OS I use, Fedora, so I will walk through the installation process using [opam](https://opam.ocaml.org/), 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](https://opam.ocaml.org/doc/Install.html) to set it up. Important--also need to actually [initialize](https://opam.ocaml.org/doc/Usage.html#opam-init) opam before it actually starts working, and [switch to a recent OCaml version](https://opam.ocaml.org/doc/Usage.html#opam-switch) 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

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

(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

Then, authorize it with Google:

    google-drive-ocamlfuse

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.

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

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
```

(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](https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/#cutthecraphowdoimakesurethatmyservicestartsafterthenetworkisreallyonline) 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

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
```

And the filesystem:

```
$ df -h
...
google-drive-ocamlfuse   15G  1.6G   14G  11% /home/yawar/GoogleDrive
```

And the files themselves:

    $ ls ~/GoogleDrive

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