diff options
-rw-r--r-- | hscript/CMakeLists.txt | 2 | ||||
-rw-r--r-- | hscript/disk.cc | 46 | ||||
-rw-r--r-- | hscript/script.cc | 9 |
3 files changed, 53 insertions, 4 deletions
diff --git a/hscript/CMakeLists.txt b/hscript/CMakeLists.txt index ed3d58f..9043e2b 100644 --- a/hscript/CMakeLists.txt +++ b/hscript/CMakeLists.txt @@ -14,7 +14,7 @@ set(HSCRIPT_INCLUDE add_library(hscript ${HSCRIPT_SOURCE}) target_compile_features(hscript PRIVATE cxx_nullptr) target_compile_features(hscript PUBLIC cxx_unicode_literals) -target_link_libraries(hscript ${BLKID_LIBRARIES} ${LIBUDEV_LIBRARIES}) +target_link_libraries(hscript ${BLKID_LIBRARIES} ${LIBUDEV_LIBRARIES} ${PARTED_LIBRARIES}) install(TARGETS hscript DESTINATION lib) install(FILES ${HSCRIPT_INCLUDE} DESTINATION include/hscript) diff --git a/hscript/disk.cc b/hscript/disk.cc index 3423928..c02d4c8 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -18,6 +18,7 @@ # include <assert.h> /* assert */ # include <blkid/blkid.h> /* blkid_get_tag_value */ # include <libudev.h> /* udev_* */ +# include <parted/parted.h> /* ped_* */ # include <sys/mount.h> /* mount */ # include <sys/stat.h> /* mkdir, stat */ # include <sys/types.h> /* S_* */ @@ -183,9 +184,50 @@ bool DiskLabel::validate(ScriptOptions options) const { return true; } -bool DiskLabel::execute(ScriptOptions) const { - /* TODO XXX NOTIMPLEMENTED */ +bool DiskLabel::execute(ScriptOptions options) const { + std::string type_str; + switch(this->type()) { + case APM: + type_str = "apm"; + break; + case MBR: + type_str = "mbr"; + break; + case GPT: + type_str = "gpt"; + break; + } + + if(options.test(Simulate)) { + std::cout << "parted -ms " << this->device() << " mklabel " + << type_str << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + PedDevice *pdevice = ped_device_get(this->device().c_str()); + PedDiskType *label = ped_disk_type_get(type_str.c_str()); + if(label == nullptr) { + output_error("installfile:" + std::to_string(this->lineno()), + "disklabel: Parted does not support label type " + + type_str + "!"); + return false; + } + + /* REQ: Runner.Execute.disklabel.Overwrite */ + ped_disk_clobber(pdevice); + PedDisk *disk = ped_disk_new_fresh(pdevice, label); + if(disk == nullptr) { + output_error("installfile:" + std::to_string(this->lineno()), + "disklabel: internal error creating new " + + type_str + " label on " + _block); + return false; + } + + return (ped_disk_commit_to_dev(disk) == 0); +#else return false; +#endif /* HAS_INSTALL_ENV */ } diff --git a/hscript/script.cc b/hscript/script.cc index cd15089..b913057 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -876,7 +876,14 @@ bool Script::execute() const { } } - /* disklabel */ + /* REQ: Runner.Execute.disklabel */ + for(auto &label : this->internal->disklabels) { + if(!label->execute(opts)) { + EXECUTE_FAILURE("disk"); + return false; + } + } + /* partition */ /* encrypt PVs */ /* lvm_pv */ |