×

Run Fedora on Google Compute Engine

Linux Fedora image on Google Compute Engine is a unique image not readily available in the list of OS images or in the marketplace. The above solution integrating Fedora into GCE will suffice application requirement running on Linux Fedora OS.

Run Fedora on Google Compute Engine

Unlike AWS, there are no Fedora images available in Google Compute Engine(GCE). Sometimes it may be required because of application dependencies. We can use CentOS 7 to build a customized Fedora OS image in GCE as it is the closest image to Fedora. Later on in this documentation, steps to do the same has been described.



Get Fedora Cloud Image for Customization

First, we need to create a Compute engine instance where we’re going to build and modify the official Fedora Cloud image. So, go to Create an instance page and :


  1. Give instance a name and choose the right zone.
    In “Machine Type”, preferred to choose the “f1-micro” as it will be sufficient to fulfill our requirement.
    In “Boot Disk”, click “Change” and choose “CentOS 7”.
    In “Identity and API access”, choose “Allow all access to Cloud APIs”.
    Allow HTTP and HTTPS traffic.

          



  1. Click on “Create” button.

  2. In the Google Cloud Console page, click on SSH button of your instance.

  3. Change your user to root using  : $ sudo -i

  4. Here we need is to install wget using the following command :

    # yum install wget

  5. Once installed, go to https://alt.fedoraproject.org/cloud/ and right click on “Download” beside “Cloud Base compressed raw image” and copy link address.







  1. Now, go back to the VM instance, and do the following:

    # wget "{PASTE LINK ADDRESS}"

    This will download the Fedora raw image.


  1. After completion of download, run:

    # xz --decompress --keep "Fedora-Cloud-Base-XX-X.X.x86_64.raw.xz"

    Note that we have to change the filename depending on the version we have downloaded.

Preparing Fedora for the Google Cloud Platform’s ride

  1. # mkdir boot
    #sudo mount -o loop,offset=1048576 "$PWD/Fedora-Cloud-Base-XX-X.X.x86_64.raw"
    "$PWD/boot"

    Here, again filename need to be changed.
    In this case,






There’s an offset value because this raw disk is a disk, not a partition. Linux will mount the first partition of the raw disk stored in the file by setting the offset value.


  1. Next:

    # cd boot
    # mount --bind /dev dev && mount --bind /sys sys && mount --bind /proc proc && mount --bind /etc/resolv.conf etc/resolv.conf
    # chroot ./ /usr/bin/bash


  2. Now, we enter into the Fedora loop-mounted raw chroot.
    Next, Google Cloud Platform software need to be installed to make it work well.
    The first thing we need to do is up-to-date the image.

    # dnf upgrade --assumeyes --nogpgcheck "*"


During the execution of this command if the process gets killed, execute this command again. This process will take some time to complete.



  1. Then, do this:


# cat > "/etc/yum.repos.d/google-cloud.repo" <<"EOR"
[google-cloud-compute]
name=Google Cloud Compute
baseurl=https://packages.cloud.google.com/yum/repos/google-cloud-compute-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg       
   https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOR




            

  1. Next, to avoid dnf dying with a message about being unable to install libjson-c.so.2, we’re going to patch the package to change the requirement to libjson-c.so.4, and then lock the package to prevent future upgrades breaking it.

    #dnf install python-google-compute-engine
    #dnf install rpmrebuild 'dnf-command(versionlock)'
    #dnf download google-compute-engine-oslogin

#rpmrebuild -ep *google-compute-engine-oslogin-*.el7.x86_64.rpm






After saving this file, a question will appear- “Do you want to continue? (y/N)”. Press “y”. It will return a result a path. Copy that path which will be required in the next command.

# dnf install “{PASTE THE PATH}”

In this case,





dnf versionlock add google-compute-engine-oslogin
# dnf install google-compute-engine
# ln -s /usr/lib64/libjson-c.so.4 /usr/lib64/libjson-c.so.2


  1. Next,

    # touch "/.autorelabel"
    # dnf clean all


  2. Now time to exit chroot:

    # exit
    $ cd ../


  1. Now you got out of the loop-mounted directory, you can unmount bind-mounted things:

    #umount boot/dev boot/proc boot/sys boot/etc/resolv.conf

    In this step if you get an error saying “target is busy”, use following command.

    #umount -l /PATH/OF/BUSY-DEVICE


  2. And then do:

    #fstrim --verbose boot


  3. Now unmount the loop-mounted device:

    #umount boot

    Here again, if you get an error saying “target is busy”, use

    # umount -l /PATH/OF/BUSY-DEVICE

    # mv "Fedora-Cloud-Base-XX-X.X.x86_64.raw" "disk.raw"
    #tar --create --auto-compress --file="Fedora-Cloud-Base-XX-X.X.x86_64.tar.gz" --sparse disk.raw

    Now we have our final image ready.



  1. Go to Google Cloud Storage and create a bucket of “Regional” type with the same location as the compute engine instance. (If you have an existing bucket no need to create a new one, existing one will work perfectly).


  1. Wait for the bucket to be created, and once done, go in SSH window again and do:

    #gsutil cp "Fedora-Cloud-Base-XX-X.X.x86_64.tar.gz" "gs://[name of the bucket]/"

    This copies the packaged image to Google Cloud Storage.
    Now, you can shut down the instance at that point. Don’t delete it yet as we’ll test the Fedora instance before deleting this build VM.
    Now in Google Compute Engine, get inside “Images”. Hit the “Create Image” button. Configure it like this:

    1. Name it “fedora-cloud-@@” where @@ is the version.

    2. In “Source”, choose “Cloud Storage file”.

    3. Click on the “Browse” button, get in your bucket, and select the .tar.gz file uploaded earlier.

    4. Create the image.

Testing phase

Now to see if it worked great, configure an instance with the created fedora image.
Don’t forget to set the boot disk size. It will be set to below 4 GB, way too small. The minimum size of Google Cloud Platform disks is 10 GB and the recommended minimum by Google is 200 GB. Click on the “Create” button.

Now, you’ve to wait for 5 minutes, enough time to clean up your keyboard! And after these 5 minutes, now you can click the “SSH” button.





Trendy