]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/recipes/default.rb
Use ttyS1 for serial console on all Supermicro servers
[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 "sysfs"
24 include_recipe "tools"
25
26 ohai_plugin "hardware" do
27   template "ohai.rb.erb"
28 end
29
30 case node[:cpu][:"0"][:vendor_id]
31 when "GenuineIntel"
32   package "intel-microcode"
33 when "AuthenticAMD"
34   package "amd64-microcode"
35 end
36
37 if node[:dmi] && node[:dmi][:system]
38   case node[:dmi][:system][:manufacturer]
39   when "empty"
40     manufacturer = node[:dmi][:base_board][:manufacturer]
41     product = node[:dmi][:base_board][:product_name]
42   else
43     manufacturer = node[:dmi][:system][:manufacturer]
44     product = node[:dmi][:system][:product_name]
45   end
46 else
47   manufacturer = "Unknown"
48   product = "Unknown"
49 end
50
51 units = []
52
53 if node[:roles].include?("bytemark") || node[:roles].include?("exonetric")
54   units << "0"
55 end
56
57 case manufacturer
58 when "HP"
59   package "hponcfg"
60
61   package "hp-health" do
62     action :install
63     notifies :restart, "service[hp-health]"
64   end
65
66   service "hp-health" do
67     action [:enable, :start]
68     supports :status => true, :restart => true
69   end
70
71   if product.end_with?("Gen8", "Gen9")
72     package "hp-ams" do
73       action :install
74       notifies :restart, "service[hp-ams]"
75     end
76
77     service "hp-ams" do
78       action [:enable, :start]
79       supports :status => true, :restart => true
80     end
81   end
82
83   units << "1"
84 when "TYAN"
85   units << "0"
86 when "TYAN Computer Corporation"
87   units << "0"
88 when "Supermicro"
89   units << "1"
90 when "IBM"
91   units << "0"
92 when "VMware, Inc."
93   package "open-vm-tools"
94
95   # Remove timeSync plugin completely
96   # https://github.com/vmware/open-vm-tools/issues/302
97   file "/usr/lib/open-vm-tools/plugins/vmsvc/libtimeSync.so" do
98     action :delete
99     notifies :restart, "service[open-vm-tools]"
100   end
101
102   # Attempt to tell Host we are not interested in timeSync
103   execute "vmware-toolbox-cmd-timesync-disable" do
104     command "/usr/bin/vmware-toolbox-cmd timesync disable"
105     ignore_failure true
106   end
107
108   service "open-vm-tools" do
109     action [:enable, :start]
110     supports :status => true, :restart => true
111   end
112 end
113
114 units.sort.uniq.each do |unit|
115   service "serial-getty@ttyS#{unit}" do
116     action [:enable, :start]
117   end
118 end
119
120 # if we need a different / special kernel version to make the hardware
121 # work (e.g: https://github.com/openstreetmap/operations/issues/45) then
122 # ensure that we have the package installed. the grub template will
123 # make sure that this is the default on boot.
124 if node[:hardware][:grub][:kernel]
125   kernel_version = node[:hardware][:grub][:kernel]
126
127   package "linux-image-#{kernel_version}-generic"
128   package "linux-image-extra-#{kernel_version}-generic"
129   package "linux-headers-#{kernel_version}-generic"
130   package "linux-tools-#{kernel_version}-generic"
131
132   boot_device = IO.popen(["df", "/boot"]).readlines.last.split.first
133   boot_uuid = IO.popen(["blkid", "-o", "value", "-s", "UUID", boot_device]).readlines.first.chomp
134   grub_entry = "gnulinux-advanced-#{boot_uuid}>gnulinux-#{kernel_version}-advanced-#{boot_uuid}"
135 else
136   grub_entry = "0"
137 end
138
139 if File.exist?("/etc/default/grub")
140   execute "update-grub" do
141     action :nothing
142     command "/usr/sbin/update-grub"
143   end
144
145   template "/etc/default/grub" do
146     source "grub.erb"
147     owner "root"
148     group "root"
149     mode 0o644
150     variables :units => units, :entry => grub_entry
151     notifies :run, "execute[update-grub]"
152   end
153 end
154
155 execute "update-initramfs" do
156   action :nothing
157   command "update-initramfs -u -k all"
158   user "root"
159   group "root"
160 end
161
162 template "/etc/initramfs-tools/conf.d/mdadm" do
163   source "initramfs-mdadm.erb"
164   owner "root"
165   group "root"
166   mode 0o644
167   notifies :run, "execute[update-initramfs]"
168 end
169
170 package "haveged"
171 service "haveged" do
172   action [:enable, :start]
173 end
174
175 package "ipmitool" if node[:kernel][:modules].include?("ipmi_si")
176
177 package "irqbalance"
178
179 service "irqbalance" do
180   action [:start, :enable]
181   supports :status => false, :restart => true, :reload => false
182 end
183
184 # Link Layer Discovery Protocol Daemon
185 package "lldpd"
186 service "lldpd" do
187   action [:start, :enable]
188   supports :status => true, :restart => true, :reload => true
189 end
190
191 tools_packages = []
192 status_packages = {}
193
194 if node[:virtualization][:role] != "guest" ||
195    (node[:virtualization][:system] != "lxc" &&
196     node[:virtualization][:system] != "lxd" &&
197     node[:virtualization][:system] != "openvz")
198
199   node[:kernel][:modules].each_key do |modname|
200     case modname
201     when "cciss"
202       tools_packages << "ssacli"
203       status_packages["cciss-vol-status"] ||= []
204     when "hpsa"
205       tools_packages << "ssacli"
206       status_packages["cciss-vol-status"] ||= []
207     when "mptsas"
208       tools_packages << "lsiutil"
209       status_packages["mpt-status"] ||= []
210     when "mpt2sas", "mpt3sas"
211       tools_packages << "sas2ircu"
212       status_packages["sas2ircu-status"] ||= []
213     when "megaraid_mm"
214       tools_packages << "megactl"
215       status_packages["megaraid-status"] ||= []
216     when "megaraid_sas"
217       tools_packages << "megacli"
218       status_packages["megaclisas-status"] ||= []
219     when "aacraid"
220       tools_packages << "arcconf"
221       status_packages["aacraid-status"] ||= []
222     when "arcmsr"
223       tools_packages << "areca"
224     end
225   end
226
227   node[:block_device].each do |name, attributes|
228     next unless attributes[:vendor] == "HP" && attributes[:model] == "LOGICAL VOLUME"
229
230     if name =~ /^cciss!(c[0-9]+)d[0-9]+$/
231       status_packages["cciss-vol-status"] |= ["cciss/#{Regexp.last_match[1]}d0"]
232     else
233       Dir.glob("/sys/block/#{name}/device/scsi_generic/*").each do |sg|
234         status_packages["cciss-vol-status"] |= [File.basename(sg)]
235       end
236     end
237   end
238 end
239
240 %w[ssacli lsiutil sas2ircu megactl megacli arcconf].each do |tools_package|
241   if tools_packages.include?(tools_package)
242     package tools_package
243   else
244     package tools_package do
245       action :purge
246     end
247   end
248 end
249
250 if tools_packages.include?("areca")
251   include_recipe "git"
252
253   git "/opt/areca" do
254     action :sync
255     repository "https://git.openstreetmap.org/private/areca.git"
256     depth 1
257     user "root"
258     group "root"
259     not_if { ENV["TEST_KITCHEN"] }
260   end
261 else
262   directory "/opt/areca" do
263     action :delete
264     recursive true
265   end
266 end
267
268 if status_packages.include?("cciss-vol-status")
269   template "/usr/local/bin/cciss-vol-statusd" do
270     source "cciss-vol-statusd.erb"
271     owner "root"
272     group "root"
273     mode 0o755
274     notifies :restart, "service[cciss-vol-statusd]"
275   end
276
277   systemd_service "cciss-vol-statusd" do
278     description "Check cciss_vol_status values in the background"
279     exec_start "/usr/local/bin/cciss-vol-statusd"
280     private_tmp true
281     protect_system "full"
282     protect_home true
283     no_new_privileges true
284     notifies :restart, "service[cciss-vol-statusd]"
285   end
286 else
287   systemd_service "cciss-vol-statusd" do
288     action :delete
289   end
290
291   template "/usr/local/bin/cciss-vol-statusd" do
292     action :delete
293   end
294 end
295
296 %w[cciss-vol-status mpt-status sas2ircu-status megaraid-status megaclisas-status aacraid-status].each do |status_package|
297   if status_packages.include?(status_package)
298     package status_package
299
300     template "/etc/default/#{status_package}d" do
301       source "raid.default.erb"
302       owner "root"
303       group "root"
304       mode 0o644
305       variables :devices => status_packages[status_package]
306     end
307
308     service "#{status_package}d" do
309       action [:start, :enable]
310       supports :status => false, :restart => true, :reload => false
311       subscribes :restart, "template[/etc/default/#{status_package}d]"
312     end
313   else
314     package status_package do
315       action :purge
316     end
317
318     file "/etc/default/#{status_package}d" do
319       action :delete
320     end
321   end
322 end
323
324 disks = if node[:hardware][:disk]
325           node[:hardware][:disk][:disks]
326         else
327           []
328         end
329
330 intel_ssds = disks.select { |d| d[:vendor] == "INTEL" && d[:model] =~ /^SSD/ }
331
332 nvmes = if node[:hardware][:pci]
333           node[:hardware][:pci].values.select { |pci| pci[:driver] == "nvme" }
334         else
335           []
336         end
337
338 intel_nvmes = nvmes.select { |pci| pci[:vendor_name] == "Intel Corporation" }
339
340 if !intel_ssds.empty? || !intel_nvmes.empty?
341   package "unzip"
342
343   intel_ssd_tool_version = "3.0.24"
344
345   remote_file "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
346     source "https://downloadmirror.intel.com/29399/eng/Intel_SSD_DCT_#{intel_ssd_tool_version}_Linux%20.zip"
347   end
348
349   execute "#{Chef::Config[:file_cache_path]}/Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip" do
350     command "unzip Intel_SSD_Data_Center_Tool_#{intel_ssd_tool_version}_Linux.zip isdct_#{intel_ssd_tool_version}-1_amd64.deb"
351     cwd Chef::Config[:file_cache_path]
352     user "root"
353     group "root"
354     not_if { ::File.exist?("#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb") }
355   end
356
357   dpkg_package "isdct" do
358     version "#{intel_ssd_tool_version}-1"
359     source "#{Chef::Config[:file_cache_path]}/isdct_#{intel_ssd_tool_version}-1_amd64.deb"
360   end
361 end
362
363 disks = disks.map do |disk|
364   next if disk[:state] == "spun_down" || %w[unconfigured failed].any?(disk[:status])
365
366   if disk[:smart_device]
367     controller = node[:hardware][:disk][:controllers][disk[:controller]]
368
369     if controller && controller[:device]
370       device = controller[:device].sub("/dev/", "")
371       smart = disk[:smart_device]
372
373       if device.start_with?("cciss/") && smart =~ /^cciss,(\d+)$/
374         array = node[:hardware][:disk][:arrays][disk[:arrays].first]
375         munin = "cciss-3#{array[:wwn]}-#{Regexp.last_match(1)}"
376       elsif smart =~ /^.*,(\d+)$/
377         munin = "#{device}-#{Regexp.last_match(1)}"
378       elsif smart =~ %r{^.*,(\d+)/(\d+)$}
379         munin = "#{device}-#{Regexp.last_match(1)}:#{Regexp.last_match(2)}"
380       end
381     end
382   elsif disk[:device] =~ %r{^/dev/(nvme\d+)n\d+$}
383     device = Regexp.last_match(1)
384     munin = device
385   elsif disk[:device]
386     device = disk[:device].sub("/dev/", "")
387     munin = device
388   end
389
390   next if device.nil?
391
392   Hash[
393     :device => device,
394     :smart => smart,
395     :munin => munin,
396     :hddtemp => munin.tr("-:", "_")
397   ]
398 end
399
400 disks = disks.compact.uniq
401
402 if disks.count.positive?
403   package "smartmontools"
404
405   template "/usr/local/bin/smartd-mailer" do
406     source "smartd-mailer.erb"
407     owner "root"
408     group "root"
409     mode 0o755
410   end
411
412   template "/etc/smartd.conf" do
413     source "smartd.conf.erb"
414     owner "root"
415     group "root"
416     mode 0o644
417     variables :disks => disks
418   end
419
420   template "/etc/default/smartmontools" do
421     source "smartmontools.erb"
422     owner "root"
423     group "root"
424     mode 0o644
425   end
426
427   service "smartd" do
428     action [:enable, :start]
429     subscribes :reload, "template[/etc/smartd.conf]"
430     subscribes :restart, "template[/etc/default/smartmontools]"
431   end
432
433   # Don't try and do munin monitoring of disks behind
434   # an Areca controller as they only allow one thing to
435   # talk to the controller at a time and smartd will
436   # throw errors if it clashes with munin
437   disks = disks.reject { |disk| disk[:smart]&.start_with?("areca,") }
438
439   disks.each do |disk|
440     munin_plugin "smart_#{disk[:munin]}" do
441       target "smart_"
442       conf "munin.smart.erb"
443       conf_variables :disk => disk
444     end
445   end
446 else
447   service "smartd" do
448     action [:stop, :disable]
449   end
450 end
451
452 if disks.count.positive?
453   munin_plugin "hddtemp_smartctl" do
454     conf "munin.hddtemp.erb"
455     conf_variables :disks => disks
456   end
457 else
458   munin_plugin "hddtemp_smartctl" do
459     action :delete
460     conf "munin.hddtemp.erb"
461   end
462 end
463
464 plugins = Dir.glob("/etc/munin/plugins/smart_*").map { |p| File.basename(p) } -
465           disks.map { |d| "smart_#{d[:munin]}" }
466
467 plugins.each do |plugin|
468   munin_plugin plugin do
469     action :delete
470     conf "munin.smart.erb"
471   end
472 end
473
474 if File.exist?("/etc/mdadm/mdadm.conf")
475   mdadm_conf = edit_file "/etc/mdadm/mdadm.conf" do |line|
476     line.gsub!(/^MAILADDR .*$/, "MAILADDR admins@openstreetmap.org")
477
478     line
479   end
480
481   file "/etc/mdadm/mdadm.conf" do
482     owner "root"
483     group "root"
484     mode 0o644
485     content mdadm_conf
486   end
487
488   service "mdadm" do
489     action :nothing
490     subscribes :restart, "file[/etc/mdadm/mdadm.conf]"
491   end
492 end
493
494 template "/etc/modules" do
495   source "modules.erb"
496   owner "root"
497   group "root"
498   mode 0o644
499 end
500
501 service "kmod" do
502   action :nothing
503   subscribes :start, "template[/etc/modules]"
504 end
505
506 if node[:hardware][:watchdog]
507   package "watchdog"
508
509   template "/etc/default/watchdog" do
510     source "watchdog.erb"
511     owner "root"
512     group "root"
513     mode 0o644
514     variables :module => node[:hardware][:watchdog]
515   end
516
517   service "watchdog" do
518     action [:enable, :start]
519   end
520 end
521
522 unless Dir.glob("/sys/class/hwmon/hwmon*").empty?
523   package "lm-sensors"
524
525   Dir.glob("/sys/devices/platform/coretemp.*").each do |coretemp|
526     cpu = File.basename(coretemp).sub("coretemp.", "").to_i
527     chip = format("coretemp-isa-%04d", cpu)
528
529     temps = if File.exist?("#{coretemp}/name")
530               Dir.glob("#{coretemp}/temp*_input").map do |temp|
531                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
532               end.sort
533             else
534               Dir.glob("#{coretemp}/hwmon/hwmon*/temp*_input").map do |temp|
535                 File.basename(temp).sub("temp", "").sub("_input", "").to_i
536               end.sort
537             end
538
539     if temps.first == 1
540       node.default[:hardware][:sensors][chip][:temps][:temp1][:label] = "CPU #{cpu}"
541       temps.shift
542     end
543
544     temps.each_with_index do |temp, index|
545       node.default[:hardware][:sensors][chip][:temps]["temp#{temp}"][:label] = "CPU #{cpu} Core #{index}"
546     end
547   end
548
549   execute "/etc/sensors.d/chef.conf" do
550     action :nothing
551     command "/usr/bin/sensors -s"
552     user "root"
553     group "root"
554   end
555
556   template "/etc/sensors.d/chef.conf" do
557     source "sensors.conf.erb"
558     owner "root"
559     group "root"
560     mode 0o644
561     notifies :run, "execute[/etc/sensors.d/chef.conf]"
562   end
563 end
564
565 if node[:hardware][:shm_size]
566   execute "remount-dev-shm" do
567     action :nothing
568     command "/bin/mount -o remount /dev/shm"
569     user "root"
570     group "root"
571   end
572
573   mount "/dev/shm" do
574     action :enable
575     device "tmpfs"
576     fstype "tmpfs"
577     options "rw,nosuid,nodev,size=#{node[:hardware][:shm_size]}"
578     notifies :run, "execute[remount-dev-shm]"
579   end
580 end