openwrt中luci界面配置说明

来源:互联网 发布:淘宝hd打开怎么变横屏 编辑:程序博客网 时间:2024/06/04 01:13

转载自:http://blog.csdn.net/laoxiao1987/article/details/7867679


1.先在system中添加一个ipv6config

   操作:先在/usr/lib/ lua/luci/controller/admin/system.lua 文件中的index页面中添加 

[php] view plain copy
 print?
  1. entry({"admin""system""ipv6config"}, cbi("admin_system/ipv6config"), "ipv6config", 30).dependent=false  

system.lua页面全部代码

[php] view plain copy
 print?
  1. --[[  
  2. LuCI - Lua Configuration Interface  
  3.   
  4. Copyright 2008 Steven Barth <steven@midlink.org>  
  5. Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>  
  6.   
  7. Licensed under the Apache License, Version 2.0 (the "License");  
  8. you may not use this file except in compliance with the License.  
  9. You may obtain a copy of the License at  
  10.   
  11.     http://www.apache.org/licenses/LICENSE-2.0  
  12.   
  13. $Id: system.lua 7785 2011-10-26 00:58:12Z jow $  
  14. ]]--  
  15.   
  16. module("luci.controller.admin.system", package.seeall)  
  17.   
  18. function index()  
  19.     entry({"admin""system"}, alias("admin""system""system"), _("System"), 30).index = true  
  20.     entry({"admin""system""system"}, cbi("admin_system/system"), _("System"), 1)  
  21.     entry({"admin""system""clock_status"}, call("action_clock_status"))  
  22.   
  23.     entry({"admin""system""admin"}, cbi("admin_system/admin"), _("Administration"), 2)  
  24.   
  25.     if nixio.fs.access("/bin/opkg") then  
  26.         entry({"admin""system""packages"}, call("action_packages"), _("Software"), 10)  
  27.         entry({"admin""system""packages""ipkg"}, form("admin_system/ipkg"))  
  28.     end  
  29.   
  30.     entry({"admin""system""startup"}, form("admin_system/startup"), _("Startup"), 45)  
  31.     entry({"admin""system""crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)  
  32.   
  33.     if nixio.fs.access("/etc/config/fstab") then  
  34.         entry({"admin""system""fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)  
  35.         entry({"admin""system""fstab""mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true  
  36.         entry({"admin""system""fstab""swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true  
  37.     end  
  38.   
  39.     if nixio.fs.access("/sys/class/leds") then  
  40.         entry({"admin""system""leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)  
  41.     end  
  42.     entry({"admin""system""ipv6config"}, cbi("admin_system/ipv6config"), "ipv6config", 30).dependent=false  
  43.   
  44.     entry({"admin""system""flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)  
  45.     entry({"admin""system""flashops""backupfiles"}, form("admin_system/backupfiles"))  
  46.   
  47.     entry({"admin""system""reboot"}, call("action_reboot"), _("Reboot"), 90)  
  48. end  
  49.   
  50. function action_clock_status()  
  51.     local set = tonumber(luci.http.formvalue("set"))  
  52.     if set ~= nil and set > 0 then  
  53.         local date = os.date("*t", set)  
  54.         if date then  
  55.             -- prevent session timeoutby updating mtime  
  56.             nixio.fs.utimes(luci.sauth.sessionpath .. "/" .. luci.dispatcher.context.authsession, set, set)  
  57.   
  58.             luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{  
  59.                 date.year, date.month, date.day, date.hour, date.min, date.sec  
  60.             })  
  61.         end  
  62.     end  
  63.   
  64.     luci.http.prepare_content("application/json")  
  65.     luci.http.write_json({ timestring = os.date("%c") })  
  66. end  
  67.   
  68. function action_packages()  
  69.     local ipkg = require("luci.model.ipkg")  
  70.     local submit = luci.http.formvalue("submit")  
  71.     local changes = false  
  72.     local install = { }  
  73.     local remove  = { }  
  74.     local stdout  = { "" }  
  75.     local stderr  = { "" }  
  76.     local out, err  
  77.   
  78.     -- Display  
  79.     local display = luci.http.formvalue("display"or "installed"  
  80.   
  81.     -- Letter  
  82.     local letter = string.byte(luci.http.formvalue("letter"or "A", 1)  
  83.     letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65  
  84.   
  85.     -- Search query  
  86.     local query = luci.http.formvalue("query")  
  87.     query = (query ~= ''and query or nil  
  88.   
  89.   
  90.     -- Packets to be installed  
  91.     local ninst = submit and luci.http.formvalue("install")  
  92.     local uinst = nil  
  93.   
  94.     -- Install from URL  
  95.     local url = luci.http.formvalue("url")  
  96.     if url and url ~= '' and submit then  
  97.         uinst = url  
  98.     end  
  99.   
  100.     -- Do install  
  101.     if ninst then  
  102.         install[ninst], out, err = ipkg.install(ninst)  
  103.         stdout[#stdout+1] = out  
  104.         stderr[#stderr+1] = err  
  105.         changes = true  
  106.     end  
  107.   
  108.     if uinst then  
  109.         local pkg  
  110.         for pkg in luci.util.imatch(uinst) do  
  111.             install[uinst], out, err = ipkg.install(pkg)  
  112.             stdout[#stdout+1] = out  
  113.             stderr[#stderr+1] = err  
  114.             changes = true  
  115.         end  
  116.     end  
  117.   
  118.     -- Remove packets  
  119.     local rem = submit and luci.http.formvalue("remove")  
  120.     if rem then  
  121.         remove[rem], out, err = ipkg.remove(rem)  
  122.         stdout[#stdout+1] = out  
  123.         stderr[#stderr+1] = err  
  124.         changes = true  
  125.     end  
  126.   
  127.   
  128.     -- Update all packets  
  129.     local update = luci.http.formvalue("update")  
  130.     if update then  
  131.         update, out, err = ipkg.update()  
  132.         stdout[#stdout+1] = out  
  133.         stderr[#stderr+1] = err  
  134.     end  
  135.   
  136.   
  137.     -- Upgrade all packets  
  138.     local upgrade = luci.http.formvalue("upgrade")  
  139.     if upgrade then  
  140.         upgrade, out, err = ipkg.upgrade()  
  141.         stdout[#stdout+1] = out  
  142.         stderr[#stderr+1] = err  
  143.     end  
  144.   
  145.   
  146.     -- List state  
  147.     local no_lists = true  
  148.     local old_lists = false  
  149.     local tmp = nixio.fs.dir("/var/opkg-lists/")  
  150.     if tmp then  
  151.         for tmp in tmp do  
  152.             no_lists = false  
  153.             tmp = nixio.fs.stat("/var/opkg-lists/"..tmp)  
  154.             if tmp and tmp.mtime < (os.time() - (24 * 60 * 60)) then  
  155.                 old_lists = true  
  156.                 break  
  157.             end  
  158.         end  
  159.     end  
  160.   
  161.   
  162.     luci.template.render("admin_system/packages", {  
  163.         display   = display,  
  164.         letter    = letter,  
  165.         query     = query,  
  166.         install   = install,  
  167.         remove    = remove,  
  168.         update    = update,  
  169.         upgrade   = upgrade,  
  170.         no_lists  = no_lists,  
  171.         old_lists = old_lists,  
  172.         stdout    = table.concat(stdout, ""),  
  173.         stderr    = table.concat(stderr, "")  
  174.     })  
  175.   
  176.     -- Remove index cache  
  177.     if changes then  
  178.         nixio.fs.unlink("/tmp/luci-indexcache")  
  179.     end  
  180. end  
  181.   
  182. function action_flashops()  
  183.     local sys = require "luci.sys"  
  184.     local fs  = require "luci.fs"  
  185.   
  186.     local upgrade_avail = nixio.fs.access("/lib/upgrade/platform.sh")  
  187.     local reset_avail   = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0  
  188.   
  189.     local restore_cmd = "tar -xzC/ >/dev/null 2>&1"  
  190.     local backup_cmd  = "tar -czT %s 2>/dev/null"  
  191.     local image_tmp   = "/tmp/firmware.img"  
  192.   
  193.     local function image_supported()  
  194.         -- XXX: yay...  
  195.         return ( 0 == os.execute(  
  196.             ". /etc/functions.sh; " ..  
  197.             "include /lib/upgrade; " ..  
  198.             "platform_check_image %q >/dev/null"  
  199.                 % image_tmp  
  200.         ) )  
  201.     end  
  202.   
  203.     local function image_checksum()  
  204.         return (luci.sys.exec("md5sum %q" % image_tmp):match("^([^%s]+)"))  
  205.     end  
  206.   
  207.     local function storage_size()  
  208.         local size = 0  
  209.         if nixio.fs.access("/proc/mtd") then  
  210.             for l in io.lines("/proc/mtd"do  
  211.                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')  
  212.                 if n == "linux" or n == "firmware" then  
  213.                     size = tonumber(s, 16)  
  214.                     break  
  215.                 end  
  216.             end  
  217.         elseif nixio.fs.access("/proc/partitions") then  
  218.             for l in io.lines("/proc/partitions"do  
  219.                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')  
  220.                 if b and n and not n:match('[0-9]') then  
  221.                     size = tonumber(b) * 1024  
  222.                     break  
  223.                 end  
  224.             end  
  225.         end  
  226.         return size  
  227.     end  
  228.   
  229.   
  230.     local fp  
  231.     luci.http.setfilehandler(  
  232.         function(meta, chunk, eof)  
  233.             if not fp then  
  234.                 if meta and meta.name == "image" then  
  235.                     fp = io.open(image_tmp, "w")  
  236.                 else  
  237.                     fp = io.popen(restore_cmd, "w")  
  238.                 end  
  239.             end  
  240.             if chunk then  
  241.                 fp:write(chunk)  
  242.             end  
  243.             if eof then  
  244.                 fp:close()  
  245.             end  
  246.         end  
  247.     )  
  248.   
  249.     if luci.http.formvalue("backup") then  
  250.         --  
  251.         -- Assemble file list, generate backup  
  252.         --  
  253.         local filelist = "/tmp/luci-backup-list.%d" % os.time()  
  254.         sys.call(  
  255.             "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..  
  256.             "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..  
  257.             "opkg list-changed-conffiles ) | sort -u > %s" % filelist  
  258.         )  
  259.         if fs.access(filelist) then  
  260.             local reader = ltn12_popen(backup_cmd:format(filelist))  
  261.             luci.http.header('Content-Disposition''attachment; filename="backup-%s-%s.tar.gz"' % {  
  262.                 luci.sys.hostname(), os.date("%Y-%m-%d")})  
  263.             luci.http.prepare_content("application/x-targz")  
  264.             luci.ltn12.pump.all(reader, luci.http.write)  
  265.             fs.unlink(filelist)  
  266.         end  
  267.     elseif luci.http.formvalue("restore") then  
  268.         --  
  269.         -- Unpack received .tar.gz  
  270.         --  
  271.         local upload = luci.http.formvalue("archive")  
  272.         if upload and #upload > 0 then  
  273.             luci.template.render("admin_system/applyreboot")  
  274.             luci.sys.reboot()  
  275.         end  
  276.     elseif luci.http.formvalue("image"or luci.http.formvalue("step") then  
  277.         --  
  278.         -- Initiate firmware flash  
  279.         --  
  280.         local step = tonumber(luci.http.formvalue("step"or 1)  
  281.         if step == 1 then  
  282.             if image_supported() then  
  283.                 luci.template.render("admin_system/upgrade", {  
  284.                     checksum = image_checksum(),  
  285.                     storage  = storage_size(),  
  286.                     size     = nixio.fs.stat(image_tmp).size,  
  287.                     keep     = (not not luci.http.formvalue("keep"))  
  288.                 })  
  289.             else  
  290.                 nixio.fs.unlink(image_tmp)  
  291.                 luci.template.render("admin_system/flashops", {  
  292.                     reset_avail   = reset_avail,  
  293.                     upgrade_avail = upgrade_avail,  
  294.                     image_invalid = true  
  295.                 })  
  296.             end  
  297.         --  
  298.         -- Start sysupgrade flash  
  299.         --  
  300.         elseif step == 2 then  
  301.             local keep = (luci.http.formvalue("keep") == "1"and "" or "-n"  
  302.             luci.template.render("admin_system/applyreboot", {  
  303.                 title = luci.i18n.translate("Flashing..."),  
  304.                 msg   = luci.i18n.translate("The system is flashing now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes until you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),  
  305.                 addr  = (#keep > 0) and "192.168.1.1" or nil  
  306.             })  
  307.             fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })  
  308.         end  
  309.     elseif reset_avail and luci.http.formvalue("reset") then  
  310.         --  
  311.         -- Reset system  
  312.         --  
  313.         luci.template.render("admin_system/applyreboot", {  
  314.             title = luci.i18n.translate("Erasing..."),  
  315.             msg   = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),  
  316.             addr  = "192.168.1.1"  
  317.         })  
  318.         fork_exec("killall dropbear uhttpd; sleep 1; mtd -r erase rootfs_data")  
  319.     else  
  320.         --  
  321.         -- Overview  
  322.         --  
  323.         luci.template.render("admin_system/flashops", {  
  324.             reset_avail   = reset_avail,  
  325.             upgrade_avail = upgrade_avail  
  326.         })  
  327.     end  
  328. end  
  329.   
  330. function action_passwd()  
  331.     local p1 = luci.http.formvalue("pwd1")  
  332.     local p2 = luci.http.formvalue("pwd2")  
  333.     local stat = nil  
  334.   
  335.     if p1 or p2 then  
  336.         if p1 == p2 then  
  337.             stat = luci.sys.user.setpasswd("root", p1)  
  338.         else  
  339.             stat = 10  
  340.         end  
  341.     end  
  342.   
  343.     luci.template.render("admin_system/passwd", {stat=stat})  
  344. end  
  345.   
  346. function action_reboot()  
  347.     local reboot = luci.http.formvalue("reboot")  
  348.     luci.template.render("admin_system/reboot", {reboot=reboot})  
  349.     if reboot then  
  350.         luci.sys.reboot()  
  351.     end  
  352. end  
  353.   
  354. function fork_exec(command)  
  355.     local pid = nixio.fork()  
  356.     if pid > 0 then  
  357.         return  
  358.     elseif pid == 0 then  
  359.         -- change to root dir  
  360.         nixio.chdir("/")  
  361.   
  362.         -- patch stdin, out, err to /dev/null  
  363.         local null = nixio.open("/dev/null""w+")  
  364.         if null then  
  365.             nixio.dup(null, nixio.stderr)  
  366.             nixio.dup(null, nixio.stdout)  
  367.             nixio.dup(null, nixio.stdin)  
  368.             if null:fileno() > 2 then  
  369.                 null:close()  
  370.             end  
  371.         end  
  372.   
  373.         -- replace with target command  
  374.         nixio.exec("/bin/sh""-c", command)  
  375.     end  
  376. end  
  377.   
  378. function ltn12_popen(command)  
  379.   
  380.     local fdi, fdo = nixio.pipe()  
  381.     local pid = nixio.fork()  
  382.   
  383.     if pid > 0 then  
  384.         fdo:close()  
  385.         local close  
  386.         return function()  
  387.             local buffer = fdi:read(2048)  
  388.             local wpid, stat = nixio.waitpid(pid, "nohang")  
  389.             if not close and wpid and stat == "exited" then  
  390.                 close = true  
  391.             end  
  392.   
  393.             if buffer and #buffer > 0 then  
  394.                 return buffer  
  395.             elseif close then  
  396.                 fdi:close()  
  397.                 return nil  
  398.             end  
  399.         end  
  400.     elseif pid == 0 then  
  401.         nixio.dup(fdo, nixio.stdout)  
  402.         fdi:close()  
  403.         fdo:close()  
  404.         nixio.exec("/bin/sh""-c", command)  
  405.     end  
  406. end  


效果如下:

 

2. 编写model

 操作:在先在/usr/lib/ lua/luci/model/cbi/admin_system文件中的添加ipv6config.lua 文件

文件全部代码

[php] view plain copy
 print?
  1. require("luci.sys")  
  2. require("luci.sys.zoneinfo")  
  3. require("luci.tools.webadmin")  
  4. require("luci.fs")  
  5. require("luci.config")  
  6.   
  7. local m, s, o  
  8.   
  9. m = Map("ipv6config", translate("ipv6config"), translate("this is ipv6 pre config"))  
  10. m:chain("luci")  
  11.   
  12. s = m:section(TypedSection, "ipv6config", translate("System ipv6"))  
  13. s.anonymous = true  
  14. s.addremove = false  
  15.   
  16. s:tab("general",  translate("General Settings"))  
  17.   
  18. o = s:taboption("general", Value, "ipv6pre", translate("ipv6 pre config"))  
  19. o.datatype = "ipv6config"  
  20.   
  21. function o.write(self, section, value)  
  22.     Value.write(self, section, value)  
  23. end  
  24.   
  25. return m  


效果:   

 

3. 添加配置文件

操作:在/etc/config中添加ipv6config文件

代码:

[php] view plain copy
 print?
  1. config 'ipv6config'  
  2.     option 'ipv6pre' '2011:86'  


ps: 注意

       config 中的ipv6config文件中的ipv6confg与ipv6pre与model中ipv6config.lua两个字段的对应

0 0
原创粉丝点击