From f3883f13d3df36389112f3f33d438f36a2c347e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Fri, 5 Nov 2021 16:25:36 +0100 Subject: Setup syslinux. --- modules/syslinux/files/mkinitcpio.linux | 14 ++++ modules/syslinux/lib/facter/blkid.rb | 17 +++++ modules/syslinux/lib/facter/efibootmgr.rb | 19 +++++ modules/syslinux/lib/facter/partid.rb | 16 ++++ modules/syslinux/manifests/init.pp | 114 ++++++++++++++++++++++++++++ modules/syslinux/templates/syslinux.cfg.epp | 69 +++++++++++++++++ 6 files changed, 249 insertions(+) create mode 100644 modules/syslinux/files/mkinitcpio.linux create mode 100644 modules/syslinux/lib/facter/blkid.rb create mode 100644 modules/syslinux/lib/facter/efibootmgr.rb create mode 100644 modules/syslinux/lib/facter/partid.rb create mode 100644 modules/syslinux/manifests/init.pp create mode 100644 modules/syslinux/templates/syslinux.cfg.epp diff --git a/modules/syslinux/files/mkinitcpio.linux b/modules/syslinux/files/mkinitcpio.linux new file mode 100644 index 0000000..81554d7 --- /dev/null +++ b/modules/syslinux/files/mkinitcpio.linux @@ -0,0 +1,14 @@ +# mkinitcpio preset file for the 'linux' package + +ALL_config="/etc/mkinitcpio.conf" +ALL_kver="/boot/efi/EFI/arch/vmlinuz-linux" + +PRESETS=('default' 'fallback') + +#default_config="/etc/mkinitcpio.conf" +default_image="/boot/efi/EFI/arch/initramfs-linux.img" +#default_options="" + +#fallback_config="/etc/mkinitcpio.conf" +fallback_image="/boot/efi/EFI/arch/initramfs-linux-fallback.img" +fallback_options="-S autodetect" diff --git a/modules/syslinux/lib/facter/blkid.rb b/modules/syslinux/lib/facter/blkid.rb new file mode 100644 index 0000000..79c6135 --- /dev/null +++ b/modules/syslinux/lib/facter/blkid.rb @@ -0,0 +1,17 @@ +Facter.add('blkid') do + setcode do + lines = Facter::Core::Execution.execute('blkid').split("\n") + out = {} + lines.each do |line| + a = line.match(/^([^:]*): (.*)/) + d = {} + a[2].split(' ').each do |pair| + m = pair.match(/(\w*)="(([^"]|\\")*)"/) + d[m[1]] = m[2] + end + out[a[1]] = d + end + out + end +end + diff --git a/modules/syslinux/lib/facter/efibootmgr.rb b/modules/syslinux/lib/facter/efibootmgr.rb new file mode 100644 index 0000000..fdf95e0 --- /dev/null +++ b/modules/syslinux/lib/facter/efibootmgr.rb @@ -0,0 +1,19 @@ +Facter.add('efi') do + setcode do + out = {} + data = Facter::Core::Execution.execute('efibootmgr') + boots = {} + data.split("\n").each do |item| + if a = item.match(/BootOrder: (.*)/) then + out['BootOrder'] = a[1].split(',') + elsif a = item.match(/(\w*): (.*)/) then + out[a[1]] = a[2] + elsif a = item.match(/Boot(\d*)(\*?) (.*)/) + # a[2] contains if it's active + boots[a[1]] = a[3] + end + end + out['boots'] = boots + out + end +end diff --git a/modules/syslinux/lib/facter/partid.rb b/modules/syslinux/lib/facter/partid.rb new file mode 100644 index 0000000..53fc37c --- /dev/null +++ b/modules/syslinux/lib/facter/partid.rb @@ -0,0 +1,16 @@ +Facter.add('partinfo') do + setcode do + obj = {} + Dir.entries('/sys/class/block/').each do |entry| + if entry == '.' or entry == '..' then next end + path = "/sys/class/block/#{entry}/partition" + if File.file?(path) then + obj[entry] = { + 'partid' => File.read(path).strip(), + 'device' => File.basename(File.dirname(File.readlink("/sys/class/block/#{entry}"))), + } + end + end + obj + end +end diff --git a/modules/syslinux/manifests/init.pp b/modules/syslinux/manifests/init.pp new file mode 100644 index 0000000..570b31c --- /dev/null +++ b/modules/syslinux/manifests/init.pp @@ -0,0 +1,114 @@ +class syslinux ( + String $kernel = 'linux', + String $efi_root = '/boot/efi', + String $bootentry = 'syslinux', +) { + + $efi_dev = $facts['mountpoints'][$efi_root] + if ! $efi_dev { + fail("A device needs to be mounted on efi_root [${efi_root}]") + } + # $efi_dev['device'] + + ensure_packages ([ + $kernel, + mkinitcpio, + syslinux, + efibootmgr, + ], { + ensure => installed, + }) + + file { "/etc/mkinitcpio.d/${kernel}.preset": + ensure => file, + source => "puppet:///modules/${module_name}/mkinitcpio.${kernel}", + } + + # cp -r /usr/lib/syslinux/efi64 ${efi_root}/EFI/syslinux + + $device = $facts['mountpoints']['/']['device'] + $partuuid = $facts['blkid'][$device]['PARTUUID'] + + file { "${efi_root}/EFI/syslinux/syslinux.cfg": + content => epp("${module_name}/syslinux.cfg.epp", { + 'default' => 'arch', + 'linux' => { + 'arch' => { + 'label' => 'Arch Linux', + 'initrd' => 'initramfs-linux.img', + 'args' => "root=PARTUUID=${partuuid} rw", + }, + 'arch-runlevel-1' => { + 'label' => 'Arch Linux (Runlevel 1)', + 'initrd' => 'initramfs-linux.img', + 'args' => "root=PARTUUID=${partuuid} rw 1", + }, + 'archfallback' => { + 'label' => 'Arch Linux fallback', + 'initrd' => 'initramfs-linux-fallback.img', + 'args' => "root=PARTUUID=${partuuid} rw", + } + }, + 'com32' => { + 'hdt' => { + 'label' => 'HDT (Hardware Detection Tool)', + 'com' => 'hdt', + }, + 'reboot' => { + 'label' => 'Reboot', + 'com' => 'reboot', + }, + 'poweroff' => { + 'label' => 'Poweroff', + 'com' => 'poweroff', + } + + } + }) + } + + file { "${efi_root}/EFI/arch": + ensure => directory, + } + + $has_syslinux = $facts['efi']['boots'].any |$_, $value| { + $value == $bootentry + } + + $partition = $facts['partinfo'][basename($efi_dev['device'])] + + if ! $has_syslinux { + $efi_device = $partition['device'] + $partid = $partition['partid'] + exec { "efibootmgr --disk '/dev/${efi_device}' --part ${partid} --create --label '${bootentry}' --loader /EFI/syslinux/syslinux.efi": + path => [ '/usr/bin', '/bin', ], + } + } + + file { '/usr/libexec': + ensure => directory, + } + + file { '/usr/libexec/move-kernel': + ensure => file, + mode => '0555', + content => @("EOF"/$) + #!/bin/sh + IFS='\n' read data + cp "/\$data" "${efi_root}/EFI/arch/vmlinuz-${kernel}" + | EOF + } + + pacman::hook { 'install-kernel': + priority => 60, # something less than /usr/share/libalpm/hooks/90-mkinitcpio-install.hook + trigger => { + type => 'Path', + operation => [ 'Install', 'Upgrade' ], + target => [ 'usr/lib/modules/*/vmlinuz', ], + }, + description => 'Moving kernel to EFI', + when => 'PostTransaction', + exec => '/usr/libexec/move-kernel', + needsTargets => true , + } +} diff --git a/modules/syslinux/templates/syslinux.cfg.epp b/modules/syslinux/templates/syslinux.cfg.epp new file mode 100644 index 0000000..4386b74 --- /dev/null +++ b/modules/syslinux/templates/syslinux.cfg.epp @@ -0,0 +1,69 @@ +<%- | String $default, + Hash $linux, + Hash $com32, +| -%> +# Config file for Syslinux - +# /boot/syslinux/syslinux.cfg +# +# Comboot modules: +# * menu.c32 - provides a text menu +# * vesamenu.c32 - provides a graphical menu +# * chain.c32 - chainload MBRs, partition boot sectors, Windows bootloaders +# * hdt.c32 - hardware detection tool +# * reboot.c32 - reboots the system +# +# To Use: Copy the respective files from /usr/lib/syslinux to /boot/syslinux. +# If /usr and /boot are on the same file system, symlink the files instead +# of copying them. +# +# If you do not use a menu, a 'boot:' prompt will be shown and the system +# will boot automatically after 5 seconds. +# +# Please review the wiki: https://wiki.archlinux.org/index.php/Syslinux +# The wiki provides further configuration examples + +DEFAULT <%= $default %> +PROMPT 0 # Set to 1 if you always want to display the boot: prompt +TIMEOUT 50 +# You can create syslinux keymaps with the keytab-lilo tool +#KBDMAP de.ktl + +# Menu Configuration +# Either menu.c32 or vesamenu32.c32 must be copied to /boot/syslinux +UI menu.c32 +#UI vesamenu.c32 + +# Refer to http://syslinux.zytor.com/wiki/index.php/Doc/menu +MENU TITLE Arch Linux +#MENU BACKGROUND splash.png +MENU COLOR border 30;44 #40ffffff #a0000000 std +MENU COLOR title 1;36;44 #9033ccff #a0000000 std +MENU COLOR sel 7;37;40 #e0ffffff #20ffffff all +MENU COLOR unsel 37;44 #50ffffff #a0000000 std +MENU COLOR help 37;40 #c0ffffff #a0000000 std +MENU COLOR timeout_msg 37;40 #80ffffff #00000000 std +MENU COLOR timeout 1;37;40 #c0ffffff #00000000 std +MENU COLOR msg07 37;40 #90ffffff #a0000000 std +MENU COLOR tabmsg 31;40 #30ffffff #00000000 std + +# boot sections follow +# +# TIP: If you want a 1024x768 framebuffer, add "vga=773" to your kernel line. +# +#-* + + +<%- $linux.each |$name, $entry| { -%> +LABEL <%= $name %> + MENU LABEL <%= $entry['label'] %> + LINUX ../arch/vmlinuz-<%= $syslinux::kernel %> + APPEND <%= $entry['args'] %> + INITRD ../arch/<%= $entry['initrd'] %> +<%- } -%> + +<%- $com32.each |$name, $entry| { -%> +LABEL <%= $name %> + MENU LABEL <%= $entry['label'] %> + COM32 <%= $entry['com'] %>.c32 +<%- } -%> + -- cgit v1.2.3