From 1fa3717743c217bb3dbe643a37a7ba96b5ff0ebd Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sun, 20 Oct 2019 00:19:53 -0500 Subject: Make Installation Environment code conditional --- .gitlab-ci.yml | 2 +- CMakeLists.txt | 20 +++++++++++++++----- hscript/disk.cc | 35 ++++++++++++++++++++++++----------- hscript/meta.cc | 31 ++++++++++++++++++++++--------- hscript/network.cc | 16 ++++++++++------ hscript/user.cc | 6 ++++-- 6 files changed, 76 insertions(+), 34 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index c8f7b74..4a4c0c2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,7 +5,7 @@ build: script: - mkdir build - cd build - - cmake .. -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON -DVALGRIND=ON + - cmake .. -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON -DVALGRIND=ON -DINSTALL=OFF - make -j4 artifacts: paths: diff --git a/CMakeLists.txt b/CMakeLists.txt index 8221928..94af0d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,16 @@ cmake_minimum_required(VERSION 3.4) include(FindPkgConfig) +include(CheckIncludeFiles) project(Horizon LANGUAGES CXX VERSION 0.1.0) -pkg_check_modules(BLKID REQUIRED blkid) -pkg_check_modules(LIBUDEV REQUIRED libudev) -pkg_check_modules(PARTED REQUIRED libparted) -find_library(BCNM_LIBRARY REQUIRED wpactrl PATH_SUFFIXES bcnm) - option(BUILD_TOOLS "Enable building of tools (Validator, Simulator, etc)" ON) option(COVERAGE "Build for code coverage tests (slow)" OFF) option(VALGRIND "Run Valgrind during test phase" OFF) +option(INSTALL "Build Installation Environment support (Linux only)" ON) option(UNSUPPORTED_NONFREE_FIRMWARE "Support loading and installation of non-libre firmware (DANGEROUS)" OFF) mark_as_advanced(FORCE UNSUPPORTED_NONFREE_FIRMWARE) @@ -22,6 +19,19 @@ IF(UNSUPPORTED_NONFREE_FIRMWARE) add_definitions(-DNON_LIBRE_FIRMWARE) ENDIF(UNSUPPORTED_NONFREE_FIRMWARE) +check_include_files(linux/wireless.h HAVE_LINUX_WIRELESS_H) +IF(NOT HAVE_LINUX_WIRELESS_H) +SET(INSTALL OFF) +ENDIF(NOT HAVE_LINUX_WIRELESS_H) + +IF(INSTALL) + add_definitions(-DHAS_INSTALL_ENV) + pkg_check_modules(BLKID REQUIRED blkid) + pkg_check_modules(LIBUDEV REQUIRED libudev) + pkg_check_modules(PARTED REQUIRED libparted) + find_library(BCNM_LIBRARY REQUIRED wpactrl PATH_SUFFIXES bcnm) +ENDIF(INSTALL) + IF(COVERAGE) SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} --coverage") SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage") diff --git a/hscript/disk.cc b/hscript/disk.cc index 713a31d..9d700dd 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -11,14 +11,16 @@ */ #include -#include /* blkid_get_tag_value */ #include /* strerror */ #include #include -#include /* mount */ -#include /* mkdir, stat */ -#include /* S_* */ -#include /* access */ +#ifdef HAS_INSTALL_ENV +# include /* blkid_get_tag_value */ +# include /* mount */ +# include /* mkdir, stat */ +# include /* S_* */ +# include /* access */ +#endif /* HAS_INSTALL_ENV */ #include "disk.hh" #include "util/output.hh" @@ -117,11 +119,13 @@ bool Mount::validate(ScriptOptions options) const { /* We only validate if running in an Installation Environment. */ if(!options.test(InstallEnvironment)) return true; +#ifdef HAS_INSTALL_ENV /* XXX TODO: This will fail validation if the block device does not * already exist. However, we must take in to account that block devices * may not yet exist during the script validation phase. This check may * need to happen in Script::validate like the Uniqueness tests. */ return(access(this->device().c_str(), F_OK) == 0); +#endif /* HAS_INSTALL_ENV */ } bool Mount::execute(ScriptOptions options) const { @@ -131,7 +135,9 @@ bool Mount::execute(ScriptOptions options) const { /* We have to get the filesystem for the node. */ if(options.test(Simulate)) { fstype = "auto"; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { fstype = blkid_get_tag_value(nullptr, "TYPE", this->device().c_str()); if(fstype == nullptr) { output_error("installfile:" + std::to_string(this->lineno()), @@ -139,7 +145,8 @@ bool Mount::execute(ScriptOptions options) const { this->device()); return false; } - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ output_info("installfile:" + std::to_string(this->lineno()), "mount: mounting " + this->device() + " on " + @@ -150,7 +157,9 @@ bool Mount::execute(ScriptOptions options) const { std::cout << "-o " << this->options() << " "; } std::cout << this->device() << " " << actual_mount << std::endl; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { /* mount */ if(mount(this->device().c_str(), actual_mount.c_str(), fstype, 0, this->options().c_str()) != 0) { @@ -165,7 +174,8 @@ bool Mount::execute(ScriptOptions options) const { return false; } } - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ /* Handle fstab. We're guaranteed to have a /target since mount has * already ran and /target is the first mount done. @@ -183,7 +193,9 @@ bool Mount::execute(ScriptOptions options) const { << "n' " << this->device() << " " << this->mountpoint() << " " << fstype << " " << fstab_opts << " >> /target/etc/fstab" << std::endl; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { if(this->mountpoint() == "/") { /* failure of mkdir will be handled in the !fstab_f case */ mkdir("/target/etc", @@ -198,7 +210,8 @@ bool Mount::execute(ScriptOptions options) const { fstab_f << this->device() << "\t" << this->mountpoint() << "\t" << fstype << "\t" << fstab_opts << "\t0\t" << pass << std::endl; - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ return true; } diff --git a/hscript/meta.cc b/hscript/meta.cc index b7da9d2..02b294d 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -13,7 +13,9 @@ #include #include #include -#include +#ifdef HAS_INSTALL_ENV +# include +#endif /* HAS_INSTALL_ENV */ #include "meta.hh" #include "util/output.hh" @@ -87,14 +89,17 @@ bool Hostname::execute(ScriptOptions opts) const { "hostname: set hostname to '" + actual + "'"); if(opts.test(Simulate)) { std::cout << "hostname " << actual << std::endl; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { if(sethostname(actual.c_str(), actual.size()) == -1) { output_error("installfile:" + std::to_string(this->lineno()), "hostname: failed to set host name", std::string(strerror(errno))); return false; } - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ /* Runner.Execute.hostname.Write. */ output_info("installfile:" + std::to_string(this->lineno()), @@ -102,7 +107,9 @@ bool Hostname::execute(ScriptOptions opts) const { if(opts.test(Simulate)) { std::cout << "printf '%s' " << actual << " > /target/etc/hostname" << std::endl; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { std::ofstream hostname_f("/target/etc/hostname"); if(!hostname_f) { output_error("installfile:" + std::to_string(this->lineno()), @@ -110,7 +117,8 @@ bool Hostname::execute(ScriptOptions opts) const { return false; } hostname_f << actual; - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ /* The second condition ensures that it isn't a single dot that simply * terminates the nodename. */ @@ -121,7 +129,9 @@ bool Hostname::execute(ScriptOptions opts) const { if(opts.test(Simulate)) { std::cout << "printf 'dns_domain_lo=\"" << domain << "\"\\" << "n' >> /target/etc/conf.d/net" << std::endl; - } else { /* LCOV_EXCL_START */ + } +#ifdef HAS_INSTALL_ENV + else { std::ofstream net_conf_f("/target/etc/conf.d/net"); if(!net_conf_f) { output_error("installfile:" + std::to_string(this->lineno()), @@ -130,7 +140,8 @@ bool Hostname::execute(ScriptOptions opts) const { return false; } net_conf_f << "dns_domain_lo\"" << domain << "\"" << std::endl; - } /* LCOV_EXCL_STOP */ + } +#endif /* HAS_INSTALL_ENV */ } return true; @@ -242,7 +253,7 @@ bool Repository::execute(ScriptOptions opts) const { return true; } - /* LCOV_EXCL_START */ +#ifdef HAS_INSTALL_ENV std::ofstream repo_f("/target/etc/apk/repositories", std::ios_base::ate); if(!repo_f) { @@ -255,5 +266,7 @@ bool Repository::execute(ScriptOptions opts) const { repo_f << this->value() << std::endl; return true; - /* LCOV_EXCL_STOP */ +#else + return false; +#endif /* HAS_INSTALL_ENV */ } diff --git a/hscript/network.cc b/hscript/network.cc index 0921aaf..03c5a03 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -12,10 +12,12 @@ #include #include /* inet_pton */ -#include /* struct iwreq */ -#include /* strerror */ -#include /* ioctl, ioctl numbers */ -#include /* close */ +#ifdef HAS_INSTALL_ENV +# include /* struct iwreq */ +# include /* strerror */ +# include /* ioctl, ioctl numbers */ +# include /* close */ +#endif #include "network.hh" #include "util/output.hh" @@ -318,7 +320,7 @@ bool NetSSID::validate(ScriptOptions options) const { return true; } - /* LCOV_EXCL_START */ +#ifdef HAS_INSTALL_ENV struct iwreq request; int my_sock = ::socket(AF_INET, SOCK_STREAM, 0); if(my_sock == -1) { @@ -349,7 +351,9 @@ bool NetSSID::validate(ScriptOptions options) const { } ::close(my_sock); return true; - /* LCOV_EXCL_STOP */ +#else + return false; +#endif } bool NetSSID::execute(ScriptOptions) const { diff --git a/hscript/user.cc b/hscript/user.cc index ec59a67..b288070 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -147,7 +147,7 @@ bool RootPassphrase::execute(ScriptOptions options) const { return true; } - /* LCOV_EXCL_START */ +#ifdef HAS_INSTALL_ENV /* This was tested on gwyn during development. */ std::ifstream old_shadow("/target/etc/shadow"); if(!old_shadow) { @@ -179,7 +179,9 @@ bool RootPassphrase::execute(ScriptOptions options) const { } new_shadow << shadow_stream.str(); return true; - /* LCOV_EXCL_STOP */ +#else + return false; +#endif /* HAS_INSTALL_ENV */ } -- cgit v1.2.3-60-g2f50