FreeBSD上,在设备消失之后(例如拔下优盘之后),检查/mnt,并且清理下面内容的脚本。

配合devd使用。

#!/usr/bin/perl

sub logmsg
{
        my ($msg) = @_;
        $msg =~ s/'/"/g;
        system("logger '[mntclean] $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;

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

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

logmsg("$partname removed, type: $parttype");

#foreach (glob("/mnt/*"))
#{
#       print $_;
#}
#

system("/bin/rm -d /mnt/" . $partname);