Skip to content

Instantly share code, notes, and snippets.

@nkwhr
Last active February 2, 2022 13:55
Show Gist options
  • Save nkwhr/9952213 to your computer and use it in GitHub Desktop.
Save nkwhr/9952213 to your computer and use it in GitHub Desktop.
a serverspec resource type for checking partitions.
module Serverspec
module Type
class Partition < Base
def initialize(partition, type)
@name = partition
@partition_table = {}
case type
when 'cylinder'
options = "-l"
when 'sector'
options = "-lu"
else
raise "Unkown type: #{type}"
end
ret = backend.run_command("fdisk #{options} | grep ^/")
ret[:stdout].each_line do |line|
boot_flag = line.include? '*'
partition, start_point, end_point, blocks, id, system \
= line.chomp.gsub('*','').split(' ', 6)
@partition_table[partition] = {
:boot_flag => boot_flag,
:start_point => start_point.to_i,
:end_point => end_point.to_i,
:blocks => blocks.to_i,
:id => id.to_i,
:system => system,
}
end
end
def available?
@partition_table.has_key?(@name)
end
def bootable?
@partition_table[@name][:boot_flag]
end
def start_point
@partition_table[@name][:start_point]
end
def end_point
@partition_table[@name][:end_point]
end
def blocks
@partition_table[@name][:blocks]
end
def id
@partition_table[@name][:id]
end
def system
@partition_table[@name][:system]
end
end
def partition(partition, type='cylinder')
Partition.new(partition, type)
end
end
end
include Serverspec::Type
require 'spec_helper'
require 'path/to/partition'
describe partition('/dev/sda1') do
its(:start_point) { should eq 1 }
its(:end_point) { should eq 131 }
its(:id) { should eq 82 }
end
describe partition('/dev/sda2', 'sector') do
it { should be_available }
it { should be_bootable }
its(:start_point) { should eq 2104515 }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment