Ubuntu's autofs, or automounter, have the script auto.smb to mount Windows shares with share names by accessing for example "/smb/minitrue/tmp". It works OK with samba shares, but NG with Windows shares according to some errors in the script. Until Today, I wrote direct settings in auto.xxx as follows for all shares instead of using the script.
minitrue -fstype=cifs,rw,username=myUser,passwd=myPasswd,iocharset=utf8 ://minitrue/tmp
But I feel very inconvenient with it, and try to fix it.
I found a solution for the issue in the site, but it does not work correctly as is.
The problems are as follows:
- auto.smb uses smbclient for listing services and it executes with "-N" (=no-pass). It is OK for servers providing the listing function for anonymous account, but causes an error with those require an authentication. Therefore we should remove "-N" from options and add authentication parameters like "-credentials".
- mount seems to be done with mount.cifs and it does not allow to use credential file but direct parameters like username, password or domain.
Then the fixed script is as follows.
#!/bin/bash
# This file must be executable to work! chmod 755
key="$1"
opts="-fstype=cifs,rw,iocharset=utf8"
creddir="/etc/auto.credentials"
credfile=""
host=`echo $key | sed 's!^//!!;s!/.*$!!'`
if [[ "$key" =~ '/' ]]; then
path=${key#*/}
fi
if [ -z "$credfile" ]; then
# Search for credentials file
if [ -n "$path" ]; then
if [[ "$path" =~ '/' ]]; then
share=${path%%/*}
else
share=$path
fi
fi
# First look for $creddir/$host.$share then for $creddir/$host
if [ -n "$share" ]; then
if [ -e "$creddir/$host.$share" ]; then
credfile="$creddir/$host.$share"
elif [ -e "$creddir/$host" ]; then
credfile="$creddir/$host"
fi
elif [ -e "$creddir/$host" ]; then
credfile="$creddir/$host"
else
credfile="$creddir/defaults"
fi
fi
smclopts=""
if [ "z$credfile" != "z" ]; then
smclopts="$smclopts --authentication-file=$credfile -gL"
credopts=""
username=`egrep '^username' $credfile | sed 's!^.*= !!;s! $!!'`
password=`egrep '^password' $credfile | sed 's!^.*= !!;s! $!!'`
domain=``egrep '^domain' $credfile | sed 's!^.*= !!;s! $!!'`
if [ "z$username" != "z" ]; then
[ "z$credopts" = "z" ] \
&& credopts="username=\"$username\"" \
|| credopts="$credopts,username=\"$username\""
fi
if [ "z$password" != "z" ]; then
[ "z$credopts" = "z" ] \
&& credopts="password=\"$password\"" \
|| credopts="$credopts,password=\"$password\""
fi
if [ "z$domain" != "z" ]; then
[ "z$credopts" = "z" ] \
&& credopts="domain=\"$domain\"" \
|| credopts="$credopts,domain=\"$domain\""
fi
opts="$opts,$credopts"
else
smclopts="$smclopts -gNL"
fi
for P in /bin /sbin /usr/bin /usr/sbin
do
if [ -x $P/smbclient ]
then
SMBCLIENT=$P/smbclient
break
fi
done
[ -x $SMBCLIENT ] || exit 1
$SMBCLIENT $smclopts $key 2>/dev/null| awk -v key="$key" -v opts="$opts" -F'|' -- '
BEGIN { ORS=""; first=1 }
/Disk/ {
if (first)
print opts; first=0
dir = $2
loc = $2
# Enclose mount dir and location in quotes
# Double quote "$" in location as it is special
gsub(/\$$/, "\\$", loc);
gsub(/\&/,"\\\\&",loc)
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
}
END { if (!first) print "\n"; else exit 1 }
'
After
this fix, we should prepare credential file with name line "minitrue",
"minitrue.tmp" and "defaults" in the directory "/etc/auto.credentials/".
The format of the files can be refered by "man smbclient".
The file defaults should be like as follows,
username = myUser
password = myPassword
domain = myDomain
The file must be create for the shared path, if username or password is different from the defaults.
日本語で追加
automountのsmbマウントがauto.smbでできない場合の修正方法を説明します。(できていれば、/smb/host/shareでアクセスすると、自動でマウントされます)
まとめ
- オリジナルのauto.smbは、サービスのリストを得るために、smbclientを無名アクセスで行っています。このため、通常のWindows共有で蹴られます。認証情報のオプション(-credentials)を付けて実行する必要があります。
- マウント自体は、mount.cifsを使用しています。こちらにも同じ認証情報を渡さないとなりませんが、ドメイン、ユーザ、パスワードで分けて渡します。
- 認証情報は、相手サーバによって違うのが普通なので、複数の認証ファイルを使い分けるようにしないとなりません。
結局のところ、
このサイト のを参考にして、上記スクリプトにしました。利用には、以下のようにします。
- /etc/auto.credentials/に認証ファイルを置きます。
共有名が、\\host\share であれば、host.shareのファイル、
share間で共通であれば、hostのファイル、
デフォルトはdefaultsのファイルです。 - マッチングの優先度は、細かく指定されているほう(host.share > host > defaults)になります。
- 書式は、
username = 認証ユーザ名
password = このユーザのパスワード
domain = この共有のドメイン