Author Topic: CIFS Mount External Share - Code for Lua start up.  (Read 18312 times)

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2473
  • Karma: +35/-48
  • Life Moves Pretty Fast....
CIFS Mount External Share - Code for Lua start up.
« on: May 18, 2014, 02:41:22 pm »
Hi @akbooer

Just looking at your code for mounting an external CIFS share and I was wondering what to add to have it check for the mount e.g every 30mins and then if the mount (or target share is not there) it should try to mount it again. 

(Example of how this may be of use.. The NAS is off when the Vera restarts/reloads.

Code: [Select]
-- cifs mount for use during startup
-- @akbooer 2014-04-23
--

local function df ()
  local info
  local p = io.popen ("df",'r')
  if p then info = p:read "*a"; p:close () end
  return info
end

local function cifsmount (x)
  local function log (msg) luup.log (("'%s' %s"): format (x.device or '', msg), 'mount') end
  local df = df ()
  if df: find (x.device, 1, true) then
    log "already mounted"
  else
    local cmd = table.concat {"mount -t cifs -o user=", x.user, ",pass=", x.pass, ",nounix,noserverino " , x.device, ' ', x.directory}
    local ok = os.execute (cmd)
    if ok
      then log "mounted OK"
      else log "failed to mount"
    end
  end
end


cifsmount {user=user, pass=pass, device=device, directory=directory}

Also if I wanted to add a prowl notification to this to confirm it is mounted, where is the best place to put it?

Code: [Select]
luup.inet.wget("http://www.prowlapp.com/publicapi/add?apikey=Myapikey&application=Vera+StartUp+Notification&event=Alert&description=Vera+has+restarted.+The+external+NAS+share+was+" .. Status .. "+mapped&priority=1")
..Status.. would be either 'successfully' or 'unsuccessfully'.
« Last Edit: May 18, 2014, 02:47:42 pm by parkerc »

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #1 on: May 18, 2014, 03:26:49 pm »
Well, you'd do something like:
  • make the cifsmount function global
  • use luup.call_delay to set up a call back if the mount failed
  • put the alert status value into the appropriate branch of the return code conditional
...or something along those lines.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Beta Testers
  • Sr. Hero Member
  • *****
  • Posts: 2473
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #2 on: May 18, 2014, 04:31:33 pm »
Thanks

How's this?

Code: [Select]
-- cifs mount for use during startup
-- @akbooer 2014-04-23
--

local function df ()
  local info
  local p = io.popen ("df",'r')
  if p then info = p:read "*a"; p:close () end
  return info
end

local function cifsmount (x)
  local function log (msg) luup.log (("'%s' %s"): format (x.device or '', msg), 'mount') end
  local df = df ()
  if df: find (x.device, 1, true) then
    log "already mounted"
  else
    local cmd = table.concat {"mount -t cifs -o user=", x.user, ",pass=", x.pass, ",nounix,noserverino " , x.device, ' ', x.directory}
    local ok = os.execute (cmd)
    if ok
      then log "mounted OK"
      luup.inet.wget("http://www.prowlapp.com/publicapi/add?apikey=Myapikey&application=Vera+StartUp+Notification&event=Alert&description=Vera+has+restarted.+The+external+NAS+share+was+mounted+successfully&priority=1")
      else log "failed to mount, Vera will try again in 10 mins"
      luup.inet.wget("http://www.prowlapp.com/publicapi/add?apikey=Myapikey&application=Vera+StartUp+Notification&event=Alert&description=Vera+has+restarted.+The+external+NAS+share+was+unable+to+be+mounted&priority=1")
      luup.call_delay("cifsmount",600)
    end
  end
end

Can you remind me of the benefits of global to local, I seem to recall global means the call/function is  always available.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #3 on: May 18, 2014, 04:56:39 pm »
It's a good start.  You do need to remove the local in front of the procedure in order to make it global.  It's a requirement of luup.call_delay that the callback handler is global.

Looking again at this code, I'm not convinced that checking the return status of the os.execute function is adequate to check the mount success.  I think you may need to look at the third return parameter, which is actually the exit code of the command.

There's little to no harm in actually running the callback every, say, 10 minutes regardless of the return status, since the mount command won't be executed if the previous df call indicates that the device is indeed mounted.

I coded a version which has the retry time as an optional parameter, just for generality, but I haven't tested it.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline LightsOn

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +4/-3
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #4 on: July 12, 2014, 06:07:24 pm »
Hi ParkerC and Akbooer,

was there any progress on this - i too can see a good use for this feature and would be interested to use if either of you got it up and running?

Thank you in advance.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #5 on: July 12, 2014, 06:12:30 pm »
Yes, I have the code somewhere, but it will have to wait until tomorrow.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline LightsOn

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +4/-3
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #6 on: July 12, 2014, 06:16:03 pm »
Okay thank you relay appreciate that  :)

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #7 on: July 13, 2014, 03:25:08 am »
I'm not sure that this is significantly different from the @parkerc version, and I haven't been able to test the retry mode (since it always works first time!)

Code: [Select]
-- cifs mount for use during startup
-- @akbooer 2014-04-23
--

local function df ()
  local info
  local p = io.popen ("df",'r')
  if p then info = p:read "*a"; p:close () end
  return info
end

function cifsmount (x)
  local function log (msg) luup.log (("cifsmount: '%s' %s"): format (x.device or '?', msg)) end
  local df = df ()
  if df: find (x.device, 1, true) then
    log "already mounted"
  else
    local cmd = ("mount -t cifs -o user=%s,pass=%s,nounix,noserverino %s %s"): format (x.user, x.pass, x.device, x.directory)
    local ok, term, status = os.execute (cmd)
    log ("mounted status: "..tostring(status))
    if ok
      then log ("mounted OK: "..tostring(ok))
      elseif x.retry then luup.call_delay ("cifsmount", 60 * x.retry)
      else log "failed to mount"
    end
  end
end


local device = "//172.16.42.100/DATAMINE"
local directory = "/dataMine"
local user = "XXX"
local pass = "xxxxxxxxxxx"

-- optional retry gives interval in minutes to retry failed mount attempt
cifsmount {user=user, pass=pass, device=device, directory=directory, retry=30}
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline LightsOn

  • Hero Member
  • *****
  • Posts: 751
  • Karma: +4/-3
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #8 on: July 13, 2014, 04:40:28 am »
Thanks akbooer. Will update my startup lua.

deejc

  • Guest
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #9 on: March 06, 2015, 01:05:15 pm »
HI,
i know this is an old thread but i have just managed to get my edge to map to my nas but it lost the mapping so i am trying to get it automated .. i added this code to my start up Lua but i am getting
Code: [Select]
ERROR : Error in lua for scenes and eventsi ssh'ed in and tried to run the code as a file but get :
Code: [Select]
lua test1.lua
lua: test1.lua:13: attempt to index global 'luup' (a nil value)
stack traceback:
test1.lua:13: in function 'log'
test1.lua:16: in function 'cifsmount'
test1.lua:35: in main chunk
[C]: ?
any ideas ?

added :

opkg install cifsmount gives me
Unknown package 'cifsmount'.
Collected errors:
 * opkg_install_cmd: Cannot install package cifsmount.
« Last Edit: March 06, 2015, 01:13:17 pm by deejc »

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #10 on: March 07, 2015, 04:16:18 pm »
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline HansW

  • Full Member
  • ***
  • Posts: 107
  • Karma: +1/-0
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #11 on: April 05, 2015, 05:24:30 pm »
Hello guys,

I have this code to mount my nas on my ui7 vera 3 but it seems that every luup restart, and I have several each day, create an extra mount. Is my code in error?

Code: [Select]
local function df ()
  local info
  local p = io.popen ("df",'r')
  if p then info = p:read "*a"; p:close () end
  return info
end

function cifsmount (x)
  local function log (msg) luup.log (("cifsmount: '%s' %s"): format (x.device or '?', msg)) end
  local df = df ()
  if df: find (x.device, 1, true) then
    log "already mounted"
  else
    local cmd = ("mount -t cifs -o user=%s,pass=%s,nounix,noserverino %s %s"): format (x.user, x.pass, x.device, x.directory)
    local ok, term, status = os.execute (cmd)
    log ("mounted status: "..tostring(status))
    if ok
      then log ("mounted OK: "..tostring(ok))
      elseif x.retry then luup.call_delay ("cifsmount", 60 * x.retry)
      else log "failed to mount"
    end
  end
end


local device = "//192.168.1.16/Vera"
local directory = "/qnap"
local user = "Vera"
local pass = "xxx"

-- optional retry gives interval in minutes to retry failed mount attempt
cifsmount {user=user, pass=pass, device=device, directory=directory, retry=30}

Thanks for any pointers.

Hans

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #12 on: April 05, 2015, 05:29:18 pm »
What does df output look like for your multiple mounts?

That section of the code should catch previous mounts, but it seems that it isn't.

3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline HansW

  • Full Member
  • ***
  • Posts: 107
  • Karma: +1/-0
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #13 on: April 07, 2015, 03:36:28 am »
Hello Akbooer,

I had the same thought but how do I catch that variable, need to change the code I guess to put it in the log?

Hans

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6196
  • Karma: +276/-70
  • "Less is more"
Re: CIFS Mount External Share - Code for Lua start up.
« Reply #14 on: April 07, 2015, 03:56:21 am »
I was really asking what it looks like if you ssh into Vera and type df at the Unix command prompt.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.