FreeBSD上,在设备插入(例如插了优盘)之后,根据信息自动挂载分区的脚本。

配合devd使用。

#!/usr/bin/perl

sub logmsg
{
        my ($msg) = @_;
        $msg =~ s/'/"/g;
        system("logger '[partmount] $msg'");
}

sub query_dev
{
        my ($name) = @_;
        $namelen = 0;
        $ret = "";
        $devs = `gpart list | grep Name`;
        foreach $i (split(/\n/, $devs))
        {
                $i =~ m/.*?Name: (.*)$/;
                $dev = $1;
                $j =`glabel status $dev 2>&1`;
                if ($? eq 0)
                {
                        foreach $line (split(/\n/, $j))
                        {
                                if ($line =~ m/^$name /)
                                {
                                        if (substr($line, 0, $namelen) == $name)
                                        {
                                                # In fact, maybe there are several
                                                # devices with the same label...
                                                # But what can we do...
                                                $ret = $dev;
                                        }
                                }
                                elsif ($line =~ m/^( *Name) *Status *Components$/)
                                {
                                        $namepart = $1;
                                        $namelen = length($namepart);
                                }
                        }
                }
        }
        return $ret;
}

if (not $ARGV[0] =~ m!(.*)?/(.*)!)
{
        print "Format error.";
        exit(1);
}

$parttype = $1;
$partname = $2;

$devname = query_dev($ARGV[0]);

$succ_sound = "/usr/local/share/wesnoth/sounds/arrive.wav";

$err_sound = "/usr/local/share/wesnoth/sounds/bell.wav";

logmsg("$partname inserted, type: $parttype, device: $devname");

if ($devname =~ m/^ad0/)
{
        $result = "";
        $retcode = -3;
} elsif ($parttype =~ m/ntfs/)
{
        mkdir("/mnt/$partname");
        $result = `/usr/local/bin/ntfs-3g -o uid=1001,umask=022,fmask=133 /dev/$parttype/$partname /mnt/$partname 2>&1`;
        $retcode = $?;
} elsif ($parttype =~ m/msdosfs/)
{
        if ($partname =~ m/IPOD/)
        {
                $result = "";
                $retcode = -2;
        }
        else
        {
                mkdir("/mnt/$partname");
                $result = `/sbin/mount_msdosfs -m 775 -L zh_CN.UTF-8 -u 1001 /dev/$parttype/$partname /mnt/$partname 2>&1`;
                $retcode = $?;
        }
}

if ($retcode == 0) {
        logmsg("Mount successed.");
        system("/usr/local/bin/mplayer -really-quiet $succ_sound");
}
elsif ($retcode == -3) {
        logmsg("Local partition found. Ignored.\n");
}
elsif ($retcode == -2) {
        logmsg("Found iPod, ejecting.\n");
        system("/sbin/camcontrol eject $devname");
}
elsif ($retcode == -1) {
        logmsg("failed to execute: $!\n");
}
elsif ($retcode & 127) {
        logmsg("Mount failed. child died with signal " . ($retcode & 127) . " , " 
                . (($retcode & 128) ? 'with' : 'without') . " coredump\n");
        logmsg("Child output: $result");
        system("/usr/local/bin/mplayer -really-quiet $err_sound");
}
else {
        logmsg("child exited with value " . ($retcode >> 8) . "\n");
        logmsg("Child output: $result");
        system("/usr/local/bin/mplayer -really-quiet $err_sound");
}