[Pkg-puppet-devel] [facter] 274/352: (FACT-234) Add blockdevice uuid fact

Stig Sandbeck Mathisen ssm at debian.org
Sun Apr 6 22:21:52 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit f7b09c73dbe8e8fc67594986f11e44821d4e5ad4
Author: Chris Portman <chris.portman at optusnet.com.au>
Date:   Fri Nov 22 23:51:12 2013 +1100

    (FACT-234) Add blockdevice uuid fact
    
    Add facts to show the uuid of partitions.
    
    Looks for the partition of block devices found in /sys/block/<device>/<device>*
    Then looks for the a link to the partition device in /dev/disk/by-uuid.  The link name is the UUID.
    
    JIRA: FACT-234
---
 lib/facter/blockdevices.rb     | 55 ++++++++++++++++++++++++++++++++++++---
 spec/unit/blockdevices_spec.rb | 58 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 109 insertions(+), 4 deletions(-)

diff --git a/lib/facter/blockdevices.rb b/lib/facter/blockdevices.rb
index e0ddc48..28da29f 100644
--- a/lib/facter/blockdevices.rb
+++ b/lib/facter/blockdevices.rb
@@ -34,7 +34,6 @@
 #   Only supports Linux 2.6+ at this time, due to the reliance on sysfs
 #
 
-
 # Fact: blockdevices
 #
 # Purpose:
@@ -47,6 +46,30 @@
 #   Block devices must have been identified using sysfs information
 #
 
+# Fact: blockdevice_<devicename>_partitions
+#
+# Purpose:
+#   Returns a comma separated list of partitions on the block device.
+#
+# Resolution:
+#   Parses the contents of /sys/block/<device/<device>*
+#
+# Caveats:
+#   Only supports Linux 2.6+ at this time, due to the reliance on sysfs
+#
+
+# Fact: blockdevice_<devicename><partition>_uuid
+#
+# Purpose:
+#   Returns the UUID of the partitions on blockdevices.
+#
+# Resolution:
+#   Parses /dev/disk/by-uuid and resolves the links back to the partitions in /dev
+#
+# Caveats:
+#   Only supports Linux 2.6+ at this time, due to the reliance on sysfs
+#
+
 # Author: Jason Gill <jasongill at gmail.com>
 
 require 'facter'
@@ -72,6 +95,8 @@ if Facter.value(:kernel) == 'Linux'
       sizefile = sysfs_block_directory + device + "/size"
       vendorfile = sysfs_device_directory + "/vendor"
       modelfile = sysfs_device_directory + "/model"
+      partitions = Dir.glob(sysfs_block_directory + device + "/#{device}*").map { |d| File.basename(d) }
+      devdisk_by_uuid_directory = '/dev/disk/by-uuid/'
 
       if File.exist?(sizefile)
         Facter.add("blockdevice_#{device}_size".to_sym) do
@@ -91,8 +116,33 @@ if Facter.value(:kernel) == 'Linux'
         end
       end
 
-    end
+      unless partitions.empty?
+        Facter.add("blockdevice_#{device}_partitions") do
+          setcode { partitions.join(',') }
+        end
+      end
 
+      partitions.each do |part|
+        Facter.add("blockdevice_#{part}_uuid") do
+          setcode do
+            uuid = nil
+            if File.directory?(devdisk_by_uuid_directory)
+              Dir.entries(devdisk_by_uuid_directory).each do |file|
+                qualified_file = "#{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/)#{part}$])
+
+                uuid = file
+              end
+            end
+            uuid
+          end
+        end
+      end
+    end
   end
 
   # Return a comma-seperated list of block devices found
@@ -101,5 +151,4 @@ if Facter.value(:kernel) == 'Linux'
       setcode { blockdevices.sort.join(',') }
     end
   end
-
 end
diff --git a/spec/unit/blockdevices_spec.rb b/spec/unit/blockdevices_spec.rb
index 54620b8..97ef493 100644
--- a/spec/unit/blockdevices_spec.rb
+++ b/spec/unit/blockdevices_spec.rb
@@ -47,6 +47,44 @@ describe "Block device facts" do
             IO.stubs(:read).with(stubdir + "/device/vendor").returns(vendor) if vendor
             IO.stubs(:read).with(stubdir + "/device/model").returns(model) if model
           end
+
+          #Stubs relating to the Partition UUID facts
+          File.stubs(:exist?).with('/dev/disk/by-uuid/').returns(true)
+
+          Dir.stubs(:glob).with("/sys/block/hda/hda*").returns([])
+
+          Dir.stubs(:glob).with("/sys/block/sda/sda*").returns([
+            '/sys/block/sda/sda1',
+            '/sys/block/sda/sda2',
+          ])
+
+          Dir.stubs(:glob).with("/sys/block/sdb/sdb*").returns([
+            '/sys/block/sda/sdb1',
+            '/sys/block/sda/sdb2',
+          ])
+
+          File.stubs(:directory?).returns(true)
+          
+          Dir.stubs(:entries).with('/dev/disk/by-uuid/').returns([
+            ".",                                    #wont match the length requirements
+            "..",                                   #wont match the length requirements
+            "11111111-1111-1111-1111-111111111111", #Should be valid
+            "22222222-2222-2222-2222-222222222222", #Should be valid
+            "33333333-3333-3333-3333-333333333333", #Should be valid
+            "44444444-4444-4444-4444-444444444444", #Wont match the link regex
+            "55555555-5555-5555-5555-555555555555"  #Wont be a symlink
+          ])
+
+          File.stubs(:readlink).with('/dev/disk/by-uuid/11111111-1111-1111-1111-111111111111').returns('../../sda1')
+          File.stubs(:readlink).with('/dev/disk/by-uuid/22222222-2222-2222-2222-222222222222').returns('../../sda2')
+          File.stubs(:readlink).with('/dev/disk/by-uuid/33333333-3333-3333-3333-333333333333').returns('../../sdb1')
+          File.stubs(:readlink).with('/dev/disk/by-uuid/44444444-4444-4444-4444-444444444444').returns('/dont/match/regex/sdb2')
+
+          File.stubs(:symlink?).with('/dev/disk/by-uuid/11111111-1111-1111-1111-111111111111').returns(true)
+          File.stubs(:symlink?).with('/dev/disk/by-uuid/22222222-2222-2222-2222-222222222222').returns(true)
+          File.stubs(:symlink?).with('/dev/disk/by-uuid/33333333-3333-3333-3333-333333333333').returns(true)
+          File.stubs(:symlink?).with('/dev/disk/by-uuid/44444444-4444-4444-4444-444444444444').returns(true)
+          File.stubs(:symlink?).with('/dev/disk/by-uuid/55555555-5555-5555-5555-555555555555').returns(false)
         end
 
         it "should report three block devices, hda, sda, and sdb, with accurate information from sda and sdb, and without invalid . or .. entries" do
@@ -64,16 +102,32 @@ describe "Block device facts" do
             Facter.fact("blockdevice_#{device}_size".to_sym).should_not == nil
             Facter.fact("blockdevice_#{device}_vendor".to_sym).should_not == nil
             Facter.fact("blockdevice_#{device}_model".to_sym).should_not == nil
+            Facter.fact("blockdevice_#{device}_partitions".to_sym).should_not == nil
           end
 
           Facter.fact(:blockdevice_sda_model).value.should == "WDC WD5000AAKS-0"
           Facter.fact(:blockdevice_sda_vendor).value.should == "ATA"
           Facter.fact(:blockdevice_sda_size).value.should == 500107862016
+          Facter.fact(:blockdevice_sda_partitions).value.should == 'sda1,sda2'
 
           Facter.fact(:blockdevice_sdb_model).value.should == "PERC H700"
           Facter.fact(:blockdevice_sdb_vendor).value.should == "DELL"
           Facter.fact(:blockdevice_sdb_size).value.should == 4499246678016
+          Facter.fact(:blockdevice_sdb_partitions).value.should == 'sdb1,sdb2'  
+
+          #These partitions should have a UUID
+          %w{ sda1 sda2 sdb1 }.each do |d|
+            Facter.value("blockdevice_#{d}_uuid".to_sym).should_not == nil
+          end
 
+          #These should not create a UUID fact
+          [ ".", "..", "sdb2" ].each do |d|
+            Facter.value("partition_#{d}_uuid".to_sym).should == nil
+          end
+
+          Facter.fact(:blockdevice_sda1_uuid).value.should == "11111111-1111-1111-1111-111111111111"
+          Facter.fact(:blockdevice_sda2_uuid).value.should == "22222222-2222-2222-2222-222222222222"
+          Facter.fact(:blockdevice_sdb1_uuid).value.should == "33333333-3333-3333-3333-333333333333"
         end
 
       end
@@ -88,6 +142,9 @@ describe "Block device facts" do
           File.stubs(:exist?).with("/sys/block/../device").returns(false)
           File.stubs(:exist?).with("/sys/block/xda/device").returns(false)
           File.stubs(:exist?).with("/sys/block/ydb/device").returns(false)
+
+          #This is here to surpress errors when Dir.entries is run in relation to uuids
+          Dir.stubs(:entries).with('/dev/disk/by-uuid/').returns([])
         end
 
         it "should not exist with invalid entries in /sys/block" do
@@ -104,6 +161,5 @@ describe "Block device facts" do
       end
 
     end
-
   end
 end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git



More information about the Pkg-puppet-devel mailing list