Install Linux 6.2 (and 6.3) on Google Cloud Compute Engine instance
Tags: googlecloud, linux, cloud
Published: 2023-02-20 15:36:37

Update : Use the same trick to install Version 6.3 !
[6.2 is here, here is what's new](https://www.omgubuntu.co.uk/2023/02/linux-kernel-6-2-new-features), but we will not focus on that, but how to try it on Google Cloud !

**Be careful, this is an [unsigned kernel, The system will fail to boot in a secure boot environment](https://lukasmestan.com/your-kernels-are-unsigned-this-system-will-fail-to-boot-in-a-secure-boot-environment/) !**

Before we start, I need you to do two important steps, so in case something goes wrong, you can always go back :

1- You need ton [install the Ops Agent](https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent/installation) so you can see [what happens inside your instance](https://www.linkedin.com/pulse/sre-concepts-part-7-whiteblack-box-monitoring-marcel-koert/),
2- Create a backup or make a snapshot and create a disk from this snapshot.

Create a snapshot,
![snapshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndnf1bn4t9s1ui5ay78g.png)

Covert it to disk to be attached to the instance
![convert](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu3uq2q4alpgzh5nb0rk.png)

Now that you took care of the backup plan, let's dive in !

Here is what we do have with a default Ubuntu 18.04 installation (there is also 20.04 and 22.04 as LTS, but here, we want just to make a small demo) :

![kernel 0](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4k1cpvv36o3u7q6e4bco.png)

We need to add mainline package to our repos list so we can install it using `apt`

`sudo add-apt-repository ppa:cappelikan/ppa`

Then update the new repos by 

`sudo apt update`

then we just install it using 

`sudo apt install -y mainline`

To get all available kernels, we just make

`mainline --list`


![mainline list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3t7hgkztd5h14qm1zvx.png)

Then, we choose what to install, by explicitly giving the version, 

`sudo mainline --install 6.2.0`

or, if we want to get the latest stable kernel, we simply :

`sudo mainline --install-latest`

Restart, and voilà !

![voila](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/co9v67ey0pne8vlazhkh.png)

...

But here is an issue, how do we go back to old kernel ?

The problem here, is we are accessing using SSH, and kernel selection is done from [Grub Menu](https://help.ubuntu.com/community/Grub2/), and SSH is not available at this stage, which means we need another way, here is how !

Here is what we have now as installed kernels, we can use one of this commands :

`dpkg --list | grep -i linux-image`

`apt list --installed | grep -i linux-image`

![installed kernels](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f3smt0x3qn4bgqreihw.png)

Now, let's change something in default grub loading, here is the trick, but before, we need to understand how Grub works :

1. 
Grub menu on a machine that we have physical access can be tuned to show a menu at restart, so we generally have two options (*menu*) : the default kernel, and advanced mode that have a *submenu*, and they are assigned by indexes starting with `0` :
1- first menu entry = 0
2- second menu entry = 1
and so on ...
and each *menu* has a *submenu* with index numbers, but to not complicate this, we will *flatten* this *submenus* to make it easier

2. Grub saves its file to `/boot/grub/grub.cfg` , but this file should never be modified manually, as it's a result of some scripts (that we will know shortly), so eachtime for example we do a kernel update, it will be erased a new one will be generated, so we need to modify this script that generates the file, and not the file itself :


![balak!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u1n3629ha7zavbrrde6i.png)

3. The files that we need to modify for our case is `/etc/default/grub`

![grub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zbui8stuln8tr3h90zct.png)

And as you can see, the first option is `0` which means take the first entry in the menu and validate it, we will change that, but first we need to :

- flatten the menu to avoid submenus
- get the index of the kernel version we need

Here is how it was with default values (with submenus):

![submenu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flvrclnj3mkxe7sbkwqa.png)

Let's first add this entry to disable submenus :

`GRUB_DISABLE_SUBMENU=y`

![disable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qbvt04m3ro60ykdtbre.png)

Then let's validate the config by running the script to re-generate the `/boot/grub/grub.cfg` by running :

`sudo update-grub`

And it's flat now, no more submenus !

![flat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i305w8c0xs8xcklnzedv.png)

Now we need to understand the origin of other variables from other files, so when we do `update-grub` there is also a bunch of scripts that runs from `/etc/grub.d/`

![grub.d](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qagsohuyzkq35d28c7r7.png)

On Google Cloud, there is another file, which is :

`/etc/default/grub.d/50-cloudimg-settings.cfg`

![hidden](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zqy943q3vxo46er6xv9v.png)

So let's change this value, it will override the global one ...

But to replace it with what ?

Well, let's enumerate the menus we got starting with zero
![enum](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vou9pn0zw88pdfnnbjm1.png)

So now that we know that we need entry `2` let's replace it :

![bzzz](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn44dqw3xnwt4og61yn9.png)

Restart, and voilà again !

![voilo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3iibjzi7w5jcjtarha2.png)