diff --git a/nbd/nbd-package/etc/curve/curvetab b/nbd/nbd-package/etc/curve/curvetab index f630137330..f018fe49dc 100644 --- a/nbd/nbd-package/etc/curve/curvetab +++ b/nbd/nbd-package/etc/curve/curvetab @@ -3,7 +3,7 @@ # use for auto map and mount curve disk when boot # # this file record the correspondence of curve nbd images/ devices -# add record when 'curve-nbd map' and delete when 'curve-nbd unmap' +# add record when 'curve-nbd map' and 'curve-nbd unmap' # # format: device image mount -# cbd:pool//curvefile_test_ /dev/nbd0 /curve +# /dev/nbd0 cbd:pool//curvefile_test_ /curve diff --git a/nbd/nbd-package/usr/map_curve_disk.sh b/nbd/nbd-package/usr/map_curve_disk.sh index 35722e5691..82a1f32c0a 100644 --- a/nbd/nbd-package/usr/map_curve_disk.sh +++ b/nbd/nbd-package/usr/map_curve_disk.sh @@ -11,6 +11,8 @@ ### END INIT INFO # default confpath +# file format is:dealflag \t device \t image \t mountpoint(option) +# + /dev/nbd0 cbd:pool//curvefile_test_ /test confPath=/etc/curve/curvetab # check sudo @@ -22,33 +24,62 @@ fi # usage function usage() { - echo "Usage: ./map_curve_disk.sh" + echo "Usage: ./map_curve_disk.sh start" echo " -c/--confPath: set the confPath (default /etc/curve/curvetab)" + echo " file format is:dealflag \t device \t image \t mountpoint(option)" + echo " example: + /dev/nbd0 cbd:pool//curvefile_test_ /test" echo " -h/--help: get the script usage" echo "Examples:" echo " ./map_curve_disk.sh start //use the default configuration" echo " ./map_curve_disk.sh start --confPath yourConfPath //use the new confPath" } -# map the curve disk based on curvetab -function map() { +# remove extra records +function dealcurvetab() { if [ ! -f ${confPath} ] then echo "ERROR: not found configuration file, confPath is ${confPath}!" exit fi - cat ${confPath} | grep -v '#' | while read line + declare -A recordmap + while read line + do + flag=$(echo $line | awk '{print $1}') + device=$(echo $line | awk '{print $2}') + if [ "$flag" = "+" ] + then + recordmap["$device"]=$line + elif [ "$flag" = "-" ] + then + unset recordmap[$device] + fi + done < ${confPath} + for key in ${!recordmap[@]}; do + echo ${recordmap[$key]} >> ${confPath}.bak + done +} + +# map the curve disk based on curvetab +function map() { + if [ ! -f ${confPath}.bak ] + then + echo "ERROR: not found configuration file, confPath is ${confPath}.bak!" + exit + fi + + cat ${confPath}.bak | grep -v '#' | while read line do - image=$(echo $line | awk '{print $1}') device=$(echo $line | awk '{print $2}') - mount=$(echo $line | awk '{print $3}') + image=$(echo $line | awk '{print $3}') + mount=$(echo $line | awk '{print $4}') curve-nbd map $image --device $device if [ "$mount" != "" ] then mount $device $mount fi done + mv ${confPath}.bak ${confPath} } if [ $# -lt 1 ] @@ -77,6 +108,7 @@ case $1 in ;; esac done + dealcurvetab map ;; *) diff --git a/nbd/src/main.cpp b/nbd/src/main.cpp index 6d1bec8851..a0bb7489bd 100644 --- a/nbd/src/main.cpp +++ b/nbd/src/main.cpp @@ -23,8 +23,6 @@ */ #include -#include -#include #include #include @@ -98,109 +96,20 @@ static void Usage() { } // use for record image and device info to auto map when boot -static void AddRecord() { - std::fstream fs; - int fd; - fs.open(CURVETAB_PATH, std::fstream::in); - if (!fs.is_open()) { +static void AddRecord(int flag) { + std::string record; + int fd = open(CURVETAB_PATH, O_WRONLY | O_APPEND); + if (fd < 0) { std::cerr << "curve-nbd: open curvetab file failed."; return; } - fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const > - (fs.rdbuf())->fd(); - if (0 == flock(fd, LOCK_SH )) { - // check if the record exists - std::string lineString; - while (getline(fs, lineString)) { - if (!lineString.empty() && lineString[0] != '#') { - std::vector lineArray; - std::stringstream ss(lineString); - std::string str; - while (getline(ss, str, '\t')) { - lineArray.push_back(str); - } - if (lineArray.size() != 2 && lineArray.size() != 3) { - std::cerr << "curve-nbd: curvetab file content error."; - fs.close(); - return; - } - if (lineArray[0] == nbdConfig->imgname) { - fs.close(); - return; - } - } - } - fs.close(); - flock(fd, LOCK_UN); - } - // write new record - fs.open(CURVETAB_PATH, std::fstream::app); - if (!fs.is_open()) { - std::cerr << "curve-nbd: open curvetab file failed."; - return; - } - fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const> - (fs.rdbuf())->fd(); - if (0 == flock(fd, LOCK_EX)) { - fs << nbdConfig->imgname << '\t' << nbdConfig->devpath << '\n'; - fs.close(); - flock(fd, LOCK_UN); - } -} - -// delete record when unmap -static void DelRecord() { - std::fstream fs; - int fd; - fs.open(CURVETAB_PATH, std::fstream::in); - if (!fs.is_open()) { - std::cerr << "curve-nbd: open curvetab file failed."; - return; - } - fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const> - (fs.rdbuf())->fd(); - std::string lineString; - std::string newContent; - if (0 == flock(fd, LOCK_SH)) { - // find record need deleted - while (getline(fs, lineString)) { - if (!lineString.empty() && lineString[0] != '#') { - std::vector lineArray; - std::stringstream ss(lineString); - std::string str; - while (getline(ss, str, '\t')) { - lineArray.push_back(str); - } - if (lineArray.size() != 2 && lineArray.size() != 3) { - std::cerr << "curve-nbd: curvetab file content error."; - fs.close(); - return; - } - if (lineArray[1] != nbdConfig->devpath) { - newContent += lineString; - newContent += "\n"; - } - } else { - newContent += lineString; - newContent += "\n"; - } - } - fs.close(); - flock(fd, LOCK_UN); - } - // update record info - fs.open(CURVETAB_PATH, std::fstream::out); - if (!fs.is_open()) { - std::cerr << "curve-nbd: open curvetab file failed."; - return; - } - fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const> - (fs.rdbuf())->fd(); - if (0 == flock(fd, LOCK_EX)) { - fs << newContent; - fs.close(); - flock(fd, LOCK_UN); + if (1 == flag) { + record = "+\t" + nbdConfig->devpath + "\t" + nbdConfig->imgname + "\n"; + } else if (-1 == flag) { + record = "-\t" + nbdConfig->devpath + "\n"; } + write(fd, record.c_str(), record.size()); + close(fd); } static int NBDConnect() { @@ -248,7 +157,7 @@ static int NBDConnect() { if (ret < 0) { ::write(waitConnectPipe[1], &connectionRes, sizeof(connectionRes)); } else { - AddRecord(); + AddRecord(1); connectionRes = 0; ::write(waitConnectPipe[1], &connectionRes, sizeof(connectionRes)); nbdTool->RunServerUntilQuit(); @@ -301,7 +210,7 @@ static int CurveNbdMain(int argc, const char* argv[]) { return -EINVAL; } - DelRecord(); + AddRecord(-1); break; } case Command::List: {