Advertisement
digimer

Untitled

Dec 28th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # This is a wrapper for dmidecode that will return the configured UUID
  4. # when 'dmidecode -s system-uuid' is called. Otherwise, it will call dmidecode
  5. # and return as appropriate.
  6. #
  7.  
  8. ## slightly edited to leave @ARGV intact, do open "-|" and exec {} $0, @ARGV,
  9. ## by Lars Ellenberg, untested, all typos mine :)
  10.  
  11. use strict;
  12. use warnings;
  13. use IO::Handle;
  14.  
  15. # Desired System UUID
  16. my $uuid;
  17. my $exit=0;
  18. my $sub=0;
  19.  
  20. $sub = (@ARGV == 3 and
  21. $ARGV[0] eq "-q" and
  22. $ARGV[1] eq "-t" and
  23. $ARGV[2] eq "0,1,4,17");
  24.  
  25. # Read the UUID from /etc/libvirt/libvirtd.conf.
  26. my $sc="</etc/libvirt/libvirtd.conf";
  27. my $fh=IO::Handle->new();
  28. open ($fh, $sc) or die "$!";
  29. while (<$fh>)
  30. {
  31. chomp;
  32. my $line=$_;
  33. next if $line =~ /^#/;
  34. if ($line =~ /host_uuid/)
  35. {
  36. ($uuid)=($line =~ /"(.*?)"/);
  37. }
  38. }
  39. $fh->close();
  40.  
  41. # Call the real dmidecode
  42. undef $fh;
  43. my $pid = open($fh, "-|");
  44. die unless defined $pid;
  45.  
  46. if ($pid == 0) {
  47. # child
  48. exec { "/usr/sbin/dmidecode.orig" } $0, @ARGV;
  49. die; # not reached
  50. }
  51.  
  52. # parent
  53. while (<$fh>)
  54. {
  55. chomp;
  56. my $line=$_;
  57. # If I found a UUID, this line is the UUID and I am doing a
  58. # substitution, well, do it already.
  59. if (($uuid) && ($line=~/(\s+)UUID: /) && ($sub))
  60. {
  61. my $space=$1;
  62. print "${space}UUID: $uuid\n";
  63. }
  64. else
  65. {
  66. print $line, "\n";
  67. }
  68. }
  69. $fh->close();
  70. $exit=$?;
  71.  
  72. exit($exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement