[ UP ] update cgi, lvm and raid can work. add set time and shutdown function to syste...
[archserver.git] / cgi / volumes / create_raid.lua
blob13a7925506e9e4f4f46d90fad8b463a285602a4f
1 #!/usr/bin/lua
3 dofile "libutil.lua"
4 local success = "<font color=green><strong>成功</strong></font><br>"
6 function display_current_raid()
7 local mdstat = get_mdstat()
8 print([[
9 <h3>系统当前Raid</h3>
10 <form action = "volumes_create_raid.cgi" method = "post">
11 <table width=90%><tr><td>
12 <table cellspacing="1" cellpadding="5" border="0" width="100%">
13 <tr align="left">
14 <td bgcolor="#ebebeb" width=1%></td>
15 <td bgcolor="#ebebeb"><strong>Raid名称</strong></td>
16 <td bgcolor="#ebebeb"><strong>容量</strong></td>
17 <td bgcolor="#ebebeb"><strong>类型</strong></td>
18 <td bgcolor="#ebebeb"><strong>使用磁盘</strong></td>
19 </tr>
20 ]])
23 for r in pairs(mdstat) do
24 local _td1 = string.format([[<tr bgcolor=#c6d5a8 align="left"><td>
25 <input type="radio" name="md" value="%s"></td><td>%s</td>]], r,string.match(r, "/dev/(%S+)"))
26 print(_td1)
27 print("<td>"..mdstat[r].size.."</td>")
28 print("<td>"..string.upper(mdstat[r].level).."</td>")
29 print("<td>"..mdstat[r].devices.."</td></tr>")
30 end
32 print([[</table></td></tr>
33 <tr><td>
34 <input type="hidden" name=todo value="type">
35 <input type="submit" value="创建新阵列">
36 <input type="hidden" value="清除">
37 </td></tr></table>
38 </form><br>]])
39 end
41 function display_create_raid()
42 print([[
43 <h3>Raid管理 - 选择Raid类型</h3>
44 <form name="select_raid" method="post" action="volumes_create_raid.cgi">
45 <table width=70%><tr><td>
46 <table cellspacing="1" cellpadding="5" border="0" width="100%">
47 <tr>
48 <td bgcolor="#ebebeb" width=1%></td>
49 <td bgcolor="#ebebeb"><strong>Raid类型</strong></td>
50 <td bgcolor="#ebebeb"><strong>概述</strong></td>
51 <td bgcolor="#ebebeb"><strong>备注</strong></td></tr>
52 <tr class="color_table_row1"><td ><input type="radio" name="raidtype" value="0"></td>
53 <td>Raid0</td><td>磁盘分块</td><td>无冗余</td></tr>
54 <tr class="color_table_row2"><td><input type="radio" name="raidtype" value="1"></td>
55 <td>Raid1</td><td>镜像冗余</td><td></td></tr>
56 <tr class="color_table_row1"><td><input type="radio" name="raidtype" value="5"></td>
57 <td>Raid5</td><td>校验冗余</td><td>推荐</td></tr>
58 <tr class="color_table_row2"><td><input type="radio" name="raidtype" value="6"></td>
59 <td>Raid6</td><td>双校验冗余</td><td></td></tr>
60 <tr class="color_table_row1"><td><input type="radio" name="raidtype" value="10"></td>
61 <td>Raid10</td><td>分块镜像</td><td></td></tr>
62 </table></tr>
63 <tr><td><input type="submit" value="选择"></td></tr></table>
64 <input type=hidden name="todo" value="disks">
65 </form>
66 ]])
67 end
69 function display_disks(qp)
70 if not qp.raidtype then ui_message_err("没有选择Raid类型") return end
71 print("select: "..qp.raidtype)
72 local disks = get_disks()
73 print([[
74 <h3>Raid管理 - 选择磁盘</h3>
75 <form name="select_raid" method="post" action="volumes_create_raid.cgi">
76 <table width=70%><tr><td>
77 <table cellspacing="1" cellpadding="5" border="0" width="100%">
78 <tr>
79 <td bgcolor="#ebebeb" width=1%></td>
80 <td bgcolor="#ebebeb"><strong>磁盘编号</strong></td>
81 <td bgcolor="#ebebeb"><strong>容量</strong></td>
82 <td bgcolor="#ebebeb"><strong>型号</strong></td></tr>
83 ]])
85 i = 0
86 for d in pairs(disks) do
87 if not disks[d].used then
88 i = i + 1
89 v = one_two(v)
90 print([[<tr class="color_table_row]]..v..[["><td><input type="checkbox"
91 name="dev_]]..i..[[" value="]]..d..[["></td><td>]]..i..[[</td><td>]]..disks[d].size..[[
92 </td><td>]]..disks[d].model..[[</td></tr>]])
93 end
94 end
95 print([[
96 </table></tr>
97 <tr><td><input type="submit" value="创建">&nbsp;<input type="reset" value="清空"></td>
98 </tr></table>
99 <input type=hidden name="todo" value="create">
100 <input type=hidden name="raidtype" value="]]..qp.raidtype..[[">
101 </form>
105 -- parted a disk to 2 partitions: 1.swap and 2.xfs, then mount swap,write to fstab
106 function do_disk(disk)
107 local fstr = myexec("sudo parted "..disk.." p")
108 local disksize = string.match(fstr, "\nDisk%s+"..disk..":%s+(%S+)\n")
109 print("disksize: "..disksize)
110 if not disksize then ui_message_err("Not get disk size") return end
111 local cmd = {
112 ["label"] = "sudo parted -s "..disk.." mklabel msdos",
113 ["swap"] = "sudo parted -s "..disk.." mkpart primary linux-swap 0 512",
114 ["xfs"] = "sudo parted -s "..disk.." mkpart primary xfs 512 "..disksize
116 local err,ret
117 local conf = disk.."1\tswap\tswap\tdefaults\t0\t0\n"
119 print("正在为磁盘 "..disk.."分区")
120 err, ret = myexec(cmd.label)
121 if ret ~= 0 then ui_message_err(err) return nil end
122 err, ret = myexec(cmd.swap)
123 if ret ~= 0 then ui_message_err(err) return nil end
124 err, ret = myexec(cmd.xfs)
125 if ret ~= 0 then ui_message_err(err) return nil else print(success) end
127 file_gsub("/etc/fstab", "\n"..disk.."1.-\n", "\n")
128 err, ret = mywrite("/etc/fstab", myread("/etc/fstab")..conf)
129 myexec("sudo swapon "..disk.."1")
130 return true
133 -- call mdadm to make a real raid and add to mdadm.conf
134 function make_raid(raid)
135 local err, ret
136 local cmd="sudo mdadm -C "..raid.md.." -l"..raid.level.." -n"..raid.num.." "..raid.devices.." -R"
137 local conf="\nDEVICE "..raid.devices.."\nARRAY "..raid.md.." level=raid"..raid.level.." devices="..string.gsub(raid.devices,"%s+",",").."\n"
139 if raid.spare ~= "" then cmd=cmd.." -x "..raid.spare end
140 for d in string.gmatch(raid.devices, "(%S+)") do
141 if not do_disk(d) then return end
144 print("正在创建磁盘阵列 ...")
145 err, ret = myexec(cmd)
146 if ret ~= 0 then ui_message_err(err) return nil else print(success) end
148 mywrite("/etc/mdadm.conf", myread("/etc/mdadm.conf")..conf)
151 function create_raid(qp)
152 local raid = {["devices"]="", ["num"]=0, ["level"] = -1, ["md"] = "", ["spare"]=""}
153 local mdstat = get_mdstat()
155 if not qp.raidtype then ui_message_err("No raid type") return end
156 for k,v in pairs(qp) do
157 if string.sub(k,1,4) == "dev_" then
158 raid.devices = raid.devices.." "..qp[k]
159 raid.num = raid.num + 1
162 if raid.devices == "" then ui_message_err("No devices") return end
163 raid.level = tonumber(qp.raidtype)
165 for i=0, 9 do
166 for md in pairs(mdstat) do
167 if "/dev/md"..i ~= md then raid.md="/dev/md"..i break end
169 if raid.md ~= "" then break end
171 for k,v in pairs(raid) do print(k.."="..v.."<br>") end
172 if not make_raid(raid) then return nil end
173 print("oK")
176 ------------------- CGI OUTPUT ---------------------
177 local qp = ui_getqp()
178 if not qp.todo then
179 display_current_raid()
180 -- display_create_raid()
181 elseif qp.todo == "type" then
182 display_create_raid()
183 elseif qp.todo == "disks" then
184 display_disks(qp)
185 elseif qp.todo == "create" then
186 create_raid(qp)