Skip to content

Commit

Permalink
🧹 move btrfs subvolumes mount logic into gucc
Browse files Browse the repository at this point in the history
  • Loading branch information
vnepogodin committed Jun 27, 2024
1 parent 9e011fc commit 97be1f3
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
8 changes: 6 additions & 2 deletions gucc/include/gucc/btrfs.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#ifndef BTRFS_HPP
#define BTRFS_HPP

#include <string> // for string
#include <string_view> // for string_view
#include <vector> // for vector

namespace gucc::fs {

struct BtrfsSubvolume final {
std::string_view subvolume;
std::string_view mountpoint;
std::string subvolume;
std::string mountpoint;
};

// Creates btrfs subvolume
Expand All @@ -17,6 +18,9 @@ auto btrfs_create_subvol(std::string_view subvolume, std::string_view root_mount
// Creates btrfs subvolumes and mounts them
auto btrfs_create_subvols(const std::vector<BtrfsSubvolume>& subvols, std::string_view device, std::string_view root_mountpoint, std::string_view mount_opts) noexcept -> bool;

// Mounts btrfs subvolumes
auto btrfs_mount_subvols(const std::vector<BtrfsSubvolume>& subvols, std::string_view device, std::string_view root_mountpoint, std::string_view mount_opts) noexcept -> bool;

} // namespace gucc::fs

#endif // BTRFS_HPP
10 changes: 9 additions & 1 deletion gucc/src/btrfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,17 @@ auto btrfs_create_subvols(const std::vector<BtrfsSubvolume>& subvols, std::strin
}
}
// TODO(vnepogodin): handle exit code
utils::exec(fmt::format(FMT_COMPILE("umount -v {} &>>/tmp/cachyos-install.log"), root_mountpoint));
utils::exec(fmt::format(FMT_COMPILE("umount -v {} &>>/tmp/cachyos-install.log"), root_mountpoint), true);

// Mount subvolumes
if (!fs::btrfs_mount_subvols(subvols, device, root_mountpoint, mount_opts)) {
spdlog::error("Failed to mount btrfs subvolumes");
return false;
}
return true;
}

auto btrfs_mount_subvols(const std::vector<BtrfsSubvolume>& subvols, std::string_view device, std::string_view root_mountpoint, std::string_view mount_opts) noexcept -> bool {

Check failure on line 65 in gucc/src/btrfs.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/btrfs.cpp:65:70 [bugprone-easily-swappable-parameters

3 adjacent parameters of 'btrfs_mount_subvols' of similar type ('std::string_view') are easily swapped by mistake
for (const auto& subvol : subvols) {
auto mount_option = fmt::format(FMT_COMPILE("subvol={},{}"), subvol.subvolume, mount_opts);
if (subvol.subvolume.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion gucc/src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ auto get_isa_levels() noexcept -> std::vector<std::string> {
std::vector<std::string> supported_isa_levels;

{
struct utsname un{};
struct utsname un { };

Check failure on line 119 in gucc/src/cpu.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/gucc/src/cpu.cpp:119:24 [readability-identifier-length

variable name 'un' is too short, expected at least 3 characters
uname(&un);
supported_isa_levels.emplace_back(un.machine);
}
Expand Down
39 changes: 25 additions & 14 deletions src/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fmt/core.h>

using namespace std::string_view_literals;
using namespace std::string_literals;
namespace fs = std::filesystem;

namespace utils {
Expand Down Expand Up @@ -81,9 +82,9 @@ void btrfs_create_subvols([[maybe_unused]] const disk_part& disk, const std::str

// Create subvolumes automatically
const std::vector<gucc::fs::BtrfsSubvolume> subvolumes{
gucc::fs::BtrfsSubvolume{.subvolume = "/@"sv, .mountpoint = "/"sv},
gucc::fs::BtrfsSubvolume{.subvolume = "/@home"sv, .mountpoint = "/home"sv},
gucc::fs::BtrfsSubvolume{.subvolume = "/@cache"sv, .mountpoint = "/var/cache"sv},
gucc::fs::BtrfsSubvolume{.subvolume = "/@"s, .mountpoint = "/"s},
gucc::fs::BtrfsSubvolume{.subvolume = "/@home"s, .mountpoint = "/home"s},
gucc::fs::BtrfsSubvolume{.subvolume = "/@cache"s, .mountpoint = "/var/cache"s},
// gucc::fs::BtrfsSubvolume{.subvolume = "/@snapshots"sv, .mountpoint = "/.snapshots"sv},
};
if (!gucc::fs::btrfs_create_subvols(subvolumes, disk.root, root_mountpoint, disk.mount_opts)) {
Expand All @@ -106,23 +107,33 @@ void mount_existing_subvols(const disk_part& disk) noexcept {
fs_opts = "compress=lzo,noatime,space_cache,ssd,commit=120"sv;
}
#ifdef NDEVENV
gucc::utils::exec("btrfs subvolume list /mnt 2>/dev/null | cut -d' ' -f9 > /tmp/.subvols"sv, true);
umount("/mnt");
const auto root_mountpoint = "/mnt"sv;

gucc::utils::exec(fmt::format(FMT_COMPILE("btrfs subvolume list {} 2>/dev/null | cut -d' ' -f9 > /tmp/.subvols"), root_mountpoint), true);
if (gucc::utils::exec(fmt::format(FMT_COMPILE("umount -v {} &>>/tmp/cachyos-install.log"), root_mountpoint), true) != "0") {
spdlog::error("Failed to unmount {}", root_mountpoint);
}

// Mount subvolumes one by one
for (const auto& subvol : gucc::utils::make_multiline(gucc::utils::exec("cat /tmp/.subvols"sv))) {
const auto& subvol_list = gucc::utils::make_multiline(gucc::utils::exec("cat /tmp/.subvols"sv));

// Get mountpoints of subvolumes
std::vector<gucc::fs::BtrfsSubvolume> subvolumes{};
for (auto&& subvol : subvol_list) {
// Ask for mountpoint
const auto& content = fmt::format(FMT_COMPILE("\nInput mountpoint of\nthe subvolume {}\nas it would appear\nin installed system\n(without prepending /mnt).\n"), subvol);
const auto& content = fmt::format(FMT_COMPILE("\nInput mountpoint of\nthe subvolume {}\nas it would appear\nin installed system\n(without prepending {}).\n"), subvol, root_mountpoint);
std::string mountpoint{"/"};
if (!tui::detail::inputbox_widget(mountpoint, content, size(ftxui::HEIGHT, ftxui::LESS_THAN, 9) | size(ftxui::WIDTH, ftxui::LESS_THAN, 30))) {
return;
}
const auto& mount_dir{fmt::format(FMT_COMPILE("/mnt/{}"), mountpoint)};
if (!fs::exists(mount_dir)) {
fs::create_directories(mount_dir);
}
// Mount the subvolume
gucc::utils::exec(fmt::format(FMT_COMPILE("mount -o \"{},subvol={}\" \"{}\" \"{}\""), fs_opts, subvol, disk.root, mount_dir));

subvolumes.push_back(gucc::fs::BtrfsSubvolume{.subvolume = subvol, .mountpoint = mountpoint});
}

// TODO(vnepogodin): add confirmation for selected mountpoint for particular subvolume

// Mount subvolumes
if (!gucc::fs::btrfs_mount_subvols(subvolumes, disk.root, root_mountpoint, fs_opts)) {
spdlog::error("Failed to mount btrfs subvolumes");
}
#endif
}
Expand Down

0 comments on commit 97be1f3

Please sign in to comment.