⬆️ ⬇️

GRUB2 and Xen kernels

Yesterday I wrote a little obzorchik of the new loader GRUB 2. And one unpleasant thing caught my attention. After all, if nothing works, you don’t pay attention to it. So, I decided to try Xen-based virtualization. She demanded to install a modified kernel. Of course, the developers have not yet thought about such subtleties, so the newly added menu item did not work. Obviously, we have two choices. We need either a script that correctly finds the kernel and correctly composes the menu item, or we have to manually enter the menu item with handles in / boot / grub/grub.cfg.

Naturally, I didn’t really understand what the right menu item should look like, so I went to Google. He prompted me to note “xen with grub2 on debian etch” in a blog of a simple Australian guy named Daniel Mateos. The article helped me and everything would be fine if Daniel didn’t take down his blog :) More precisely, he was tired of Wordpress, and he, with his hand somewhere out of place, created a new site on django. That's why I decided to write what was considered in his article here. For greater safety.



GRUB2 attracted Australian admins supporting GPT / EFI partitions, with which you can use disks larger than 2TB (they needed 5TB). Like any person who has run a gossip on the topic of documentation on GRUB2, the guys are very upset. She is not. Therefore, they hoped that the following will benefit you.



0. Prepare GRUB



Open and edit /etc/kernel-img.conf, bringing it to this form:

do_symlinks = yes

relative_links = yes

do_bootloader = no

do_bootfloppy = no

do_initrd = yes

link_in_boot = no

postinst_hook = update-grub

postrm_hook = update-grub


All lines are intuitive. The config defines how the system will behave when installing a new kernel from a deb package. More details in man kernel-img.conf



Next steps:

# mount /dev/sda1 /boot

# apt-get install grub-pc

# grub-install /dev/sda

# apt-get install linux-image-2.6.18-5-amd64




Line by line decoding:

1. Mount the necessary section in the appropriate directory. It is absolutely not necessary.

2. Install, in fact, GRUB 2. It is strange if you are not worth it.

3. Register the bootloader in the boot sector of the hard disk. This may take some time.

4. Install the kernel. After this, GRUB will add it to the menu itself. The kernel is normal, not Xen-ovsky, so problems should not arise.

')

1. Installing Xen Items



# apt-get install xen-hypervisor-3.2-1-amd64

# apt-get install xen-docs-3.2 xen-utils-3.2-1 xen-tools

# apt-get install libc6-xen

# apt-get install linux-image-2.6.18-5-xen-amd64

# apt-get install xen-linux-system-2.6.18-5-xen-amd64

# apt-get install xen-linux-system-2.6.18-5-xen-vserver-amd64




I note that the author installs a specific kernel version (2.6.18-5) for a specific architecture (amd64). No need to stupidly copy-paste these commands. Adjust them according to your system and preferences.



2.1. Manually adding entries to the menu



Just to remind you: this is not the right way to edit the menu. According to the rules, we need to write a script that would make such entries for Xen automatically. But if time is more precious to you than system reliability, you can do that. Everything will work until the next update of the bootloader.

Add a similar entry to the end of grub.cfg:



menuentry “Xen 3.2″ {

multiboot (hd0,1)/xen-3.2-1-amd64.gz dom0_mem=256M

module (hd0,1)/vmlinuz-2.6.18-5-xen-amd64 root=/dev/sda2 ro

module (hd0,1)/initrd.img-2.6.18-5-xen-amd64

}




Now, after rebooting, you should be in the dom0 kernel running inside the Xen hypervisor.



2.2 Script to solve the problem correctly



This script is provided in a comment to a post by a man named Roger Smith. Of course, he needs to be improved, but the basis has been laid and from here one can understand the logic of the script's actions.



#!/bin/bash

set -e



kernels=`ls /boot/vmlinuz-*-xen-*|sed s/”.*vmlinuz-”//` 2>/dev/null

initrds=`ls /boot/vmlinuz-*-xen-*|sed s/”.*initrd.img-”//` 2>/dev/null

xenimgs=`ls /boot/xen-*|sed s/”.*xen-”//|sed s/”.gz”//` 2>/dev/null

MODULE=”}”

for i in $xenimgs; do

for j in $kernels; do

echo “Found xen kernel and image: $j –> $i” >&2

echo “$initrds” | grep $j > /dev/null 2>&1

if [ $? -eq 0 ]; then

MODULE=”module ${GRUB_DRIVE_BOOT}/initrd.img-$j

}”

else

MODULE=”}”

fi



cat << EOF

menuentry “Xen ${i} kernel $j” {

multiboot ${GRUB_DRIVE_BOOT}/xen-$i.gz dom0_mem=256M

module ${GRUB_DRIVE_BOOT}/vmlinuz-$j root=/dev/sda2 ro console=tty0

${MODULE}

EOF

echo “”

done

done




Necessary actions:

1. Create a file for the script

#touch /etc/grub.d/10_xen



2. Set access rights to it:

#chmod 755 /etc/grub.d/10_xen



3. Add the above script to /etc/grub.d/10_xen



That's all. Good luck!

Source: https://habr.com/ru/post/56260/



All Articles