Created
April 10, 2013 20:48
-
-
Save andrewgross/5358309 to your computer and use it in GitHub Desktop.
Beware i wrote this over a year ago for ubuntu 12.04+ (note it uses xvd* instead of sd*)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_open_point(existing_device_prefix, new_device_range_start, new_device_range_stop) | |
# existing_device_prefix should be something like 'xvd' or 'md' | |
# new_device_range should be similar to '0' and '9' or 'f' and 'p' | |
possible_attach_points = (new_device_range_start..new_device_range_stop).map do |drive_letter| | |
"/dev/#{existing_device_prefix}#{drive_letter}" | |
end | |
Chef::Log.debug("#{@new_resource} Possible Attach Points: #{possible_attach_points}") | |
# Get a list of in use devices (fold numbered partitions together if looking at disk drives) | |
existing_devices = `ls /dev/#{existing_device_prefix}*`.split().each do |existing_device| | |
if existing_device_prefix.include? "xvd" | |
existing_device.gsub!(/[0-9]/, "") | |
end | |
end | |
Chef::Log.debug("#{@new_resource} Existing Devices: #{existing_devices}") | |
existing_devices.uniq! | |
# Return an array of all open attach points | |
free_points = possible_attach_points - existing_devices | |
Chef::Log.debug("#{@new_resource} Free Points: #{free_points}") | |
if not free_points.any? | |
# Fail over to operator control | |
raise "No open mount points in the range /dev/#{existing_device_prefix}[#{new_device_range_start}-#{new_device_range_stop}]" | |
end | |
return free_points | |
end | |
def get_free_attach_point | |
# Linux kernel 3.0+ uses /dev/xvd* instead of /dev/sd* | |
# AWS recommends using mount points f - p, although others are available | |
free_point = get_open_point("xvd", "f", "p").first | |
Chef::Log.debug("#{@new_resource} Free Attach Point: #{free_point}") | |
return free_point | |
end | |
def get_free_raid_point | |
# If we have over 10 raids per machine, god help us | |
free_point = get_open_point("md", "0", "9").first | |
Chef::Log.debug("#{@new_resource} Free Raid Point: #{free_point}") | |
return free_point | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment