summaryrefslogtreecommitdiff
path: root/manifests/init.pp
blob: 4b89208d8844c02a9b64828b89e325f5821f3798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
class syslinux (
  String $kernel = 'linux',
  String $efi_root = '/boot/efi',
  String $bootentry = 'syslinux',

  Hash[String,Syslinux::Bootentry,1] $boot_entries,
  String $default_boot_entry = $boot_entries.map |$k, $_| { $k }[0],
) {

  $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']


  $cpus = $facts['processors']['models']
  $has_amd   = $cpus.any |$i| { $i =~ /AMD/ }
  $has_intel = $cpus.any |$i| { $i =~ /Intel/ }

  if $has_amd   { ensure_packages(['amd-ucode']) }
  if $has_intel { ensure_packages(['intel-ucode']) }

  $entries = $boot_entries.map |$key, $entry| {
    case $entry['type'] {
      'linux': {
        $extra_args = $entry['extra_args']
        $initrd = $entry['initrd']
        $initrd_multi = [
          if $has_amd { '../amd-ucode.img' },
          if $has_intel { '../intel-ucode.img' },
          "../arch/${initrd}",
          ].filter |$i| { $i != undef }.join(',')
        $hash = {
          'APPEND' => "root=PARTUUID=${partuuid} rw ${extra_args}",
          'INITRD' => $initrd_multi,
          'LINUX'  => "../arch/vmlinuz-${kernel}",
        }
      }
      'com': {
        $com = $entry['com']
        $hash = {
          'COM32' => "${com}.c32",
        }
      }
    }

    $common = { 'MENU LABEL' => $entry['label'], }
    [$key, $common + $hash]
  }.convert_to(Hash)

  file { "${efi_root}/EFI/syslinux/syslinux.cfg":
    content     => epp("${module_name}/syslinux.cfg.epp", {
      'default' => $default_boot_entry,
      'entries' => $entries,
      })
  }

  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
      # File managed by Puppet
      IFS='\n' read data
      cp "/\$data" "${efi_root}/EFI/arch/vmlinuz-${kernel}"
      | EOF
  }

  file { '/usr/libexec/move-ucode':
    ensure  => file,
    mode    => '0555',
    content => @("EOF"/$)
      #!/bin/sh
      # File managed by Puppet
      IFS='\n' read data
      cp "/\$data" "${efi_root}/EFI/"
      | 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 ,
  }


  pacman::hook { 'install-ucode-initrd':
    priority    => 60,
    trigger     => {
      type        => 'Path',
      operation   => [ 'Install', 'Upgrade' ],
      target      => [ 'boot/*-ucode.img', ],
    },
    description  => 'Moving µCode-updates to EFI',
    when         => 'PostTransaction',
    exec         => '/usr/libexec/move-ucode',
    needsTargets => true ,
  }
}