以前、HP ProLiant MicroServer N54LにFreeNAS 9をインストール、プラグインを利用してDLNA/UPnPサーバを稼働させました
Install DLNA/UPnP Server plugin on FreeNAS 9
http://www.rootlinks.net/2013/10/26/install-dlnaupnp-server-plugin-on-freenas-9/
いくつかコンテンツをコピーしてWindows Media Playerで動作確認をしたのですが、追加コンテンツをコピーしても、いつまでたっても追加分がWindows Media Playerに表示されませんでした
色々と調べるとコンテンツの登録にはDBの更新が必要でした
その仕組として設定ファイルに”inotify=yes”と記入することで通常はコンテンツが追加されると自動的にDBを更新することができます
プラグインでインストールしたminiDLNAサーバの設定ファイルを確認すると
1 2 3 4 5 6 7 8 9 10 |
; html-script: false ] root@dlna_1:/ # cat /usr/pbi/minidlna-amd64/etc/minidlna.conf media_dir=/media port=8200 inotify=yes enable_tivo=no strict_dlna=no notify_interval=895 friendly_name=FreeNAS DLNA |
と”inotify=yes”の記載がデフォルトであります。しかしこれでもDBの更新が行われません
実はinotifyはLinux Kernelの機能のようでFreeBSDでは動作しないとのこと。同様の機能がFreeBSDにはkqueueがあるようですがminiDLNAは対応していない
それで設定画面に[Rescan on (re)start]があるんですね
miniDLNAサーバの再起動でコンテンツをスキャンしてDBを更新するわけです
となるとコンテンツを追加、削除するたびにサービスの再起動が必要になるのか
ちょっと残念と思っていたらFreeNASのフォーラムにDBを自動で更新するスクリプトがありました
MiniDLNA Automatic Scan – Fix
http://forums.freenas.org/threads/minidlna-automatic-scan-fix.9312/
早々に設定してみます
- jexec dlna_1 /bin/csh
- wait_onコマンドのインストール
- スクリプトscanmediaの作成
- scanmediaに実行権限追加
- 起動時に自動実行の設定
- FreeNAS管理画面から再起動
Shellからdlna_1に入ります
1 2 |
; html-script: false ][root@freenas] ~# jexec dlna_1 /bin/csh root@dlna_1:/ # |
wait_onコマンドはファイルやディレクトリの変更を通知してくれるコマンドのようです
wait_on(1) – FreeBSD Man Pages
http://www.manualpages.de/FreeBSD/FreeBSD-ports-9.0-RELEASE/man1/wait_on.1.html
1 2 3 4 5 6 7 |
; html-script: false ] root@dlna_1:/ # pkg_add -r wait_on Fetching ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-9-stable/Latest/wait_on.tbz ... Done. root@dlna_1:/ # rehash root@dlna_1:/ # wait_on usage: wait_on [-chivw] [-t timeout] file1 [...] |
スクリプトのscanmediaを作成します
取り敢えずコピー&ペーストで大丈夫と思いますがメディアコンテンツのマウント先が異なる場合はMediaLocation=”/media”の部分を書き換えて下さい
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
; html-script: false ] root@dlna_1:/ # cd /etc/rc.d root@dlna_1:/etc/rc.d # vi scanmedia root@dlna_1:/etc/rc.d # cat scanmedia #!/bin/sh # File name 'scanmedia' # Place this file into /etc/rc.d # Edit /etc/rc.conf to include scanmedia_enable="YES" # Things to know... The purpose of this script is to trigger a rescan of the # media for the minidlna plugin. This uses the "wait_on" command which # although works, it has it's limitations: # 1) It it rescans on the detection of a change, not the completion of a change. # 2) It has a limitation of the directory string length so I have implemented #an alternate method for scanning however it does actively read the drives #so if you wanted your drives to sleep, this method will prevent sleeping #from occurring and I strongly recommend you work on your directory structure. . /etc/rc.subr name="scanmedia" rcvar=scanmedia_enable PATH="$PATH:/usr/local/bin" start_cmd="${name}_start" stop_cmd=":" load_rc_config $name eval "${rcvar}=\${${rcvar}:-'NO'}" # Variables used in this script... # RedAlert = Maximum string length wait_on can handle RedAlert=261649 # YellowAlert = User selected warning point. Looking to use this to enable the # flashing warning light in the GUI. YellowAlert=255000 # TriggeredDelay = The amount of time in seconds to delay rescanning from the point of a # change being detected. The default is 300 seconds (5 minutes). Change to your liking # and think of this as how long it takes you to write a large file (movie) to your NAS. TriggeredDelay=300 # AlternateScanRate = The amount of time in seconds between polling the media for changes. # Default value is 1800 seconds (30 minutes). Change to your liking. AlternateScanRate=1800 # ScanMode = Normal or Alternate method. Default is '0' for Normal Mode. This changes # in the script based on the actual directory string length vs the RedAlert value. ScanMode=0 # MediaLocation = The location your want to scan for your media. The default is "/media". MediaLocation="/media" scanmedia_normal() { while [ "$ScanMode" -eq "0" ] ; do # First lets locate all the subdirectories. Since the minidlna plugin only # allows one path for media we will use the path defined above (default=/media) directories=$(find $MediaLocation -type d -print0 | xargs -0 -I % echo -n '"%" ') # Check on maximum length of directory string. if [ ${#directories} -gt $YellowAlert ]; then if [ ${#directories} -gt $RedAlert ]; then date > /minidlnaerror.log echo "RED ALERT! You have exceeded the maximum directory string length of 261649 and you are currently at" >> /minidlnaerror.log echo ${#directories}". The alternate method is in use and you need to shorten the directory names or rearange your folders" >> /minidlnaerror.log echo "to restore the normal scanning method." >> /minidlnaerror.log # Change from normal scan mode to alternate scan mode. ScanMode=1 else date > /minidlnaerror.log echo "YELLOW ALERT! The maximum directory string length is 261649 and you are currently at" >> /minidlnaerror.log echo ${#directories}". You should shorten the directory names or rearange your folders" >> /minidlnaerror.log echo "to remove this warning." >> /minidlnaerror.log fi fi if [ "$ScanMode" -eq "0" ] then # Wait_on will trigger on any event which writes to the directories listed. csh -c "wait_on -w $directories" # Now let's call the rescanning routine scanmedia_rescan fi done } scanmedia_rescan() { # This routine pauses for the user specified delay (default 5 mins) before killing # the minidlna service and restarting it. sleep $TriggeredDelay # We must stop the minidlna service in the GUI first, then wait 10 seconds (probably overkill) # and then kill the service. service minidlna onestop sleep 10 pkill minidlna # We wait 10 seconds to allow the service lots of time to wrap up. sleep 10 # And now to start up the service again. service minidlna onestart } scanmedia_alternate() { while [ "$ScanMode" -eq "1" ] ; do #Lets check to see if we can go back to normal_scan mode. directories=$(find $MediaLocation -type d -print0 | xargs -0 -I % echo -n '"%" ') if [ ${#directories} -lt $RedAlert ] then ScanMode=0 fi # We list all the files and save as a file in memory, hopefully the file doesn't get too # big to cause an issue. If it does then remove the '/var/' from the below lines. ls -RlT $MediaLocation > /var/altscan1.dat sleep $AlternateScanRate ls -RlT $MediaLocation > /var/altscan2.dat # diff return 0 if files are identical, 1 if they are different, 2 if one of the files cannot open. diff -q /var/altscan1.dat /var/altscan2.dat # We don't really care about cases 0 and 2 but it's nice to have for troubleshooting. They # are commented out right now. case "$?" in #"0") echo "Files Identical";; "1") scanmedia_rescan;; #"2") echo "Failed to open one of the files";; esac done } scanmedia_start() { while :; do if [ "$ScanMode" -eq "0" ] then scanmedia_normal else scanmedia_alternate fi done } scanmedia_start run_rc_command "$1" |
scanmediaに実行権限追加を追加します
1 2 3 |
; html-script: false ]root@dlna_1:/etc/rc.d # chmod +x scanmedia root@dlna_1:/etc/rc.d # ls -l scanmedia -rwxr-xr-x 1 root wheel 4970 Nov 10 17:58 scanmedia |
起動時にスクリプトscanmediaが自動実行するように設定します
/etc/rc.confにscanmedia_enable=”YES”を追加します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
; html-script: false ] root@dlna_1:/etc/rc.d # cd .. root@dlna_1:/etc # vi rc.conf root@dlna_1:/etc # cat rc.conf portmap_enable="NO" sshd_enable="YES" sendmail_enable="NO" sendmail_submit_enable="NO" sendmail_outbound_enable="NO" sendmail_msp_queue_enable="NO" hostname="dlna_1" devfs_enable="YES" devfs_system_ruleset="devfsrules_common" minidlna_enable="YES" minidlna_flags="-R " scanmedia_enable="YES" |
FreeNASの管理画面からminiDLNAを再起動します
試しにコンテンツを追加して5分後にWindows Media Playerで確認すると追加されたコンテンツが表示されていました
取り敢えず使えそうですが、今度はFreeNAS管理画面からプラグインdlna_1のサービスの停止、起動が[Some error occurred]と表示されて制御できなくなってしまいました