Google Drive

To integrate Google Drive with a Debian Linux instance running on Google Cloud Platform (GCP) use a tool called google-drive-ocamlfuse. This tool allows to mount a Google Drive as a filesystem on the Linux instance.

1. Install google-drive-ocamlfuse

Install required libraries for google-drive-ocamlfuse:

sudo apt-get install software-properties-common
sudo apt-get install python3-launchpadlib

Add the repository for google-drive-ocamlfuse:

sudo add-apt-repository ppa:alessandro-strada/ppa

The repository should contain the following 2 lines. Edit the repository:

sudo nano /etc/apt/sources.list.d/alessandro-strada-ubuntu-ppa-bookworm.list

If necessary, add the following 2 lines in that repository:

deb http://ppa.launchpad.net/alessandro-strada/ppa/ubuntu xenial main

deb-src http://ppa.launchpad.net/alessandro-strada/ppa/ubuntu xenial main

If necessary, download key from keyservers directly into the trusted set of keys (see Install ‘google-drive-ocamlfuse’ on Debian Stretch & Buster):

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys AD5F235DF639B041

Install google-drive-ocamlfuse:

sudo apt install google-drive-ocamlfuse

2. Authenticate google-drive-ocamlfuse with the Google Account

To authenticate google-drive-ocamlfuse without using a browser, use a headless authentication method provided by Google.

  1. Generate OAuth2 Credentials:

    • Go to the Google Cloud Console: https://console.cloud.google.com/.

    • Create a new project or select an existing one.

    • Navigate to the “APIs & Services” > “Credentials” section.

    • Click on “Create credentials” and select “OAuth client ID”.

    • Choose “Desktop app” as the application type.

    • Click “Create” and note down the generated Client ID and Client Secret.

  2. Run the Authentication Command:

    • Open a terminal on your VM.

    • Run the following command to start the authentication process:

      google-drive-ocamlfuse -headless -id <CLIENT_ID> -secret <CLIENT_SECRET>
      

      Replace <CLIENT_ID> and <CLIENT_SECRET> with the values obtained from the Google Cloud Console.

    • Example:

      google-drive-ocamlfuse -headless -id   701070352033-7j2jmeotq77mtkg60dsc9q6hris4pvj3.apps.googleusercontent.com  -secret   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
      
  3. Follow the Authentication Flow:

    • The command will provide a URL and a verification code.

    • Copy the URL and open it in a browser on your local machine.

    • Sign in with your Google account if prompted and grant permission to the application.

    • After granting permission, you’ll receive a verification code.

  4. Enter the Verification Code:

    • Copy the verification code provided by Google.

    • Paste the code into the terminal where the google-drive-ocamlfuse command is running.

    • Press Enter.

  5. Complete Authentication:

    • Once the verification code is entered, google-drive-ocamlfuse should complete the authentication process.

    • You should see a message indicating successful authentication.


3. Set Google Drive API

Set Google Drive API in the Google Cloud account for setting the API.


4. Set mounting directory

Create a directory where to mount the Google Drive and then mount it using google-drive-ocamlfuse:

cd /usr/local/logistics
sudo mkdir google-drive

Fix Permission issues if happened:

sudo chown $USER:$USER /usr/local/logistics/google-drive
sudo chmod 777 /usr/local/logistics/google-drive

5. Mount and umount Google Drive

Mount Google Drive:

google-drive-ocamlfuse /usr/local/logistics/google-drive

Unmount Google Drive:

fusermount -u /usr/local/logistics/google-drive

6. Mount automatically with crontab

  1. As a prerequisite allow option user_allow_other in fuse, so that read/write access works on Google Drive:

    sudo nano /etc/fuse.conf
    

    Set the following parameter in fuse.conf:

    # Allow non-root users to specify the allow_other or allow_root mount options.
    user_allow_other
    
  2. To mount Google Drive automatically on system boot, add a line to the crontab file:

    crontab -e
    

    Add the following line to the end of the file with the path to the mount point:

    # Mount google-drive with google-drive-ocamlfuse:
    @reboot /usr/bin/google-drive-ocamlfuse -label default -o allow_other -verbose /usr/local/logistics/google-drive
    

    Now, the Google Drive should be mounted automatically on system boot and persist through reboots.