]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
93891599a5393434071bfc9005d59be875d39dba
[chef.git] / cookbooks / hardware / recipes / default.rb
1 #
2 # Cookbook:: hardware
3 # Recipe:: default
4 #
5 # Copyright:: 2012, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 include_recipe "apt"
21 include_recipe "git"
22 include_recipe "munin"
23 include_recipe "prometheus"
24 include_recipe "sysfs"
25 include_recipe "tools"
26
27 ohai_plugin "hardware" do
28   template "ohai.rb.erb"
29 end
30
31 if platform?("debian")
32   package "firmware-linux"
33 end
34
35 if node[:cpu] && node[:cpu][:"0"] && node[:cpu][:"0"][:vendor_id]
36   case node[:cpu][:"0"][:vendor_id]
37   when "GenuineIntel"
38     package "intel-microcode"
39   when "AuthenticAMD"
40     package "amd64-microcode"
41   end
42 end
43
44 if node[:dmi] && node[:dmi][:system]
45   case node[:dmi][:system][:manufacturer]
46   when "empty"
47     manufacturer = node[:dmi][:base_board][:manufacturer]
48     product = node[:dmi][:base_board][:product_name]
49   else
50     manufacturer = node[:dmi][:system][:manufacturer]
51     product = node[:dmi][:system][:product_name]
52   end
53 else
54   manufacturer = "Unknown"
55   product = "Unknown"
56 end
57
58 units = []
59
60 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric") || node[:roles].include?("prgmr")
61   units << "0"
62 end
63
64 case manufacturer
65 when "HP", "HPE"
66   include_recipe "apt::management-component-pack"
67
68   package "hponcfg"
69
70   execute "update-ilo" do
71     action :nothing
72     command "/usr/sbin/hponcfg -f /etc/ilo-defaults.xml"
73     not_if { kitchen? }
74   end
75
76   template "/etc/ilo-defaults.xml" do
77     source "ilo-defaults.xml.erb"
78     owner "root"
79     group "root"
80     mode "644"
81     notifies :run, "execute[update-ilo]"
82   end
83
84   package "hp-health" do
85     action :install
86     notifies :restart, "service[hp-health]"
87     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
88   end
89
90   service "hp-health" do
91     action [:enable, :start]
92     supports :status => true, :restart => true
93     only_if { platform?("ubuntu") && node[:lsb][:release].to_f < 22.04 }
94   end
95
96   if product.end_with?("Gen8", "Gen9")
97     package "hp-ams" do
98       action :install
99       notifies :restart, "service[hp-ams]"
100     end
101
102     service "hp-ams" do
103       action [:enable, :start]
104       supports :status => true, :restart => true
105     end
106   elsif product.end_with?("Gen10")
107     package "amsd" do
108       action :install
109       notifies :restart, "service[amsd]"
110     end
111
112     service "amsd" do
113       action [:enable, :start]
114       supports :status => true, :restart => true
115     end
116   end
117
118   units << if product.end_with?("Gen10")
119              "0"
120            else
121              "1"
122            end
123
124   watchdog_module = "hpwdt"
125 when "TYAN"
126   units << "0"
127 when "TYAN Computer Corporation"
128   units << "0"
129 when "Supermicro"
130   units << "1"
131 when "IBM"
132   units << "0"
133 when "VMware, Inc."
134   package "open-vm-tools"
135
136   # Remove timeSync plugin completely
137   # https://github.com/vmware/open-vm-tools/issues/302
138   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
139     action :delete
140     notifies :restart, "service[open-vm-tools]"
141   end
142
143   # Attempt to tell Host we are not interested in timeSync
144   execute "vmware-toolbox-cmd-timesync-disable" do
145     command "/usr/bin/vmware-toolbox-cmd timesync disable"
146     ignore_failure true
147   end
148
149   service "open-vm-tools" do
150     action [:enable, :start]
151     supports :status => true, :restart => true
152   end
153 end
154
155 units.sort.uniq.each do |unit|
156   service "serial-getty@ttyS#{unit}" do
157     action [:enable, :start]
158     not_if { kitchen? }
159   end
160 end
161
162 # if we need a different / special kernel version to make the hardware
163 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
164 # ensure that we have the package installed. the grub template will
165 # make sure that this is the default on boot.
166 if node[:hardware][:grub][:kernel]
167   kernel_version = node[:hardware][:grub][:kernel]
168
169   package "linux-image-#{kernel_version}-generic"
170   package "linux-image-extra-#{kernel_version}-generic"
171   package "linux-headers-#{kernel_version}-generic"
172   package "linux-tools-#{kernel_version}-generic"
173
174   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
175   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
176   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
177 else
178   grub_entry = "0"
179 end
180
181 if File.exist?("/etc/default/grub")
182   execute "update-grub" do
183     action :nothing
184     command "/usr/sbin/update-grub"
185     not_if { kitchen? }
186   end
187
188   template "/etc/default/grub" do
189     source "grub.erb"
190     owner "root"
191     group "root"
192     mode "644"
193     variables :units => units, :entry => grub_entry
194     notifies :run, "execute[update-grub]"
195   end
196 end
197
198 package "initramfs-tools"
199
200 execute "update-initramfs" do
201   action :nothing
202   command "update-initramfs -u -k all"
203   user "root"
204   group "root"
205 end
206
207 template "/etc/initramfs-tools/conf.d/mdadm" do
208   source "initramfs-mdadm.erb"
209   owner "root"
210   group "root"
211   mode "644"
212   notifies :run, "execute[update-initramfs]"
213 end
214
215 # haveged is only required on older kernels
216 # /dev/random is not blocking anymore in 5.15+
217 if Chef::Util.compare_versions(node[:kernel][:release], [5, 15]).negative?
218   package "haveged"
219   service "haveged" do
220     action [:enable, :start]
221   end
222 else
223   service "haveged" do
224     action [:stop, :disable]
225   end
226   package "haveged" do
227     action :remove
228   end
229 end
230
231 if node[:kernel][:modules].include?("ipmi_si")
232   package "ipmitool"
233   package "freeipmi-tools"
234
235   template "/etc/prometheus/ipmi_local.yml" do
236     source "ipmi_local.yml.erb"
237     owner "root"
238     group "root"
239     mode "644"
240   end
241
242   prometheus_exporter "ipmi" do
243     port 9290
244     user "root"
245     private_devices false
246     protect_clock false
247     system_call_filter ["@system-service", "@raw-io"]
248     options "--config.file=/etc/prometheus/ipmi_local.yml"
249     subscribes :restart, "template[/etc/prometheus/ipmi_local.yml]"
250   end
251 end
252
253 package "irqbalance"
254
255 service "irqbalance" do
256   action [:start, :enable]
257   supports :status => false, :restart => true, :reload => false
258 end
259
260 package "lldpd"
261
262 service "lldpd" do
263   action [:start, :enable]
264   supports :status => true, :restart => true, :reload => true
265 end
266
267 ohai_plugin "lldp" do
268   template "lldp.rb.erb"
269 end
270
271 package %w[
272   rasdaemon
273   ruby-sqlite3
274 ]
275
276 service "rasdaemon" do
277   action [:enable, :start]
278 end
279
280 prometheus_exporter "rasdaemon" do
281   port 9797
282   user "root"
283 end
284
285 tools_packages = []
286 status_packages = {}
287
288 if node[:virtualization][:role] != "guest" ||
289    (node[:virtualization][:system] != "lxc" &&
290     node[:virtualization][:system] != "lxd" &&
291     node[:virtualization][:system] != "openvz")
292
293   node[:kernel][:modules].each_key do |modname|
294     case modname
295     when "cciss"
296       tools_packages << "ssacli"
297       status_packages["cciss-vol-status"] ||= []
298     when "hpsa"
299       tools_packages << "ssacli"
300       status_packages["cciss-vol-status"] ||= []
301     when "mptsas"
302       tools_packages << "lsiutil"
303       status_packages["mpt-status"] ||= []
304     when "mpt2sas", "mpt3sas"
305       tools_packages << "sas2ircu"
306       status_packages["sas2ircu-status"] ||= []
307     when "megaraid_sas"
308       tools_packages << "megacli"
309       status_packages["megaclisas-status"] ||= []
310     when "aacraid"
311       tools_packages << "arcconf"
312       status_packages["aacraid-status"] ||= []
313     when "arcmsr"
314       tools_packages << "areca"
315     end
316   end
317
318   node[:block_device].each do |name, attributes|
319     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
320
321     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
322       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
323     else
324       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
325         status_packages["cciss-vol-status"] |= [File.basename(sg)]
326       end
327     end
328   end
329 end
330
331 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
332   if tools_packages.include?(tools_package)
333     package tools_package
334   else
335     package tools_package do
336       action :purge
337     end
338   end
339 end
340
341 if tools_packages.include?("areca")
342   include_recipe "git"
343
344   git "/opt/areca" do
345     action :sync
346     repository "https://git.openstreetmap.org/private/areca.git"
347     depth 1
348     user "root"
349     group "root"
350     not_if { kitchen? }
351   end
352 else
353   directory "/opt/areca" do
354     action :delete
355     recursive true
356   end
357 end
358
359 include_recipe "apt::hwraid" unless status_packages.empty?
360
361 %w[cciss-vol-status mpt-status sas2ircu-status megaclisas-status aacraid-status].each do |status_package|
362   if status_packages.include?(status_package)
363     package status_package
364
365     service "#{status_package}d" do
366       action [:stop, :disable]
367     end
368
369     file "/etc/default/#{status_package}d" do
370       action :delete
371     end
372   else
373     package status_package do
374       action :purge
375     end
376   end
377 end
378
379 systemd_service "cciss-vol-statusd" do
380   action :delete
381 end
382
383 template "/usr/local/bin/cciss-vol-statusd" do
384   action :delete
385 end
386
387 disks = if node[:hardware][:disk]
388           node[:hardware][:disk][:disks]
389         else
390           []
391         end
392
393 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
394
395 nvmes = if node[:hardware][:pci]
396           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
397         else
398           []
399         end
400
401 unless nvmes.empty?
402   package "nvme-cli"
403 end
404
405 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
406
407 if !intel_ssds.empty? || !intel_nvmes.empty?
408   package "unzip"
409
410   sst_tool_version = "1.3"
411   sst_package_version = "#{sst_tool_version}.208-0"
412
413   # remote_file "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
414   #   source "https://downloadmirror.intel.com/743764/SST_CLI_Linux_#{sst_tool_version}.zip"
415   # end
416
417   execute "#{Chef::Config[:file_cache_path]}/SST_CLI_Linux_#{sst_tool_version}.zip" do
418     command "unzip SST_CLI_Linux_#{sst_tool_version}.zip sst_#{sst_package_version}_amd64.deb"
419     cwd Chef::Config[:file_cache_path]
420     user "root"
421     group "root"
422     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb") }
423   end
424
425   dpkg_package "sst" do
426     version "#{sst_package_version}"
427     source "#{Chef::Config[:file_cache_path]}/sst_#{sst_package_version}_amd64.deb"
428   end
429
430   dpkg_package "intelmas" do
431     action :purge
432   end
433 end
434
435 disks = disks.map do |disk|
436   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
437
438   if disk[:smart_device]
439     controller = node[:hardware][:disk][:controllers][disk[:controller]]
440
441     if controller && controller[:device]
442       device = controller[:device].sub("/dev/", "")
443       smart = disk[:smart_device]
444
445       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
446         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
447         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
448       elsif smart =~ /^.*,(\d+)$/
449         munin = "#{device}-#{Regexp.last_match(1)}"
450       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
451         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
452       end
453     elsif disk[:device]
454       device = disk[:device].sub("/dev/", "")
455       smart = disk[:smart_device]
456
457       if smart =~ /^.*,(\d+),(\d+),(\d+)$/
458         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}:#{Regexp.last_match(3)}"
459       end
460     end
461   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
462     device = Regexp.last_match(1)
463     munin = device
464   elsif disk[:device]
465     device = disk[:device].sub("/dev/", "")
466     munin = device
467   end
468
469   next if device.nil? || munin.nil?
470
471   Hash[
472     :device => device,
473     :smart => smart,
474     :munin => munin,
475     :hddtemp => munin.tr("-:", "_")
476   ]
477 end
478
479 disks = disks.compact.uniq
480
481 if disks.count.positive?
482   package "smartmontools"
483
484   template "/etc/cron.daily/update-smart-drivedb" do
485     source "update-smart-drivedb.erb"
486     owner "root"
487     group "root"
488     mode "755"
489   end
490
491   template "/usr/local/bin/smartd-mailer" do
492     source "smartd-mailer.erb"
493     owner "root"
494     group "root"
495     mode "755"
496   end
497
498   template "/etc/smartd.conf" do
499     source "smartd.conf.erb"
500     owner "root"
501     group "root"
502     mode "644"
503     variables :disks => disks
504   end
505
506   template "/etc/default/smartmontools" do
507     source "smartmontools.erb"
508     owner "root"
509     group "root"
510     mode "644"
511   end
512
513   service "smartmontools" do
514     action [:enable, :start]
515     subscribes :reload, "template[/etc/smartd.conf]"
516     subscribes :restart, "template[/etc/default/smartmontools]"
517   end
518
519   template "/etc/prometheus/collectors/smart.devices" do
520     source "smart.devices.erb"
521     owner "root"
522     group "root"
523     mode "644"
524     variables :disks => disks
525   end
526
527   prometheus_collector "smart" do
528     interval "15m"
529     user "root"
530     capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
531     private_devices false
532     private_users false
533     protect_clock false
534   end
535
536   # Don't try and do munin monitoring of disks behind
537   # an Areca controller as they only allow one thing to
538   # talk to the controller at a time and smartd will
539   # throw errors if it clashes with munin
540   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
541
542   disks.each do |disk|
543     munin_plugin "smart_#{disk[:munin]}" do
544       target "smart_"
545       conf "munin.smart.erb"
546       conf_variables :disk => disk
547     end
548   end
549 else
550   service "smartd" do
551     action [:stop, :disable]
552   end
553 end
554
555 if disks.count.positive?
556   munin_plugin "hddtemp_smartctl" do
557     conf "munin.hddtemp.erb"
558     conf_variables :disks => disks
559   end
560 else
561   munin_plugin "hddtemp_smartctl" do
562     action :delete
563     conf "munin.hddtemp.erb"
564   end
565 end
566
567 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
568           disks.map { |d| "smart_#{d[:munin]}" }
569
570 plugins.each do |plugin|
571   munin_plugin plugin do
572     action :delete
573     conf "munin.smart.erb"
574   end
575 end
576
577 if File.exist?("/etc/mdadm/mdadm.conf")
578   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
579     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
580
581     line
582   end
583
584   file "/etc/mdadm/mdadm.conf" do
585     owner "root"
586     group "root"
587     mode "644"
588     content mdadm_conf
589   end
590
591   service "mdmonitor" do
592     action :nothing
593     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
594   end
595 end
596
597 file "/etc/modules" do
598   action :delete
599 end
600
601 node[:hardware][:modules].each do |module_name|
602   kernel_module module_name do
603     action :install
604     not_if { kitchen? }
605   end
606 end
607
608 node[:hardware][:blacklisted_modules].each do |module_name|
609   kernel_module module_name do
610     action :blacklist
611   end
612 end
613
614 if watchdog_module
615   kernel_module watchdog_module do
616     action :install
617   end
618
619   execute "systemctl-reload" do
620     action :nothing
621     command "systemctl daemon-reload"
622     user "root"
623     group "root"
624   end
625
626   directory "/etc/systemd/system.conf.d" do
627     owner "root"
628     group "root"
629     mode "755"
630   end
631
632   template "/etc/systemd/system.conf.d/watchdog.conf" do
633     source "watchdog.conf.erb"
634     owner "root"
635     group "root"
636     mode "644"
637     notifies :run, "execute[systemctl-reload]"
638   end
639 end
640
641 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
642   package "lm-sensors"
643
644   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
645     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
646     chip = format("coretemp-isa-%04d", cpu)
647
648     temps = if File.exist?("#{coretemp}/name")
649               Dir.glob("#{coretemp}/temp*_input").map do |temp|
650                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
651               end.sort
652             else
653               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
654                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
655               end.sort
656             end
657
658     if temps.first == 1
659       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
660       temps.shift
661     end
662
663     temps.each_with_index do |temp, index|
664       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
665     end
666   end
667
668   execute "/etc/sensors.d/chef.conf" do
669     action :nothing
670     command "/usr/bin/sensors -s"
671     user "root"
672     group "root"
673   end
674
675   template "/etc/sensors.d/chef.conf" do
676     source "sensors.conf.erb"
677     owner "root"
678     group "root"
679     mode "644"
680     notifies :run, "execute[/etc/sensors.d/chef.conf]"
681   end
682 end
683
684 if node[:hardware][:shm_size]
685   execute "remount-dev-shm" do
686     action :nothing
687     command "/bin/mount -o remount /dev/shm"
688     user "root"
689     group "root"
690   end
691
692   mount "/dev/shm" do
693     action :enable
694     device "tmpfs"
695     fstype "tmpfs"
696     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
697     notifies :run, "execute[remount-dev-shm]"
698   end
699 end
700
701 prometheus_collector "ohai" do
702   interval "15m"
703   user "root"
704   proc_subset "all"
705   capability_bounding_set %w[CAP_DAC_OVERRIDE CAP_SYS_ADMIN CAP_SYS_RAWIO]
706   private_devices false
707   private_users false
708   protect_clock false
709   protect_kernel_modules false
710 end