Commit cdfb1c65 authored by Jan Koester's avatar Jan Koester
Browse files

added smart support

parent 7c6ff16a
Loading
Loading
Loading
Loading
+3 −1
Changes for CMakeLists.txt: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.16)
project(storaged VERSION 0.1.0 LANGUAGES CXX)

# storaged: software RAID (md) and LVM2 without mdadm and the lvm2 tools.
# storaged: software RAID (md) and LVM2 without mdadm and the lvm2 tools,
# and SMART without smartctl (ATA over SG_IO, NVMe admin commands).
# md v1.2 superblocks and LVM2 labels and metadata are read and written
# here, arrays are run with the md ioctls and logical volumes are
# device-mapper devices. At start it assembles the arrays and activates
@@ -31,6 +32,7 @@ add_executable(storaged
    src/blockdev.cpp
    src/raid.cpp
    src/lvm.cpp
    src/smart.cpp
)
target_include_directories(storaged PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(storaged PRIVATE sysbus::sysbus)
+33 −0
Changes for include/storaged/api.h: 33 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -78,6 +78,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *   RemoveLV {vg, lv, confirm}
 *   Activate / Deactivate {vg, lv?}     without lv: every plain LV
 *
 * SMART (ATA drives over SG_IO -- SATA, SAS and USB bridges that pass ATA
 * through --, NVMe drives with admin commands; no smartctl). An ATA drive
 * in standby isn't spun up: its last reading is shown (read_at), power
 * says standby. A drive that doesn't answer keeps its last reading too,
 * with problem set.
 *   ListSmart -> per disk: disk (name), size, model, serial?, firmware?,
 *       transport (ata|nvme|none), smart (available), enabled, rotation?
 *       (ssd|rpm), power (active|idle|standby|unknown), health
 *       (passed|failing|unknown), temperature? (°C), power_on_hours?,
 *       power_cycles?, ATA: reallocated?, pending?, uncorrectable?,
 *       crc_errors?; NVMe: critical_warning (bits), available_spare,
 *       spare_threshold, percentage_used (%), data_read, data_written
 *       (bytes), unsafe_shutdowns, media_errors, error_log_entries;
 *       self_test_supported, self_test? (passed|failed|aborted|
 *       interrupted|running|none), self_test_remaining? (% while
 *       running), short_test_minutes?, long_test_minutes?, read_at? (unix
 *       time of the values), problem? (why there are none), warning
 *       (repeated: what is wrong, in the call's language)
 *   GetSmart {disk, wake?} -> the same for one disk, then per ATA
 *       attribute: attr (id), name, value, worst, threshold, raw, prefail,
 *       failing (now|past|no); wake: read a drive in standby too
 *   StartSelfTest {disk, type (short|long)}   the drive runs it on its own
 *   AbortSelfTest {disk}
 * Every half hour storaged reads all drives (standby ones not); a warning
 * that is new is logged and sent as the signal SmartWarning.
 *
 * Free devices, in both lists: device (name), size, model, serial,
 * partition, parent? (of a partition), content_type?, content_label?
 * (what blkid would name; it is lost), partitions? (a disk's: lost too).
@@ -122,12 +148,19 @@ constexpr const char *RemoveLV = "RemoveLV";
constexpr const char *Activate = "Activate";
constexpr const char *Deactivate = "Deactivate";

constexpr const char *ListSmart = "ListSmart";
constexpr const char *GetSmart = "GetSmart";
constexpr const char *StartSelfTest = "StartSelfTest";
constexpr const char *AbortSelfTest = "AbortSelfTest";

constexpr const char *ListJobs = "ListJobs";
} // namespace method

namespace signal {
// fields: job, area, state -- a job ended (done or failed)
constexpr const char *JobFinished = "JobFinished";
// fields: disk, warning (English) -- a SMART warning that wasn't there before
constexpr const char *SmartWarning = "SmartWarning";
} // namespace signal

// Error names besides sysbus's own (InvalidArgs, Failed, AccessDenied,
+9 −0
Changes for src/main.cpp: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * device that might not answer runs in a child process (jobs.h,
 * blockdev.h). SIGTERM stops it, jobs that still run go on. */

#include <algorithm>
#include <cerrno>
#include <csignal>
#include <cstring>
@@ -52,6 +53,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "lvm.h"
#include "raid.h"
#include "result.h"
#include "smart.h"

namespace storaged {

@@ -104,6 +106,7 @@ sysbus::Message Daemon::handle(const sysbus::Message &call, const sysbus::Caller
    if (op == method::ListJobs) jobs::list(r, call.get("area"));
    else if (raid::handles(op)) r = raid::handle(call);
    else if (lvm::handles(op)) r = lvm::handle(call);
    else if (smart::handles(op)) r = smart::handle(call);
    else return sysbus::Message::error(call, sysbus::error::UnknownMethod, "unknown method " + op);
    if (op.compare(0, 4, "List") != 0) {
        bool background = !r.failed() && !r.fields.empty() && r.fields[0].first == "background";
@@ -143,6 +146,9 @@ void Daemon::run() {
        // running jobs: also look every second (a job whose pipe a stuck
        // grandchild holds open ends without its pipe saying so)
        int timeout = jobs::running() ? 1000 : _bus.running() ? -1 : 5000;
        // the periodic SMART check
        long long untilCheck = smart::msUntilCheck();
        if (timeout < 0 || untilCheck < timeout) timeout = (int)std::min<long long>(untilCheck, 60 * 60 * 1000);
        int r = poll(fds.data(), fds.size(), timeout);
        if (r < 0 && errno != EINTR) {
            std::cerr << "storaged: poll: " << strerror(errno) << std::endl;
@@ -161,6 +167,9 @@ void Daemon::run() {
            }
        }
        finished();
        if (smart::msUntilCheck() == 0)
            for (auto &w : smart::check())
                if (_bus.running()) _bus.emit(signal::SmartWarning, {{"disk", w.first}, {"warning", w.second}});
    }
}

src/smart.cpp

0 → 100644
+717 −0

File added.

Preview size limit exceeded, changes collapsed.

src/smart.h

0 → 100644
+59 −0
Changes for src/smart.h: 59 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
Copyright (c) 2026, Jan Koester jan.koester@gmx.net
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

/* SMART, without smartctl: ATA drives (SATA, and SAS/USB bridges that
 * translate ATA PASS-THROUGH) over SG_IO, NVMe drives with the admin
 * commands of the nvme driver's ioctl. The methods are in
 * include/storaged/api.h.
 *
 * Every look at a drive runs guarded (blockdev.h): a disk that doesn't
 * answer stalls a child, not storaged. An ATA drive in standby isn't
 * spun up to be read: its last values are shown, with when they were
 * read. Every half hour all drives are read (standby ones aren't); a
 * warning that wasn't there before is logged and sent as the signal
 * SmartWarning. */

#pragma once

#include <string>
#include <vector>

#include <sysbus/message.h>

#include "result.h"

namespace storaged::smart {

bool handles(const std::string &method);
Result handle(const sysbus::Message &call);

// the periodic check: due when msUntilCheck() is 0; check() returns the
// warnings that are new since the last one (disk, text)
long long msUntilCheck();
std::vector<std::pair<std::string, std::string>> check();

} // namespace storaged::smart