Skip to content

Commit

Permalink
nbd: add auto map at boot
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanHai committed May 14, 2021
1 parent 4b772c2 commit c119d4b
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions curve-ansible/roles/install_package/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local_nbd_package_path: ../../nbd-package
snapshot_clone_server_log_dir: /data/log/curve/snapshotclone
local_snapshotcloneserver_package_path: ../curve-snapshotcloneserver
local_monitor_package_path: ../../curve-monitor
curvetab_path: /etc/curve

local_snapshotcloneserver_nginx_package_path: ../curve-nginx
snapshotcloneserver_nginx_dir: /etc/curve/nginx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@
local_file_path: "{{ local_nbd_package_path }}/bin/"
file_mode: 0755
include_tasks: copy_file_to_remote.yml

- name: install curvetab
vars:
remote_dir_name: "{{ curvetab_path }}"
local_file_path: "{{ local_nbd_package_path }}/etc/"
file_mode: 0755
include_tasks: copy_file_to_remote.yml

- name: add nbd auto start at boot
block:
- name: get distro name
vars:
distro:
include_tasks: common_tasks/get_distro_name.yml
- name: copy map_curve_disk.sh to init.d
vars:
remote_dir_name: "/etc/init.d"
local_file_path: "{{ curve_bin_dir }}/map_curve_disk.sh"
file_mode: 0755
include_tasks: copy_file_to_remote.yml
- name: enable on debain ubuntu
shell: sudo update-rc.d map_curve_disk.sh defaults
when: "'Ubuntu' in distro or 'Debian' in distro"
- name: enable on centos
shell: sudo chkconfig --add map_curve_disk.sh && sudo chkconfig map_curve_disk.sh on
when: "'CentOS' in distro"
3 changes: 3 additions & 0 deletions mk-tar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ cp bazel-bin/nebd/src/part2/nebd-server build/nebd-package/bin

# step 4.2 prepare for curve-nbd package
mkdir -p build/nbd-package/bin
mkdir -p build/nbd-package/etc
cp bazel-bin/nbd/src/curve-nbd build/nbd-package/bin
cp nbd/nbd-package/usr/bin/map_curve_disk.sh build/nbd-package/bin
cp nbd/nbd-package/etc/curve/curvetab build/nbd-package/etc

#step5 打包tar包
echo "start make tarball"
Expand Down
9 changes: 9 additions & 0 deletions nbd/nbd-package/DEBIAN/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -e

chmod +x /usr/bin/map_curve_disk.sh
cp /usr/bin/map_curve_disk.sh /etc/init.d
update-rc.d map_curve_disk.sh defaults

exit 0
8 changes: 8 additions & 0 deletions nbd/nbd-package/DEBIAN/prerm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

set -e

update-rc.d map_curve_disk.sh remove
rm -f /etc/init.d/map_curve_disk.sh

exit 0
9 changes: 9 additions & 0 deletions nbd/nbd-package/etc/curve/curvetab
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# /etc/curvetab
# 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'
#
# format: device image mount
# cbd:pool//curvefile_test_ /dev/nbd0 /curve
1 change: 1 addition & 0 deletions nbd/src/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

using std::cerr;

Expand Down
80 changes: 80 additions & 0 deletions nbd/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,84 @@ static void Usage() {
<< std::endl;
}

// use for record image and device info to auto map when boot
static void AddRecord() {
std::fstream fs;
// check if exist
fs.open(CURVETAB_PATH, std::fstream::in);
if (!fs.is_open()) {
std::cerr << "curve-nbd: open curvetab file failed.";
}

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();
// write new record
fs.open(CURVETAB_PATH, std::fstream::app);
if (!fs.is_open()) {
std::cerr << "curve-nbd: open curvetab file failed.";
}
fs << nbdConfig->imgname << '\t' << nbdConfig->devpath << '\n';
fs.close();
}

// delete record when unmap
static void DelRecord() {
std::fstream fs;
fs.open(CURVETAB_PATH, std::fstream::in);
if (!fs.is_open()) {
std::cerr << "curve-nbd: open curvetab file failed.";
}

std::string lineString;
std::string newContent;
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();
// update record info
fs.open(CURVETAB_PATH, std::fstream::out);
fs << newContent;
fs.close();
}

static int NBDConnect() {
int waitConnectPipe[2];

Expand Down Expand Up @@ -140,6 +218,7 @@ static int NBDConnect() {
if (ret < 0) {
::write(waitConnectPipe[1], &connectionRes, sizeof(connectionRes));
} else {
AddRecord();
connectionRes = 0;
::write(waitConnectPipe[1], &connectionRes, sizeof(connectionRes));
nbdTool->RunServerUntilQuit();
Expand Down Expand Up @@ -192,6 +271,7 @@ static int CurveNbdMain(int argc, const char* argv[]) {
return -EINVAL;
}

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

0 comments on commit c119d4b

Please sign in to comment.