--[[ script for init disks for worker 1. if only one disk, format and mount it directry. 2. if the machine had more than 2 disks, make a raid0 disk array and mount it. ]] -- The disk that size between 5TiB(5368709120000) and 9TiB(9684354560000) local disk_min_size = 5368709120000 local disk_max_size = 9684354560000 -- disk amount path, set it to nil if you do not want to amount it --local ceph_mount_paths = {"/data01", "/data02"} local ceph_mount_paths = nil local disk_mount_path = "/yuanyu" -- define disk scan policy, the default way is get disk infos by /QarkCloud, fdisk is the another optional. local disk_scan_policy = "qark" -- skip disks not to be format and build raid array local disk_white_list = {} -- disk_white_list["/dev/sda"] = 1 -- disk_white_list["/dev/sdb"] = 1 -- if set true, it will format disks before mount local format_disk = true --[[ function for mount single disk --]] function do_mount_disk(disk_path, mount_path) print("Try to format disk ...\n") local cmd = "mkfs.xfs -f ".. disk_path print("By command: "..cmd) local status = os.execute(cmd) if (status == nil) then print(" -- [Failed].\n") return false else print(" -- [OK].\n") end print("Try to mount disk ...\n") -- fetch UUID cmd = "blkid |grep '"..disk_path.."' | awk -F ' ' '{print $2}'" local p = io.popen(cmd, "r") local uuid = p:read("*all") if (uuid == nil or uuid == "") then print("Can not get the UUID for disk ".. disk_path.."\n") return false end uuid = string.gsub(uuid, "[ \t\n\r]+$", "") if (file_exists(mount_path) == false) then mkdir(mount_path, 0755) end cmd = "mount ".. uuid .. " " .. mount_path print("By command: ".. cmd) status = os.execute(cmd) if (status == nil) then print(" -- [Failed].\n") return false else print(" -- [OK].\n") end local fstab = file_get_contents("/etc/fstab") if (string.find(fstab, "^"..uuid) == nil) then local text = uuid.." "..disk_mount_path.." xfs defaults 0 0 \n"; print("Write partitions info to fstab...") status = file_put_contents("/etc/fstab", text, FILE_APPEND) if (status == false) then print(" -- [Failed].\n") return false else print(" -- [OK].\n") end end return true end --[[ function for making raid0 disk array --]] function make_raid_array(disks, mount_path) if (file_exists("/dev/md0") == true) then return do_mount_disk("/dev/md0", mount_path) end local cmd = "echo y|mdadm -Cv /dev/md0 -a yes -n ".. #disks .." -l 0" for _, disk in pairs(disks) do cmd = cmd .." ".. disk["name"] -- format disk to fixed raid0 destory when reboot if (format_disk == true) then local cmd = "mkfs.xfs -f ".. disk["name"] print("Try to format disk "..disk["name"].." ... ") local status = os.execute(cmd) if (status == nil) then print(" -- [Failed].\n") return false else print(" -- [OK].\n") end end end print("Try to create raid0 disk array... \n") print("By command: ".. cmd) local status = os.execute(cmd) if (status == nil) then print(" -- [Failed].\n") return false end print(" -- [OK].\n") local content = file_get_contents("/etc/mdadm/mdadm.conf") if (content ~= nil and string.find(content, "^".."ARRAY /dev/md0") == nil) then print("Try to save raid0 array to /etc/mdadm/mdadm.conf...") cmd = "mdadm -Ds >> /etc/mdadm/mdadm.conf" status = os.execute(cmd) if (status == nil) then print(" -- [Failed].\n") return false else print(" -- [OK].\n") end end return do_mount_disk("/dev/md0", mount_path) end -- find the disks that meet the requirement local disk_list = {} if (disk_scan_policy == "qark") then local data = file_get_contents("/var/run/qark-client/monitor.protocol.json") local json = nil if (data == nil) then echo "No config file found, please run /QarkCloud-booter first.\n" else local json = json_decode(data) local disks = json["disk"] for key, disk in pairs(disks) do if (disk["size"] > disk_min_size and disk["size"] < disk_max_size and disk_white_list[disk["name"]] == nil) then table.insert(disk_list, disk) end end end else -- collect disk informations by fdisk command local cmd = "fdisk -l |grep 'Disk /dev/' |awk -F ',|:' '{print $1,$3}' |awk -F ' ' '{print $2, $3}'" local handler = popen(cmd, "r") if (handler == nil) then print("Fail to invoke ", cmd, "\n") return false end local line = fgets(handler) while (line ~= nil) do local name, size = string.match(line, "([^%s]+)%s+(%d+)") size = tonumber(size) if (name ~= nil and size > disk_min_size and size < disk_max_size and disk_white_list[name] == nil) then local disk = {} disk["name"] = name disk["size"] = size --print(name, ", ", size, "\n") table.insert(disk_list, disk) end line = fgets(handler) end end function mount_disk(disks, path) -- check raid0 is mounted local cmd = "df -h |grep "..path.." |wc -l" print("Run command: "..cmd.."\n") local p = io.popen(cmd, "r") local result = p:read("*all") if (tonumber(result) > 0) then print(path .. " has been mounted, skip.\n") return end if (#disks == 0) then print("No suitable disks found.\n") end if (#disks == 1) then do_mount_disk(disk_list[1]["name"], path) end if (#disks > 1) then local dir = opendir("/dev/"); local dir_name = readdir(dir); while (dir_name ~= nil) do if (string.find(dir_name, "md")~= nil) then local dev_file = "/dev/"..dir_name; if (file_exists(dev_file) == true) then print("Try to stop raid0 " .. "/dev/"..dir_name .. " ...") -- umount raid0 os.execute("umount -f ".. "/dev/"..dir_name) cmd = "mdadm -S "..dev_file status = os.execute(cmd) print(cmd) -- sleep 3 secs to wait raid0 stop sleep(3) os.execute(cmd) if (status == nil) then print(" --[Failed]\n") else print(" --[OK]\n") end end end dir_name = readdir(dir); end closedir(dir); m_disk = make_raid_array(disks, path) end end if (disk_mount_path ~= nil) then mount_disk(disk_list, disk_mount_path) end local p = io.popen("df -h |grep -nE '/yuanyu'", "r") local result = p:read("*all") print(result)