diff --git a/.github/workflows/Linux.yml b/.github/workflows/Linux.yml index daec56ff..3fa0cced 100644 --- a/.github/workflows/Linux.yml +++ b/.github/workflows/Linux.yml @@ -40,26 +40,8 @@ jobs: cd module ./mk.sh build - - name: Build blksnap-tools - working-directory: tools/blksnap + - name: Build blksnap-dev, blksnap-tools and blksnap-tests + working-directory: . run: | - mkdir bin - cd bin - cmake .. - make - - - name: Build blksnap-dev - working-directory: lib/blksnap - run: | - mkdir bin - cd bin - cmake .. - make - - - name: Build blksnap-tests - working-directory: tests/cpp - run: | - mkdir bin - cd bin - cmake .. + cmake . make diff --git a/.gitignore b/.gitignore index 1d667971..b9a822aa 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,11 @@ !.clang-format !.circleci cmake* +CMakeCache.txt +CMakeFiles +Makefile +obj-*-linux-gnu +debian/ /build/ /lib/blksnap/bin/ /tools/blksnap/bin/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..87b6561f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0+ + +cmake_minimum_required(VERSION 3.5) +project(blksnap) + +set(CMAKE_CXX_STANDARD 14) + +add_subdirectory(${CMAKE_SOURCE_DIR}/lib/blksnap) +add_subdirectory(${CMAKE_SOURCE_DIR}/tools/blksnap) +add_subdirectory(${CMAKE_SOURCE_DIR}/tests/cpp) + +if(EXISTS ${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in) + configure_file(${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in + ${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake @ONLY + ) + add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/cmake/cmake_uninstall.cmake") +endif() \ No newline at end of file diff --git a/README.md b/README.md index 6259c876..5d39e888 100644 --- a/README.md +++ b/README.md @@ -81,24 +81,7 @@ The program allows for execution of individual ioctls of the blksnap module. The interface of the program may seem inconvenient to the user, since it is assumed that it will be called by other applications. ### How to build -Installing the necessary deb packages. -``` bash -sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev -``` -Or installing the necessary rpm packages. -``` bash -sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static -``` -Build. -``` bash -cd ./tools/blksnap -mkdir bin -cd bin -cmake .. -make -``` - -For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages) +See [how to build library, tools and tests](#how-to-build-library-tools-and-tests) ## Library The dynamic C library is not needed to work with blksnap. File @@ -107,24 +90,7 @@ But to demonstrate how to call the ioctl, a static c++ library was created. The library can also help you quickly create a working prototype. In the project, the library is used for tests. ### How to build -Installing the necessary deb packages. -``` bash -sudo apt install g++ cmake uuid-dev libboost-filesystem-dev -``` -Or installing the necessary rpm packages. -``` bash -sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static -``` -Build. -``` bash -cd ./lib/blksnap -mkdir bin -cd bin -cmake .. -make -``` - -For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages) +See [how to build library, tools and tests](#how-to-build-library-tools-and-tests) ## Tests The test scripts are written in bash and use the blksnap tool to control @@ -133,6 +99,19 @@ the module. To implement complex test algorithms, С++ tests are implemented. C++ tests use the static library libblksnap.a and it must be compiled to build С++ tests. ### How to build +See [how to build library, tools and tests](#how-to-build-library-tools-and-tests) + +### How to run all usage tests +``` bash +# change working directory to the tests one, for example for debian package is /opt/blksnap/tests +cd /opt/blksnap/tests +# execute all tests script +sudo ./all.sh +# or for logging the output to a file +sudo ./all.sh 2>&1 | tee -a /tmp/blksnap_test_$(date -u '+%Y-%m-%d_%H-%M-%S').log +``` + +## How to build library, tools and tests Installing the necessary deb packages. ``` bash sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev libssl-dev @@ -143,26 +122,19 @@ sudo yum install g++ cmake libuuid-devel boost-static libstdc++-static openssl-s ``` Build. ``` bash -cd ./tests/cpp -mkdir bin -cd bin -cmake .. +cmake . make ``` - -For deb packages see [how to create dev, tools and tests deb packages](#how-to-create-dev-tools-and-tests-deb-packages) - -### How to run all usage tests -``` bash -# change working directory to the tests one, for example for debian package is /opt/blksnap/tests -cd /opt/blksnap/tests -# execute all tests script -sudo ./all.sh -# or for logging the output to a file -sudo ./all.sh 2>&1 | tee -a /tmp/blksnap_test_$(date -u '+%Y-%m-%d_%H-%M-%S').log +Install (but it is recommended to use packages instead, for example the [debian](#how-to-create-dev-tools-and-tests-deb-packages) ones) +``` +sudo make install +``` +Uninstall (if needed) +``` +sudo make uninstall ``` -## How to create dev, tools and tests deb packages +### How to create dev, tools and tests deb packages ``` bash sudo apt install g++ cmake uuid-dev libboost-program-options-dev libboost-filesystem-dev libssl-dev debhelper cd ./pkg/deb diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in new file mode 100644 index 00000000..d925e0e2 --- /dev/null +++ b/cmake/cmake_uninstall.cmake.in @@ -0,0 +1,22 @@ +if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt") + message(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"") +endif() + +file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files) +string(REPLACE "\n" ";" files "${files}") +foreach(file ${files}) + message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") + if(EXISTS "$ENV{DESTDIR}${file}") + exec_program( + "@CMAKE_COMMAND@" ARGS "-E rm -f \"$ENV{DESTDIR}${file}\"" + OUTPUT_VARIABLE rm_out + RETURN_VALUE rm_retval + ) + if("${rm_retval}" STREQUAL 0) + else() + message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") + endif() + else() + message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") + endif() +endforeach() \ No newline at end of file diff --git a/lib/blksnap/CMakeLists.txt b/lib/blksnap/CMakeLists.txt index 91e04fab..e551fcd4 100644 --- a/lib/blksnap/CMakeLists.txt +++ b/lib/blksnap/CMakeLists.txt @@ -17,7 +17,7 @@ # along with this program. If not, see . cmake_minimum_required(VERSION 3.5) -project(blksnap) +project(blksnap-dev) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc -pthread") @@ -30,8 +30,6 @@ if (NOT LIBUUID_LIBRARY) message(FATAL_ERROR "libuuid not found. please install uuid-dev or libuuid-devel package.") endif () -include_directories(${CMAKE_SOURCE_DIR}/../../include) - set(SOURCE_FILES Blksnap.cpp Cbt.cpp @@ -39,11 +37,13 @@ set(SOURCE_FILES Session.cpp ) -add_library(${PROJECT_NAME} ${SOURCE_FILES}) +add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES}) +set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "blksnap") -set_target_properties(${PROJECT_NAME} - PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ - #LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ - #RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ -) +target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../include) + +install(TARGETS ${PROJECT_NAME} DESTINATION lib) + +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../include/blksnap + DESTINATION include + FILES_MATCHING PATTERN "*.h") diff --git a/lib/blksnap/Session.cpp b/lib/blksnap/Session.cpp index 8ba6f28f..38b315f2 100644 --- a/lib/blksnap/Session.cpp +++ b/lib/blksnap/Session.cpp @@ -204,7 +204,7 @@ namespace static void FallocateStorage(const std::string& filename, const off_t filesize) { - int fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE); + int fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE, 0600); if (fd < 0) throw std::system_error(errno, std::generic_category(), "[TBD]Failed to create file for diff storage."); diff --git a/pkg/deb/blksnap/blksnap-tests.install b/pkg/deb/blksnap/blksnap-tests.install index d6eba11a..afcee1b0 100644 --- a/pkg/deb/blksnap/blksnap-tests.install +++ b/pkg/deb/blksnap/blksnap-tests.install @@ -1 +1 @@ -opt +opt/blksnap/tests diff --git a/pkg/deb/blksnap/rules b/pkg/deb/blksnap/rules index cdf91660..393953af 100755 --- a/pkg/deb/blksnap/rules +++ b/pkg/deb/blksnap/rules @@ -1,6 +1,19 @@ #!/usr/bin/make -f # -*- makefile -*- -include /usr/share/dpkg/pkg-info.mk +include /usr/share/dpkg/default.mk +export DEB_BUILD_MAINT_OPTIONS = hardening=+all +DPKG_EXPORT_BUILDFLAGS = 1 +CFLAGS+=$(CPPFLAGS) +CXXFLAGS+=$(CPPFLAGS) + + +DEB_CMAKE_EXTRA_FLAGS = \ + -DCMAKE_INSTALL_PREFIX=/usr \ + -DCMAKE_BUILD_TYPE=RelWithDebInfo %: dh $@ + +override_dh_auto_configure: + dh_auto_configure -- $(DEB_CMAKE_EXTRA_FLAGS) + diff --git a/pkg/deb/build-blksnap.sh b/pkg/deb/build-blksnap.sh index 60956e58..1bce6238 100755 --- a/pkg/deb/build-blksnap.sh +++ b/pkg/deb/build-blksnap.sh @@ -11,76 +11,18 @@ CURR_DIR=$(pwd) cd "../../" ROOT_DIR=$(pwd) -BUILD_DIR="build/pkg" -rm -rf ${BUILD_DIR} -mkdir -p ${BUILD_DIR} +# prepare debian directory +rm -rf ${ROOT_DIR}/debian +cp -r ${CURR_DIR}/blksnap ${ROOT_DIR}/debian -### build library -mkdir -p "lib/blksnap/bin" -cd "lib/blksnap/bin" -cmake ../ -make -cd ${ROOT_DIR} - -# copy library -mkdir -p ${BUILD_DIR}/usr/lib/ -cp lib/libblksnap.a ${BUILD_DIR}/usr/lib/ -# copy include -mkdir -p ${BUILD_DIR}/usr/include/blksnap/ -cp include/blksnap/* ${BUILD_DIR}/usr/include/blksnap/ - -### build tools -SOURCE_DIR="tools/blksnap/bin" -TARGET_DIR="usr/sbin" - -# build -rm -rf ${SOURCE_DIR}/* -mkdir -p ${SOURCE_DIR} -cd ${SOURCE_DIR} -cmake ../ -make -cd ${ROOT_DIR} - -# copy binaries -mkdir -p ${BUILD_DIR}/${TARGET_DIR} -cp ${SOURCE_DIR}/blksnap ${BUILD_DIR}/${TARGET_DIR}/ -chmod +x ${BUILD_DIR}/${TARGET_DIR}/* - -### build tests -SOURCE_DIR="tests" -TARGET_DIR="opt/blksnap/tests" - -# build -rm -rf ${SOURCE_DIR}/cpp/bin/* -mkdir -p ${SOURCE_DIR}/cpp/bin/* -cd ${SOURCE_DIR}/cpp/bin -cmake ../ -make -cd ${ROOT_DIR} - -# copy binaries and scripts -mkdir -p ${BUILD_DIR}/${TARGET_DIR} -for FILE in ${SOURCE_DIR}/* -do - if [ -f "${FILE}" ] - then - cp ${FILE} ${BUILD_DIR}/${TARGET_DIR}/ - fi -done -chmod +x ${BUILD_DIR}/${TARGET_DIR}/* - - -### prepare other package files -cp -r ${CURR_DIR}/blksnap ${BUILD_DIR}/debian - -cat > ${BUILD_DIR}/debian/changelog << EOF +cat > ${ROOT_DIR}/debian/changelog << EOF blksnap (${VERSION}) stable; urgency=low * Release. -- Veeam Software Group GmbH $(date -R) EOF -cd ${BUILD_DIR} +cd ${ROOT_DIR} # build backage dpkg-buildpackage -us -uc -b diff --git a/tests/blksnap.sh b/tests/blksnap.sh index 0ac0ba72..9034939c 100755 --- a/tests/blksnap.sh +++ b/tests/blksnap.sh @@ -6,7 +6,7 @@ if [ -f "/usr/bin/blksnap" ] || [ -f "/usr/sbin/blksnap" ] then BLKSNAP=blksnap else - BLKSNAP="$(cd ../; pwd)/tools/blksnap/bin/blksnap" + BLKSNAP="$(cd ../; pwd)/tools/blksnap/blksnap" fi ID="" diff --git a/tests/cpp/CMakeLists.txt b/tests/cpp/CMakeLists.txt index 4720e910..4b128bcb 100644 --- a/tests/cpp/CMakeLists.txt +++ b/tests/cpp/CMakeLists.txt @@ -20,89 +20,50 @@ if (NOT OPENSSL_LIBRARIES) message(FATAL_ERROR "openssl not found. please install libssl-dev package.") endif () -include_directories(${CMAKE_SOURCE_DIR}/../../include) -set(BLKSNAP_LIBRARY "${CMAKE_SOURCE_DIR}/../../lib/libblksnap.a") add_subdirectory(helpers) -set(CORRUPT_SRC - TestSector.cpp - corrupt.cpp -) +set(TESTS_LIBS blksnap-dev Helpers::Lib Boost::program_options Boost::filesystem ${LIBUUID_LIBRARY}) + set(TEST_CORRUPT test_corrupt) -add_executable(${TEST_CORRUPT} ${CORRUPT_SRC}) -target_link_libraries(${TEST_CORRUPT} PRIVATE Helpers::Lib) -target_link_libraries(${TEST_CORRUPT} PRIVATE ${BLKSNAP_LIBRARY}) -target_link_libraries(${TEST_CORRUPT} PRIVATE Boost::program_options) -target_link_libraries(${TEST_CORRUPT} PRIVATE Boost::filesystem ) -target_link_libraries(${TEST_CORRUPT} PRIVATE ${LIBUUID_LIBRARY}) +add_executable(${TEST_CORRUPT} TestSector.cpp corrupt.cpp) +target_link_libraries(${TEST_CORRUPT} PRIVATE ${TESTS_LIBS}) target_include_directories(${TEST_CORRUPT} PRIVATE ./) -set_target_properties(${TEST_CORRUPT} - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ -) -set(CBT_SRC - cbt.cpp -) set(TEST_CBT test_cbt) -add_executable(${TEST_CBT} ${CBT_SRC}) -target_link_libraries(${TEST_CBT} PRIVATE Helpers::Lib) -target_link_libraries(${TEST_CBT} PRIVATE ${BLKSNAP_LIBRARY}) -target_link_libraries(${TEST_CBT} PRIVATE Boost::program_options) -target_link_libraries(${TEST_CBT} PRIVATE Boost::filesystem ) -target_link_libraries(${TEST_CBT} PRIVATE ${LIBUUID_LIBRARY}) +add_executable(${TEST_CBT} cbt.cpp) +target_link_libraries(${TEST_CBT} PRIVATE ${TESTS_LIBS}) target_include_directories(${TEST_CBT} PRIVATE ./) -set_target_properties(${TEST_CBT} - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ -) -set(DIFF_STORAGE_SRC - TestSector.cpp - diff_storage.cpp -) set(TEST_DIFF_STORAGE test_diff_storage) -add_executable(${TEST_DIFF_STORAGE} ${DIFF_STORAGE_SRC}) -target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE Helpers::Lib) -target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE ${BLKSNAP_LIBRARY}) -target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE Boost::program_options) -target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE Boost::filesystem ) -target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE ${LIBUUID_LIBRARY}) +add_executable(${TEST_DIFF_STORAGE} TestSector.cpp diff_storage.cpp) +target_link_libraries(${TEST_DIFF_STORAGE} PRIVATE ${TESTS_LIBS}) target_include_directories(${TEST_DIFF_STORAGE} PRIVATE ./) -set_target_properties(${TEST_DIFF_STORAGE} - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ -) -set(BOUNDARY_SRC - TestSector.cpp - boundary.cpp -) set(TEST_BOUNDARY test_boundary) -add_executable(${TEST_BOUNDARY} ${BOUNDARY_SRC}) -target_link_libraries(${TEST_BOUNDARY} PRIVATE Helpers::Lib) -target_link_libraries(${TEST_BOUNDARY} PRIVATE ${BLKSNAP_LIBRARY}) -target_link_libraries(${TEST_BOUNDARY} PRIVATE Boost::program_options) -target_link_libraries(${TEST_BOUNDARY} PRIVATE Boost::filesystem ) -target_link_libraries(${TEST_BOUNDARY} PRIVATE ${LIBUUID_LIBRARY}) +add_executable(${TEST_BOUNDARY} TestSector.cpp boundary.cpp) +target_link_libraries(${TEST_BOUNDARY} PRIVATE ${TESTS_LIBS}) target_include_directories(${TEST_BOUNDARY} PRIVATE ./) -set_target_properties(${TEST_BOUNDARY} - PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ -) -set(PERFORMANCE_SRC - performance.cpp -) set(TEST_PERFORMANCE test_performance) -add_executable(${TEST_PERFORMANCE} ${PERFORMANCE_SRC}) -target_link_libraries(${TEST_PERFORMANCE} PRIVATE Helpers::Lib) -target_link_libraries(${TEST_PERFORMANCE} PRIVATE ${BLKSNAP_LIBRARY}) -target_link_libraries(${TEST_PERFORMANCE} PRIVATE Boost::program_options) -target_link_libraries(${TEST_PERFORMANCE} PRIVATE Boost::filesystem ) -target_link_libraries(${TEST_PERFORMANCE} PRIVATE ${LIBUUID_LIBRARY}) +add_executable(${TEST_PERFORMANCE} performance.cpp) +target_link_libraries(${TEST_PERFORMANCE} PRIVATE ${TESTS_LIBS}) target_include_directories(${TEST_PERFORMANCE} PRIVATE ./) -set_target_properties(${TEST_PERFORMANCE} + +set_target_properties(${TEST_CORRUPT} ${TEST_CBT} ${TEST_DIFF_STORAGE} ${TEST_BOUNDARY} ${TEST_PERFORMANCE} PROPERTIES - RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}../../ + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../ +) + +#add_custom_target(blksnap-tests DEPENDS ${TEST_CORRUPT} ${TEST_CBT} ${TEST_DIFF_STORAGE} ${TEST_BOUNDARY} ${TEST_PERFORMANCE}) + +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../ + DESTINATION /opt/blksnap/tests + USE_SOURCE_PERMISSIONS + PATTERN "*.sh" + PATTERN "build_and_install.sh" EXCLUDE + PATTERN "cpp" EXCLUDE +) + +install(TARGETS ${TEST_CORRUPT} ${TEST_CBT} ${TEST_DIFF_STORAGE} ${TEST_BOUNDARY} ${TEST_PERFORMANCE} + DESTINATION /opt/blksnap/tests ) diff --git a/tools/blksnap/CMakeLists.txt b/tools/blksnap/CMakeLists.txt index 5a7e5ebd..03622efd 100644 --- a/tools/blksnap/CMakeLists.txt +++ b/tools/blksnap/CMakeLists.txt @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0+ cmake_minimum_required(VERSION 3.5) -project(blksnap) +project(blksnap-tools) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++ -static-libgcc") @@ -14,15 +14,13 @@ if (NOT LIBUUID_LIBRARY) message(FATAL_ERROR "libuuid not found. please install uuid-dev or libuuid-devel package.") endif () -include_directories(${CMAKE_SOURCE_DIR}/../../include) - -set(SOURCE_FILES main.cpp) - -add_executable(${PROJECT_NAME} ${SOURCE_FILES}) +add_executable(${PROJECT_NAME} main.cpp) +set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "blksnap") target_compile_definitions(${PROJECT_NAME} PUBLIC BLK_SNAP_MODIFICATION) -target_link_libraries(${PROJECT_NAME} PRIVATE Boost::program_options) -target_link_libraries(${PROJECT_NAME} PRIVATE Boost::filesystem) -target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBUUID_LIBRARY}) +set(TOOLS_LIBS blksnap-dev ${LIBUUID_LIBRARY} Boost::filesystem Boost::program_options) +target_link_libraries(${PROJECT_NAME} PRIVATE ${TOOLS_LIBS}) target_include_directories(${PROJECT_NAME} PRIVATE ./) + +install(TARGETS ${PROJECT_NAME} DESTINATION sbin) diff --git a/tools/blksnap/main.cpp b/tools/blksnap/main.cpp index 34a9b224..f266c045 100644 --- a/tools/blksnap/main.cpp +++ b/tools/blksnap/main.cpp @@ -800,7 +800,7 @@ class StretchSnapshotArgsProc : public IArgsProc fs::remove(filepath); filename = filepath.string(); - fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE); + fd = ::open(filename.c_str(), O_CREAT | O_RDWR | O_EXCL | O_LARGEFILE, 0600); if (fd < 0) throw std::system_error(errno, std::generic_category(), "[TBD]Failed to create file for diff storage."); m_allocated_sectFiles.push_back(filename);