#!/usr/bin/perl
#
# scan system for a hfs disk and mount them if /vasm/<label> is present
#
# ATTENTION !
#    This is linux specific tool. Should be substituted with
#
#    RHEL 5u3
# assumption:
#   /sbin/parted is installed
#


use strict;
use Data::Dumper;

#----------------------------------- external calls
my $PARTED   ='/sbin/parted';
my $DF       ='/bin/df -P';
my $MOUNT='/bin/mount';
my $UMOUNT='/bin/umount';


# MAIN ===========================================================

#-------------------------------- get list from parted
# followinbg output is expected from parted -l:
#Disk /dev/sda: 36.4GB
#Sector size (logical/physical): 512B/512B
#Partition Table: msdos
#
#Number  Start   End     Size    Type     File system  Flags
# 1      32.3kB  107MB   107MB   primary  ext2         boot
# 2      107MB   36.4GB  36.3GB  primary               lvm
#
#Model: Ext Hard  Disk (scsi)
#Disk /dev/sdc: 500GB
#Sector size (logical/physical): 512B/512B
#Partition Table: gpt
#
#Number  Start   End    Size   File system  Name                  Flags
# 1      20.5kB  210MB  210MB  fat32        EFI System Partition  boot 
# 2      210MB   250GB  250GB  hfs+         va-14001                   
# 3      250GB   500GB  250GB  hfs+         va-14002                   

my %disks;
if( -x $PARTED and open(PLIST,"$PARTED -s -l 2>/dev/null|")) {    # only if parted is available
 my @plist=<PLIST>;
 close PLIST;
 my $dev='';
 foreach(@plist) {
   $dev=$1 if m|^Disk\s(/.+):|;
   next if not /^\s*(\d+)\s.+\shfs\+\s+(va-\d+)/;
   $disks{$2}="$dev$1" if ! exists $disks{$2};   # ignore if name is not unique
 }
}

print Dumper \%disks;
#-------------------------------- check if mountpoints are present

foreach (keys %disks) {
  my $mnt="/vasm/$_";
  next if not -d $mnt;        # skip if no mount point
  `$DF $mnt | grep -sq $mnt`;
  next if not $?;             # skip if already mounted
  `$UMOUNT $disks{$_} 2>/dev/null`; # in case if mounted
  `$MOUNT  $disks{$_} $mnt`;
  `$DF $mnt | grep -sq $mnt`;
  if($?) {
    print "ERROR: Cannot mount $disks{$_} $mnt\n";
  }else  {
    print "SUCCESS: $disks{$_} is mounted to $mnt\n";
  }
}


