Skip to content

Commit

Permalink
btrfs: fix mount issue in openwrt, fix bug on create readonly snapsho…
Browse files Browse the repository at this point in the history
…t && update translation
  • Loading branch information
lisaac committed Feb 7, 2020
1 parent e1584ff commit 20e678d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 38 deletions.
37 changes: 24 additions & 13 deletions luasrc/model/cbi/diskman/btrfs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ if not uuid then luci.http.redirect(luci.dispatcher.build_url("admin/system/disk
-- mount subv=/ to tempfs
mount_point = "/tmp/.btrfs_tmp"
nixio.fs.mkdirr(mount_point)
luci.util.exec("umount "..mount_point .. " >/dev/null 2>&1")
luci.util.exec("mount -o subvol=/ -U "..uuid.." "..mount_point)
luci.util.exec(dm.command.umount .. " "..mount_point .. " >/dev/null 2>&1")
luci.util.exec(dm.command.mount .. " -t btrfs -o subvol=/ UUID="..uuid.." "..mount_point)

m = SimpleForm("btrfs", translate("Btrfs"), translate("Manage Btrfs"))
m.template = "diskman/cbi/xsimpleform"
m.redirect = luci.dispatcher.build_url("admin/system/diskman")
m.submit = false
m.reset = false
m.redirect = luci.dispatcher.build_url("admin/system/diskman")

-- info
local btrfs_info = dm.get_btrfs_info(mount_point)
Expand Down Expand Up @@ -109,7 +110,7 @@ btn_remove.render = function(self, section, scope)
btn_remove.inputtitle = translate("Create")
btn_remove.inputstyle = "add"
self.view_disabled = false
elseif subvolume_list[section].path == "/" then
elseif subvolume_list[section].path == "/" or subvolume_list[section].default_subvolume then
btn_remove.inputtitle = translate("Delete")
btn_remove.inputstyle = "remove"
self.view_disabled = true
Expand All @@ -125,13 +126,17 @@ btn_remove.write = function(self, section, value)
local cmd
if value == translate("Delete") then
cmd = dm.command.btrfs .. " subvolume delete " .. mount_point .. subvolume_list[section].path
elseif value == translate("Create") and value_path then
luci.util.perror(mount_point)
cmd = dm.command.btrfs .. " subvolume create " .. mount_point .. value_path
elseif value == translate("Create") then
if value_path and value_path:match("^/") then
cmd = dm.command.btrfs .. " subvolume create " .. mount_point .. value_path
else
m.errmessage = translate("Please input Subvolume Path, Subvolume must start with '/'")
return
end
end
local res = luci.util.exec(cmd.. " 2>&1")
if res and (res:match("ERR") or res:match("not enough arguments")) then
m.errmessage = res
m.errmessage = luci.util.pcdata(res)
else
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/btrfs/" .. uuid))
end
Expand All @@ -151,7 +156,7 @@ end
-- local cmd = dm.command.btrfs .. " subvolume delete " .. mount_point .. snapshot_list[section].path
-- local res = luci.util.exec(cmd.. " 2>&1")
-- if res and (res:match("ERR") or res:match("not enough arguments")) then
-- m.errmessage = res
-- m.errmessage = luci.util.pcdata(res)
-- else
-- luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/btrfs/" .. uuid))
-- end
Expand All @@ -160,20 +165,24 @@ end
-- new snapshots
local s_snapshot = m:section(SimpleSection, translate("New Snapshot"))
local value_sorce, value_dest, value_readonly
local v_sorce = s_snapshot:option(Value, "_source", translate("Source Path"))
local v_sorce = s_snapshot:option(Value, "_source", translate("Source Path"), translate("The source path for create the snapshot"))
v_sorce.placeholder = "/data"
v_sorce.forcewrite = true
v_sorce.write = function(self, section, value)
value_sorce = value
end

local v_readonly = s_snapshot:option(Flag, "_readonly", translate("Readonly"))
local v_readonly = s_snapshot:option(Flag, "_readonly", translate("Readonly"), translate("The path where you want to store the snapshot"))
v_readonly.forcewrite = true
v_readonly.rmempty = false
v_readonly.disabled = 0
v_readonly.enabled = 1
v_readonly.default = 1
v_readonly.write = function(self, section, value)
value_readonly = value
end
local v_dest = s_snapshot:option(Value, "_dest", translate("Destination Path (optional)"))
v_dest.forcewrite = true
v_dest.placeholder = "/.snapshot/202002051538"
v_dest.write = function(self, section, value)
value_dest = value
Expand All @@ -186,13 +195,15 @@ btn_snp_create.write = function(self, section, value)
if value_sorce and value_sorce:match("^/") then
if not value_dest then value_dest = "/.snapshot"..value_sorce.."/"..os.date("%Y%m%d%H%M%S") end
nixio.fs.mkdirr(mount_point..value_dest:match("(.-)[^/]+$"))
local cmd = dm.command.btrfs .. " subvolume snapshot " .. (value_readonly == 1 and " -r " or " ") .. mount_point..value_sorce .. " " .. mount_point..value_dest
local cmd = dm.command.btrfs .. " subvolume snapshot" .. (value_readonly == 1 and " -r " or " ") .. mount_point..value_sorce .. " " .. mount_point..value_dest
local res = luci.util.exec(cmd .. " 2>&1")
if res and (res:match("ERR") or res:match("not enough arguments")) then
m.errmessage = res
m.errmessage = luci.util.pcdata(res)
else
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/btrfs/" .. uuid))
end
else
m.errmessage = translate("Please input Source Path of snapshot, Source Path must start with '/'")
end
end

Expand Down
8 changes: 4 additions & 4 deletions luasrc/model/cbi/diskman/disks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ if dm.command.mdadm then
-- mdadm --create --verbose /dev/md0 --level=stripe --raid-devices=2 /dev/sdb6 /dev/sdc5
local res = dm.create_raid(rname, rlevel, rmembers)
if res and res:match("^ERR") then
m.errmessage = translate(res)
m.errmessage = luci.util.pcdata(res)
return
end
dm.gen_mdadm_config()
Expand Down Expand Up @@ -251,9 +251,9 @@ btn_umount.write = function(self, section, value)
local res
if value == translate("Mount") then
luci.util.exec("mkdir -p ".. _mount_point.mount_point)
res = luci.util.exec("mount ".. _mount_point.device .. (_mount_point.fs and (" -t ".. _mount_point.fs )or "") .. (_mount_point.mount_options and (" -o " .. _mount_point.mount_options.. " ") or " ").._mount_point.mount_point .. " 2>&1")
res = luci.util.exec(dm.command.mount .. " ".. _mount_point.device .. (_mount_point.fs and (" -t ".. _mount_point.fs )or "") .. (_mount_point.mount_options and (" -o " .. _mount_point.mount_options.. " ") or " ").._mount_point.mount_point .. " 2>&1")
elseif value == translate("Umount") then
res = luci.util.exec("umount "..mount_point[section].mount_point .. " 2>&1")
res = luci.util.exec(dm.command.umount .. " "..mount_point[section].mount_point .. " 2>&1")
end
if res:match("^mount:") or res:match("^umount:") then
m.errmessage = luci.util.pcdata(res)
Expand Down Expand Up @@ -307,7 +307,7 @@ if dm.command.btrfs then
-- mkfs.btrfs -L label -d blevel /dev/sda /dev/sdb
local res = dm.create_btrfs(blabel, blevel, bmembers)
if res and res:match("^ERR") then
m.errmessage = translate(res)
m.errmessage = luci.util.pcdata(res)
return
end
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman"))
Expand Down
34 changes: 18 additions & 16 deletions luasrc/model/cbi/diskman/partition.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ btn_eject.forcewrite = true
btn_eject.write = function(self, section, value)
for i, p in ipairs(disk_info.partitions) do
if p.mount_point ~= "-" then
m.errmessage = p.name .. "is in use! please unmount it first!"
m.errmessage = p.name .. translate("is in use! please unmount it first!")
return
end
end
Expand Down Expand Up @@ -209,18 +209,21 @@ if not disk_info.p_table:match("Raid") then
btn_format.write = function(self, section, value)
local partition_name = "/dev/".. disk_info.partitions[section].name
if not nixio.fs.access(partition_name) then
m.errmessage = "Partition NOT found!"
m.errmessage = translate("Partition NOT found!")
return
end
local fs = disk_info.partitions[section]._fs
if not format_cmd[fs] then
m.errmessage = "Filesystem NOT support!"
m.errmessage = translate("Filesystem NOT support!")
return
end
local cmd = format_cmd[fs].cmd .. " " .. format_cmd[fs].option .. " " .. partition_name
-- luci.util.perror(cmd)
local res = luci.sys.exec(cmd)
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/partition/" .. dev))
local res = luci.util.exec(cmd .. " 2>&1")
if res and res:lower():match("error+") then
m.errmessage = luci.util.pcdata(res)
else
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/partition/" .. dev))
end
end

local btn_action = s_partition_table:option(Button, "_action")
Expand Down Expand Up @@ -248,7 +251,6 @@ if not disk_info.p_table:match("Raid") then
Button.render(self, section, scope)
end
btn_action.write = function(self, section, value)
-- luci.util.perror(value)
if value == translate("New") then
local start_sec = disk_info.partitions[section]._sec_start and tonumber(disk_info.partitions[section]._sec_start) or tonumber(disk_info.partitions[section].sec_start)
local end_sec = disk_info.partitions[section]._sec_end
Expand All @@ -265,7 +267,7 @@ if not disk_info.p_table:match("Raid") then
start_sec = start_sec .. "s"
end
else
m.errmessage = "Invalid Start Sector!"
m.errmessage = translate("Invalid Start Sector!")
return
end
-- support +size format for End sector
Expand All @@ -284,7 +286,7 @@ if not disk_info.p_table:match("Raid") then
elseif tonumber(end_sec) then
end_sec = end_sec .. "s"
else
m.errmessage = "Invalid End Sector!"
m.errmessage = translate("Invalid End Sector!")
return
end
local part_type = "primary"
Expand All @@ -303,23 +305,23 @@ if not disk_info.p_table:match("Raid") then

-- partiton
local cmd = dm.command.parted .. " -s -a optimal /dev/" .. dev .. " mkpart " .. part_type .." " .. start_sec .. " " .. end_sec
local res = luci.util.exec(cmd)
if res:match("Error.+") then
m.errmessage = res
local res = luci.util.exec(cmd .. " 2>&1")
if res and res:lower():match("error+") then
m.errmessage = luci.util.pcdata(res)
else
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/partition/" .. dev))
end
elseif value == translate("Remove") then
-- remove partition
local number = tostring(disk_info.partitions[section].number)
if (not number) or (number == "") then
m.errmessage = "Partition not exists!"
m.errmessage = translate("Partition not exists!")
return
end
local cmd = dm.command.parted .. " -s /dev/" .. dev .. " rm " .. number
local res = luci.util.exec(cmd)
if res:match("Error.+") then
m.errmessage = res
local res = luci.util.exec(cmd .. " 2>&1")
if res and res:lower():match("error+") then
m.errmessage = luci.util.pcdata(res)
else
luci.http.redirect(luci.dispatcher.build_url("admin/system/diskman/partition/" .. dev))
end
Expand Down
8 changes: 3 additions & 5 deletions luasrc/model/diskman.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ for _, cmd in ipairs(CMD) do
d.command[cmd] = command:match("^.+"..cmd) or nil
end

d.command.mount = nixio.fs.access("/usr/bin/mount") and "/usr/bin/mount" or "/bin/mount"
d.command.umount = nixio.fs.access("/usr/bin/umount") and "/usr/bin/umount" or "/bin/umount"

local mounts = nixio.fs.readfile("/proc/mounts") or ""
local swaps = nixio.fs.readfile("/proc/swaps") or ""
local df = luci.sys.exec(d.command.df) or ""
Expand Down Expand Up @@ -670,11 +673,6 @@ if not snapshot then
end
end
end
luci.util.perror(_uuid)
luci.util.perror(_otime)
luci.util.perror(_id)
luci.util.perror(_snap)
luci.util.perror(_uuid)
if _uuid and _otime and _id then
subvolume["0".._id] = {id = _id , uuid = _uuid, otime = _otime, snapshots = _snap, path = "/"}
if default_subvolume_id == _id then
Expand Down
30 changes: 30 additions & 0 deletions po/zh-cn/diskman.po
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,33 @@ msgstr "数据"

msgid "Btrfs Info"
msgstr "Btrfs 信息"

msgid "The source path for create the snapshot"
msgstr "创建快照的源数据目录"

msgid "The path where you want to store the snapshot"
msgstr "存放快照数据目录"

msgid "Please input Source Path of snapshot, Source Path must start with '/'"
msgstr "请输入快照源路径,源路径必须以'/'开头"

msgid "Please input Subvolume Path, Subvolume must start with '/'"
msgstr "请输入子卷路径,子卷路径必须以'/'开头"

msgid "is in use! please unmount it first!"
msgstr "正在被使用!请先卸载!"

msgid "Partition NOT found!"
msgstr "分区未找到!"

msgid "Filesystem NOT support!"
msgstr "文件系统不支持!"

msgid "Invalid Start Sector!"
msgstr "无效的起始扇区!"

msgid "Invalid End Sector"
msgstr "无效的终止扇区!"

msgid "Partition not exists!"
msgstr "分区不存在!"

0 comments on commit 20e678d

Please sign in to comment.