Module: Facter::Util::Partitions::Linux

Defined in:
lib/facter/util/partitions/linux.rb

Constant Summary collapse

SYSFS_BLOCK_DIRECTORY =

Only Linux 2.6+ kernels support sysfs which is required to easily get device details

'/sys/block/'
DEVDISK_BY_UUID_DIRECTORY =
'/dev/disk/by-uuid/'

Class Method Summary collapse

Class Method Details

.filesystem(partition) ⇒ Object



52
53
54
55
56
# File 'lib/facter/util/partitions/linux.rb', line 52

def self.filesystem(partition)
  if Facter::Core::Execution.which('blkid')
    Facter::Core::Execution.exec("blkid #{File.join('/dev', partition)}").scan(/TYPE="([^"]*)"/).flatten.first
  end
end

.listObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/facter/util/partitions/linux.rb', line 7

def self.list
  if File.exist?(SYSFS_BLOCK_DIRECTORY)
    devices = Dir.entries(SYSFS_BLOCK_DIRECTORY).select { |d| File.exist?( SYSFS_BLOCK_DIRECTORY + d + "/device" ) }
  
    if devices.empty?
      []
    else
      devices.collect do |device|
        Dir.glob( SYSFS_BLOCK_DIRECTORY + device + "/#{device}*" ).collect do |d|
          File.basename(d)
        end
      end.flatten
    end
  else
    []
  end
end

.mount(partition) ⇒ Object



46
47
48
49
50
# File 'lib/facter/util/partitions/linux.rb', line 46

def self.mount(partition)
  if Facter::Core::Execution.which('mount')
    Facter::Core::Execution.exec('mount').scan(/\/dev\/#{partition}\son\s(\S+)/).flatten.first
  end
end

.size(partition) ⇒ Object



42
43
44
# File 'lib/facter/util/partitions/linux.rb', line 42

def self.size(partition)
  read_size(partition)
end

.uuid(partition) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/facter/util/partitions/linux.rb', line 25

def self.uuid(partition)
  uuid = nil
  if File.exist?(DEVDISK_BY_UUID_DIRECTORY)
    Dir.entries(DEVDISK_BY_UUID_DIRECTORY).each do |file|
      qualified_file = File.join(DEVDISK_BY_UUID_DIRECTORY, file)

      #A uuid is 16 octets long (RFC4122) which is 32hex chars + 4 '-'s
      next unless file.length == 36
      next unless File.symlink?(qualified_file)
      next unless File.readlink(qualified_file).match(%r[(?:\.\./\.\./|/dev/)#{partition}$])

      uuid = file
    end
  end
  uuid
end