Skip to content

Instantly share code, notes, and snippets.

@andrewgross
Created September 11, 2012 22:45
Show Gist options
  • Save andrewgross/3702731 to your computer and use it in GitHub Desktop.
Save andrewgross/3702731 to your computer and use it in GitHub Desktop.
mdadm LWRP :create override
action :create do
unless @current_resource.exists
command = "yes | mdadm --create #{@new_resource.raid_device} --chunk=#{@new_resource.chunk} --level #{@new_resource.level} --raid-devices #{@new_resource.devices.length} #{@new_resource.devices.join(" ")}"
# Check the mdadm raid registry
# Existing raids will show as active or inactive
# Take the name of the raid array
find_current_raid_arrays_command = "cat /proc/mdstat | grep active | awk '{print $1}'"
Chef::Log.debug("#{@new_resource} mdadm command: #{command}")
attempts = 0
previous_raid_arrays = shell_out!(find_current_raid_arrays_command).stdout.split()
Chef::Log.debug("#{@new_resource} Previous Raid Arrays: #{previous_raid_arrays}")
begin
create_array = shell_out!(command)
rescue Exception
attempts += 1
# Sometimes a blkid process will be tying up the resource after mdadm usage, it should clear quickly
sleep(1)
current_raid_arrays = shell_out!(find_current_raid_arrays_command).stdout.split()
Chef::Log.debug("Current Raid Arrays: #{current_raid_arrays}")
failed_raid_arrays = current_raid_arrays - previous_raid_arrays
Chef::Log.debug("Failed Raid Arrays: #{failed_raid_arrays}")
failed_raid_arrays.each do |raid_array|
stop_raid_array_command = "mdadm --stop /dev/#{raid_array}"
stop_raid_array = shell_out!(stop_raid_array_command)
Chef::Log::debug("#{@new_resource} Attempting to stop failed arrays: #{stop_raid_array.stdout}")
end
Chef::Log::info("#{@new_resource} mdadm raid creation failed. Performing retry #{attempts} out of #{@new_resource.attempts}")
sleep(1)
retry unless attempts > @new_resource.attempts
end
Chef::Log.info("#{@new_resource} created raid device (#{@new_resource.raid_device})")
# Avoid issue where it may show as /dev/md0 or /dev/md/0
raid_uuid_command = "mdadm --detail --scan | sed 's:/::g' | grep #{new_resource.raid_device.gsub('/', '')} | awk -F 'UUID=' '{print $2}' | awk '{print $1}'"
raid_uuid = shell_out!(raid_uuid_command).stdout
raid_config_command = "mdadm --detail --scan | grep #{raid_uuid}"
raid_config = shell_out!(raid_config_command).stdout
persist_raid_config_command = "/usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf"
shell_out!(persist_raid_config_command)
Chef::Log.info("#{@new_resource} added raid device configuration (#{raid_config})")
update_initramfs_command = "update-initramfs -u"
shell_out!(update_initramfs_command)
Chef::Log.info("#{@new_resource} updated initramfs image")
@new_resource.updated_by_last_action(true)
else
Chef::Log.debug("#{@new_resource} raid device already exists, skipping create (#{@new_resource.raid_device})")
end
new_resource.updated_by_last_action(true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment