Skip to content

Commit

Permalink
Added support for new GRUB configuration in >=1.4
Browse files Browse the repository at this point in the history
Added support for the new GRUB configuration style used in VyOS 1.4 and newer.
  • Loading branch information
zdc committed Mar 14, 2024
1 parent 8f2bc37 commit 425b808
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hyperv.yml
Expand Up @@ -21,7 +21,7 @@
- install-image
- mount-root-fs
- install-config
- install-grub
- install-grub-wrapper
- install-persistence-conf
- install-cloud-init-wrapper
- unmount-pre
Expand Down
2 changes: 1 addition & 1 deletion qemu.yml
Expand Up @@ -24,7 +24,7 @@
- install-image
- mount-root-fs
- install-config
- install-grub
- install-grub-wrapper
- install-persistence-conf
- install-cloud-init-wrapper
- install-guest-agent-wrapper
Expand Down
2 changes: 1 addition & 1 deletion raw.yml
Expand Up @@ -24,7 +24,7 @@
- install-image
- mount-root-fs
- install-config
- install-grub
- install-grub-wrapper
- install-persistence-conf
- install-cloud-init-wrapper
- install-guest-agent-wrapper
Expand Down
46 changes: 46 additions & 0 deletions roles/install-grub-v2/files/vyos_unattended_installer.py
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
#
# Copyright (C) 2023 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from importlib import util
from os import environ
from sys import exit

spec = util.spec_from_file_location(
"vinstall", "/usr/libexec/vyos/op_mode/image_installer.py"
)
vinstall = util.module_from_spec(spec)
spec.loader.exec_module(vinstall)

if __name__ == "__main__":
# read configuration variables
vyos_version = environ["vyos_version"]
console_type = "tty" if environ["console_type"] == "kvm" else "ttyS"
install_target = environ["install_target"]

# install GRUB configuration files
vinstall.setup_grub("/boot")
vinstall.grub.version_add(vyos_version, "/boot")
vinstall.grub.set_default(vyos_version, "/boot")
vinstall.grub.set_console_type(console_type, "/boot")

# install GRUB
vinstall.grub.install(install_target, "/boot/boot", "/boot/efi")

# sort inodes (to make GRUB read config files in alphabetical order)
vinstall.grub.sort_inodes(f"/boot/{vinstall.grub.GRUB_DIR_VYOS}")
vinstall.grub.sort_inodes(f"/boot/{vinstall.grub.GRUB_DIR_VYOS_VERS}")

exit()
55 changes: 55 additions & 0 deletions roles/install-grub-v2/tasks/main.yml
@@ -0,0 +1,55 @@
# It is necessary to mount and bind /dev, /proc, /sys and /boot in order to execute grub-install
# and install GRUB correctly within the {{ volume_drive }} using chroot

# XXX: ansible mount module requires fstype so it cannot be used for binding an already
# mounted location, we get to use mount directly at least for /boot
- name: Mount and bind /dev /proc /sys and {{ vyos_write_root }}/boot to {{ vyos_install_root }}
become: true
shell: mount --bind /dev {{ vyos_install_root }}/dev &&
mount --bind /proc {{ vyos_install_root }}/proc &&
mount --bind /sys {{ vyos_install_root }}/sys &&
mount --bind {{ vyos_write_root }} {{ vyos_install_root }}/boot

- name: Mount EFI
become: true
mount:
src: "{{ vyos_target_drive }}p{{ partition_num_efi }}"
path: "{{ vyos_install_root }}/boot/efi"
fstype: vfat
state: mounted
boot: no
when: partition_num_efi is defined

- name: Pause
pause:
prompt: "Continue?"

- name: Copy installer
become: true
copy:
src: "files/vyos_unattended_installer.py"
dest: "{{ vyos_install_root }}/tmp/vyos_unattended_installer.py"

- name: Install GRUB and configuration
become: true
command: chroot {{ vyos_install_root }} python3 /tmp/vyos_unattended_installer.py
environment:
vyos_version: "{{ vyos_version }}"
install_target: "{{ loop_device.stdout }}"
console_type: "{{ grub_console }}"

- name: Remove installer
become: true
file:
path: "{{ vyos_install_root }}/tmp/vyos_unattended_installer.py"
state: absent

- name: Unmount EFI
become: true
mount:
src: "{{ vyos_target_drive }}p{{ partition_num_efi }}"
path: "{{ vyos_install_root }}/boot/efi"
fstype: vfat
state: absent
boot: no
when: partition_num_efi is defined
2 changes: 2 additions & 0 deletions roles/install-grub-v2/tests/inventory
@@ -0,0 +1,2 @@
localhost

4 changes: 4 additions & 0 deletions roles/install-grub-v2/tests/test.yml
@@ -0,0 +1,4 @@
---
- hosts: localhost
roles:
- install-grub-v2
9 changes: 9 additions & 0 deletions roles/install-grub-wrapper/tasks/main.yml
@@ -0,0 +1,9 @@
- name: Select GRUB for installer for VyOS <=1.3
include_role:
name: install-grub
when: vyos_version is regex("^1\.[2-3].*$")

- name: Select GRUB for installer for VyOS >=1.4
include_role:
name: install-grub-v2
when: vyos_version is regex("^1\.[4-9].*$")
2 changes: 2 additions & 0 deletions roles/install-grub-wrapper/tests/inventory
@@ -0,0 +1,2 @@
localhost

4 changes: 4 additions & 0 deletions roles/install-grub-wrapper/tests/test.yml
@@ -0,0 +1,4 @@
---
- hosts: localhost
roles:
- install-grub-wrapper
2 changes: 1 addition & 1 deletion vagrant-libvirt.yml
Expand Up @@ -22,7 +22,7 @@
- install-image
- mount-root-fs
- install-config
- install-grub
- install-grub-wrapper
- install-persistence-conf
- unmount-pre
- unmount-all
Expand Down
2 changes: 1 addition & 1 deletion vmware.yml
Expand Up @@ -27,7 +27,7 @@
- install-image
- mount-root-fs
- install-config
- install-grub
- install-grub-wrapper
- install-persistence-conf
- install-cloud-init-wrapper
- fstrim
Expand Down

0 comments on commit 425b808

Please sign in to comment.