|
| 1 | +/* eclean-kernel2 |
| 2 | + * (c) 2017 Michał Górny |
| 3 | + * 2-clause BSD license |
| 4 | + */ |
| 5 | + |
| 6 | +#ifdef HAVE_CONFIG_H |
| 7 | +# include "config.h" |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "ek2/bootloaders/grub2.h" |
| 11 | + |
| 12 | +#include "ek2/util/directorystream.h" |
| 13 | +#include "ek2/util/error.h" |
| 14 | +#include "ek2/util/relativepath.h" |
| 15 | + |
| 16 | +#include <cerrno> |
| 17 | +#include <cstring> |
| 18 | +#include <iostream> |
| 19 | +#include <string> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +extern "C" |
| 23 | +{ |
| 24 | +# include <fcntl.h> |
| 25 | +# include <unistd.h> |
| 26 | +# include <sys/wait.h> |
| 27 | +}; |
| 28 | + |
| 29 | +static const char grub2_autogen_header[] = |
| 30 | + "#\n" |
| 31 | + "# DO NOT EDIT THIS FILE\n" |
| 32 | + "#\n" |
| 33 | + "# It is automatically generated by grub-mkconfig"; |
| 34 | + |
| 35 | +GRUB2::GRUB2(const Options& opts) |
| 36 | + : BootLoader(opts), is_autogenerated_(false), is_multislot_(false) |
| 37 | +{ |
| 38 | + DirectoryStream topds; |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + topds.open(opts.boot_path); |
| 43 | + } |
| 44 | + catch (IOError& e) |
| 45 | + { |
| 46 | + if (e.err() != ENOENT) |
| 47 | + throw; |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // try /boot/grub/grub.cfg first |
| 52 | + try |
| 53 | + { |
| 54 | + grub_dir_.reset(new DirectoryStream(RelativePath(topds, "grub"))); |
| 55 | + grub_cfg_.reset(new RelativePath(grub_dir_, "grub.cfg")); |
| 56 | + process_config(); |
| 57 | + return; |
| 58 | + } |
| 59 | + catch (IOError& e) |
| 60 | + { |
| 61 | + if (e.err() != ENOENT) |
| 62 | + throw; |
| 63 | + } |
| 64 | + |
| 65 | + // fallback to /boot/grub2/grub.cfg |
| 66 | + try |
| 67 | + { |
| 68 | + grub_dir_.reset(new DirectoryStream(RelativePath(topds, "grub2"))); |
| 69 | + grub_cfg_.reset(new RelativePath(grub_dir_, "grub.cfg")); |
| 70 | + process_config(); |
| 71 | + is_multislot_ = true; |
| 72 | + return; |
| 73 | + } |
| 74 | + catch (IOError& e) |
| 75 | + { |
| 76 | + if (e.err() != ENOENT) |
| 77 | + throw; |
| 78 | + } |
| 79 | + |
| 80 | + // no success? make sure it's null again |
| 81 | + grub_cfg_.reset(nullptr); |
| 82 | +} |
| 83 | + |
| 84 | +bool GRUB2::is_installed() const |
| 85 | +{ |
| 86 | + return !!grub_cfg_; |
| 87 | +} |
| 88 | + |
| 89 | +void GRUB2::process_config() |
| 90 | +{ |
| 91 | + int fd = grub_cfg_->file_fd(O_RDONLY); |
| 92 | + |
| 93 | + // check if the config is autogenerated |
| 94 | + char buf[sizeof(grub2_autogen_header)-1]; |
| 95 | + ssize_t rd = read(fd, &buf, sizeof(buf)); |
| 96 | + if (rd == -1) |
| 97 | + throw IOError("Reading failed on " + grub_cfg_->filename(), errno); |
| 98 | + if (rd == sizeof(buf) && !memcmp(buf, grub2_autogen_header, sizeof(buf))) |
| 99 | + is_autogenerated_ = true; |
| 100 | +} |
| 101 | + |
| 102 | +std::unique_ptr<BootLoader> GRUB2::construct(const Options& opts) |
| 103 | +{ |
| 104 | + std::unique_ptr<GRUB2> b{new GRUB2(opts)}; |
| 105 | + |
| 106 | + if (b->is_installed()) |
| 107 | + return std::move(b); |
| 108 | + |
| 109 | + return nullptr; |
| 110 | +} |
| 111 | + |
| 112 | +void GRUB2::postrm() |
| 113 | +{ |
| 114 | + // if the config is autogenerated, and we removed some kernels |
| 115 | + // we should update it |
| 116 | + if (is_autogenerated_) |
| 117 | + { |
| 118 | + // TODO: move this to util/ |
| 119 | + // love the C exec*() API |
| 120 | + std::vector<std::string> argv{"grub-mkconfig", |
| 121 | + is_multislot_ ? "grub2-mkconfig" : "grub-mkconfig", |
| 122 | + "-o", grub_cfg_->path()}; |
| 123 | + std::vector<std::vector<char>> argv_rw; |
| 124 | + std::vector<char*> cargv; |
| 125 | + |
| 126 | + if (opts_.pretend) |
| 127 | + { |
| 128 | + std::cerr << "\nThe following command would be run: " |
| 129 | + << argv[1] << " " << argv[2] << " " << argv[3] << std::endl; |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + std::cerr << "\nRunning: " << argv[1] << " " << argv[2] |
| 134 | + << " " << argv[3] << std::endl; |
| 135 | + |
| 136 | + for (const std::string& s : argv) |
| 137 | + { |
| 138 | + // std::string -> std::vector<char> |
| 139 | + argv_rw.emplace_back(s.begin(), s.end()); |
| 140 | + argv_rw.back().push_back('\0'); |
| 141 | + |
| 142 | + // std::vector<char> -> char* |
| 143 | + cargv.push_back(argv_rw.back().data()); |
| 144 | + } |
| 145 | + // sentinel |
| 146 | + cargv.push_back(nullptr); |
| 147 | + |
| 148 | + pid_t child_pid = fork(); |
| 149 | + switch (child_pid) |
| 150 | + { |
| 151 | + case -1: |
| 152 | + throw IOError("fork() failed", errno); |
| 153 | + break; |
| 154 | + case 0: |
| 155 | + std::cerr << cargv[0] << "\n"; |
| 156 | + execvp(cargv[0], cargv.data()); |
| 157 | + std::cerr << "Failed to execute " << cargv[0] |
| 158 | + << ": " << strerror(errno) << std::endl; |
| 159 | + _exit(1); |
| 160 | + break; |
| 161 | + default: |
| 162 | + waitpid(child_pid, nullptr, 0); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments