Skip to content

Commit

Permalink
use log to replace filelock
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanHai committed May 19, 2021
1 parent d719751 commit 593b92b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 111 deletions.
4 changes: 2 additions & 2 deletions nbd/nbd-package/etc/curve/curvetab
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 38 additions & 6 deletions nbd/nbd-package/usr/map_curve_disk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ]
Expand Down Expand Up @@ -77,6 +108,7 @@ case $1 in
;;
esac
done
dealcurvetab
map
;;
*)
Expand Down
115 changes: 12 additions & 103 deletions nbd/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/

#include <sys/wait.h>
#include <sys/file.h>
#include <ext/stdio_filebuf.h>
#include <unistd.h>

#include <iostream>
Expand Down Expand Up @@ -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<std::string> 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<std::string> 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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -301,7 +210,7 @@ static int CurveNbdMain(int argc, const char* argv[]) {
return -EINVAL;
}

DelRecord();
AddRecord(-1);
break;
}
case Command::List: {
Expand Down

0 comments on commit 593b92b

Please sign in to comment.