From 2c399d4494e801c7d539109f1844e28fd77b8952 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Wed, 19 Jun 2019 12:14:49 +0200 Subject: [PATCH] [libcalamares] Add settle / sync support method --- src/libcalamares/CMakeLists.txt | 1 + src/libcalamares/partition/Sync.cpp | 47 +++++++++++++++++++++++++++ src/libcalamares/partition/Sync.h | 49 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/libcalamares/partition/Sync.cpp create mode 100644 src/libcalamares/partition/Sync.h diff --git a/src/libcalamares/CMakeLists.txt b/src/libcalamares/CMakeLists.txt index b0f369c2f..174989b35 100644 --- a/src/libcalamares/CMakeLists.txt +++ b/src/libcalamares/CMakeLists.txt @@ -37,6 +37,7 @@ set( libSources # Partition service partition/PartitionSize.cpp + partition/Sync.cpp # Utility service utils/CalamaresUtilsSystem.cpp diff --git a/src/libcalamares/partition/Sync.cpp b/src/libcalamares/partition/Sync.cpp new file mode 100644 index 000000000..97acdc106 --- /dev/null +++ b/src/libcalamares/partition/Sync.cpp @@ -0,0 +1,47 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "Sync.h" + +#include "utils/CalamaresUtilsSystem.h" +#include "utils/Logger.h" + +void CalamaresUtils::Partition::sync() +{ + auto* s = CalamaresUtils::System::instance(); + + auto r = s->runCommand( CalamaresUtils::System::RunLocation::RunInHost, + { "/sbin/udevadm", "settle" }, + QString(), + QString(), + 10 // timeout seconds + ); + + if ( r.getExitCode() != 0 ) + { + cWarning() << "Could not settle disks."; + r.explainProcess( "udevadm", 10 ); + } + + s->runCommand( CalamaresUtils::System::RunLocation::RunInHost, + { "/bin/sync" }, + QString(), + QString(), + 10 // timeout seconds + ); +} diff --git a/src/libcalamares/partition/Sync.h b/src/libcalamares/partition/Sync.h new file mode 100644 index 000000000..4de49267f --- /dev/null +++ b/src/libcalamares/partition/Sync.h @@ -0,0 +1,49 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#ifndef PARTITION_SYNC_H +#define PARTITION_SYNC_H + +namespace CalamaresUtils +{ +namespace Partition +{ + +/** @brief Run "udevadm settle" or other disk-sync mechanism. + * + * Call this after mounting, unmount, toggling swap, or other functions + * that might cause the disk to be "busy" for other disk-modifying + * actions (in particular, KPMcore actions with the sfdisk backend + * are sensitive, and systemd tends to keep disks busy after a change + * for a while). + */ +void sync(); + +/** @brief RAII class for calling sync() */ +struct Syncer +{ + ~Syncer() + { + sync(); + } +}; + +} +} + +#endif