-
Notifications
You must be signed in to change notification settings - Fork 522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nbd: add auto map at boot #347
Conversation
5459eab
to
c119d4b
Compare
nbd/src/main.cpp
Outdated
static void AddRecord() { | ||
std::fstream fs; | ||
// check if exist | ||
fs.open(CURVETAB_PATH, std::fstream::in); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read-only open?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read-only open?
std::fstream::in is read-only
nbd/src/main.cpp
Outdated
while (getline(ss, str, '\t')) { | ||
lineArray.push_back(str); | ||
} | ||
if (lineArray.size() != 2 && lineArray.size() != 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstances lineArray.size() == 2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Under what circumstances lineArray.size() == 2
if you only use curve-nbd map, will only image and device write in curvetab like: cbd:pool//test_test_ /dev/nbd0
then if you mkfs on the device, you need record the mount point in the curvetab manually like fstab.
reason for not using fstab is fstab run before rc or system.d at reboot.
nbd/src/main.cpp
Outdated
fs.close(); | ||
return; | ||
} | ||
if (lineArray[0] == nbdConfig->imgname) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if the same mount point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if the same mount point?
Not needed, only record image and device here. Mount point is ot necessary in curvetab.
The correspondence between image device and mount-point is record manually after you mount like fstab
nbd/src/main.cpp
Outdated
} | ||
fs.close(); | ||
// write new record | ||
fs.open(CURVETAB_PATH, std::fstream::app); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a problem to mount multiple volumes at the same time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a problem to mount multiple volumes at the same time?
add flock
nbd/src/main.cpp
Outdated
} | ||
|
||
std::string lineString; | ||
while (getline(fs, lineString)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remarks:// Check if the record exists
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remarks:// Check if the record exists
done
nbd/src/main.cpp
Outdated
std::fstream fs; | ||
fs.open(CURVETAB_PATH, std::fstream::in); | ||
if (!fs.is_open()) { | ||
std::cerr << "curve-nbd: open curvetab file failed."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return?
done
// check if exist | ||
fs.open(CURVETAB_PATH, std::fstream::in); | ||
if (!fs.is_open()) { | ||
std::cerr << "curve-nbd: open curvetab file failed."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return with error code?
nbd/src/main.cpp
Outdated
|
||
std::string lineString; | ||
std::string newContent; | ||
while (getline(fs, lineString)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remarks: // find record
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remarks: // find record
done.
// find record need deleted
nbd/src/main.cpp
Outdated
while (getline(ss, str, '\t')) { | ||
lineArray.push_back(str); | ||
} | ||
if (lineArray.size() != 2 && lineArray.size() != 3) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above
done
nbd/src/main.cpp
Outdated
} | ||
fs.close(); | ||
// update record info | ||
fs.open(CURVETAB_PATH, std::fstream::out); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
judgment : fs.is_open()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
judgment : fs.is_open()
done
5bfbddc
to
d719751
Compare
do | ||
image=$(echo $line | awk '{print $1}') | ||
device=$(echo $line | awk '{print $2}') | ||
mount=$(echo $line | awk '{print $3}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation
done
nbd/src/main.cpp
Outdated
if (0 == flock(fd, LOCK_SH )) { | ||
// check if the record exists | ||
std::string lineString; | ||
while (getline(fs, lineString)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic for parse file in AddRecord
and DelRecord
is highly coincident, you can abstract the comon logic into one function.
nbd/src/main.cpp
Outdated
} | ||
if (lineArray.size() != 2 && lineArray.size() != 3) { | ||
std::cerr << "curve-nbd: curvetab file content error."; | ||
fs.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The close()
is redunrant, because the file stream's destructor will close the file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
close()
is redunrant, because the file stream's destructor will close the file.
the fs will close when goes out of scope. but this fs reused latter in this function. so I think close is needed
nbd/src/main.cpp
Outdated
} | ||
} | ||
} | ||
fs.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should invoke flock(fd, LOCK_UN)
before fs.close()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should invoke
flock(fd, LOCK_UN)
beforefs.close()
.
I think this is ok , because flock() is tied to the file descriptor, it gets released when the file is closed. so flock(fd, LOCK_UN) even not write
nbd/src/main.cpp
Outdated
@@ -95,6 +97,112 @@ static void Usage() { | |||
<< std::endl; | |||
} | |||
|
|||
// use for record image and device info to auto map when boot | |||
static void AddRecord() { | |||
std::fstream fs; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use std::ifstream
to replace std::fstream
and open with std::fstream::in
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
std::ifstream
to replacestd::fstream
and open withstd::fstream::in
.
because fs reused latter in this function as std::fstream::app
nbd/src/main.cpp
Outdated
} | ||
fd = static_cast<__gnu_cxx::stdio_filebuf< char > * const > | ||
(fs.rdbuf())->fd(); | ||
if (0 == flock(fd, LOCK_SH )) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want use the flock
to sync the operation in file, i suggest use the FileLock
class in nebd/src/common/file_lock.cpp.
nbd/src/main.cpp
Outdated
fs.close(); | ||
flock(fd, LOCK_UN); | ||
} | ||
// update record info |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note there is time window in the read and write logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note there is time window in the read and write logic.
what is mean?
34e4652
to
f89fa8d
Compare
@@ -61,6 +61,7 @@ namespace nbd { | |||
#define PROCESS_NAME "curve-nbd" | |||
#define NBD_PATH_PREFIX "/sys/block/nbd" | |||
#define DEV_PATH_PREFIX "/dev/nbd" | |||
#define CURVETAB_PATH "/etc/curve/curvetab" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it's better to named CURVETAB_LOG_PATH
?
unset recordmap[$device] | ||
fi | ||
done < ${confPath} | ||
for key in ${!recordmap[@]};do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for key in ${!recordmap[@]};do
-> for key in ${!recordmap[@]}; do
f89fa8d
to
593b92b
Compare
LGTM |
593b92b
to
4fd181c
Compare
recheck |
1 similar comment
recheck |
recheck |
What problem does this PR solve?
Issue Number: close #xxx
Problem Summary:
What is changed and how it works?
What's Changed:
How it Works:
Side effects(Breaking backward compatibility? Performance regression?):
Check List