| 1 | #include "mount.hpp" |
| 2 | |
| 3 | #include "ServiceManager.hpp" |
| 4 | |
| 5 | #include <mos/syscall/usermode.h> |
| 6 | |
| 7 | RegisterUnit(mount, Mount); |
| 8 | |
| 9 | Mount::Mount(const std::string &id, toml::table &table, std::shared_ptr<const Template> template_, const ArgumentMap &args) |
| 10 | : Unit(id, table, template_, args), // |
| 11 | mount_point(PopArg(table, key: "mount_point" )), // |
| 12 | fs_type(PopArg(table, key: "fs_type" )), // |
| 13 | options(PopArg(table, key: "options" )), // |
| 14 | device(PopArg(table, key: "device" )) // |
| 15 | { |
| 16 | if (mount_point.empty()) |
| 17 | std::cerr << "mount: missing mount_point" << std::endl; |
| 18 | if (fs_type.empty()) |
| 19 | std::cerr << "mount: missing fs_type" << std::endl; |
| 20 | if (device.empty()) |
| 21 | std::cerr << "mount: missing device" << std::endl; |
| 22 | } |
| 23 | |
| 24 | bool Mount::Start() |
| 25 | { |
| 26 | status.Starting(); |
| 27 | if (syscall_vfs_mount(device: device.c_str(), mount_point: mount_point.c_str(), fs_type: fs_type.c_str(), options: options.c_str()) != 0) |
| 28 | { |
| 29 | status.Failed(msg: strerror(errno)); |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | status.Started(); |
| 34 | ServiceManager->OnUnitStarted(unit: this); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | bool Mount::Stop() |
| 39 | { |
| 40 | status.Stopping(); |
| 41 | std::cout << "stopping mount " << id << std::endl; |
| 42 | status.Inactive(); |
| 43 | const auto err = syscall_vfs_unmount(mount_point: mount_point.c_str()); |
| 44 | if (err != 0) |
| 45 | { |
| 46 | errno = -err; |
| 47 | status.Failed(msg: strerror(errno)); |
| 48 | return false; |
| 49 | } |
| 50 | ServiceManager->OnUnitStopped(unit: this); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void Mount::onPrint(std::ostream &os) const |
| 55 | { |
| 56 | os << " mount_point: " << this->mount_point << std::endl; |
| 57 | os << " fs_type: " << this->fs_type << std::endl; |
| 58 | os << " options: " << this->options << std::endl; |
| 59 | os << " device: " << this->device << std::endl; |
| 60 | } |
| 61 | |