haiku: USB backend (Storage Kit), bundled doctest, VM sync tooling
- usb_haiku.cpp: lists /dev/disk/usb/* drives (never the boot volume), formats MBR + FAT32 at 1 MiB through BDiskDevice/BPartition, mounts, unmounts and ejects; modifications are cancelled on failure - third_party/doctest (MIT single header) used when no system package exists (HaikuPorts has none) - tools/haiku-vm.sh: sync command, dedicated SSH key; tools/qmp.py: typefile command and slower key pacing - builds on Haiku R1/beta6 x86_64; usb-list sees the virtual stick. Known issue: some unit tests still fail on Haiku (to investigate). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WDASLCiN764v685boRVYar
This commit is contained in:
1 parent
114976b912
commit
b2ca454d44
8 files changed
+7376
-3
No files matched your search
@@ -24,6 +24,10 @@ target_link_libraries(openpu_core
|
||||
# USB backend
|
||||
if(HAIKU)
|
||||
target_sources(openpu_core PRIVATE usb_haiku.cpp)
|
||||
# The disk device API (BDiskDevice, BPartition) lives in the private Storage Kit headers.
|
||||
find_path(HAIKU_PRIVATE_STORAGE DiskDevice.h PATHS /boot/system/develop/headers/private/storage REQUIRED)
|
||||
target_include_directories(openpu_core SYSTEM PRIVATE ${HAIKU_PRIVATE_STORAGE}
|
||||
/boot/system/develop/headers/private/shared)
|
||||
target_link_libraries(openpu_core PUBLIC be)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Haiku USB backend: enumeration, FAT32 formatting, mounting and ejecting through the Storage Kit
|
||||
// disk device API (BDiskDeviceRoster / BDiskDevice / BPartition), the same API DriveSetup uses.
|
||||
// Haiku is single-user, so no authorisation step is involved.
|
||||
|
||||
#include "openpu/usb.hpp"
|
||||
|
||||
#include <DiskDevice.h>
|
||||
#include <DiskDeviceRoster.h>
|
||||
#include <DiskDeviceTypes.h>
|
||||
#include <Partition.h>
|
||||
#include <Path.h>
|
||||
#include <String.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace openpu {
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr off_t kPartitionOffset = 1024 * 1024; // 1 MiB, like the Linux backend
|
||||
|
||||
std::string statusText(status_t s)
|
||||
{
|
||||
return std::strerror(s); // NOLINT(concurrency-mt-unsafe): Haiku's strerror is thread-safe
|
||||
}
|
||||
|
||||
[[noreturn]] void fail(const std::string& what, status_t status)
|
||||
{
|
||||
throw UsbError(UsbError::Kind::Failed, what + ": " + statusText(status));
|
||||
}
|
||||
|
||||
bool startsWith(const char* s, const char* prefix)
|
||||
{
|
||||
return s != nullptr && std::strncmp(s, prefix, std::strlen(prefix)) == 0;
|
||||
}
|
||||
|
||||
/// Calls `fn` for the device itself and every partition below it.
|
||||
template <typename F>
|
||||
void forEachPartition(BPartition* p, F&& fn)
|
||||
{
|
||||
fn(p);
|
||||
for (int32 i = 0; i < p->CountChildren(); ++i)
|
||||
forEachPartition(p->ChildAt(i), fn);
|
||||
}
|
||||
|
||||
bool hostsBootVolume(BDiskDevice& device)
|
||||
{
|
||||
bool boot = false;
|
||||
forEachPartition(&device, [&](BPartition* p) {
|
||||
BPath mp;
|
||||
if (p->IsMounted() && p->GetMountPoint(&mp) == B_OK && std::strcmp(mp.Path(), "/boot") == 0)
|
||||
boot = true;
|
||||
});
|
||||
return boot;
|
||||
}
|
||||
|
||||
partition_id parseId(const std::string& id)
|
||||
{
|
||||
try {
|
||||
return static_cast<partition_id>(std::stol(id));
|
||||
} catch (const std::exception&) {
|
||||
throw UsbError(UsbError::Kind::Failed, "invalid drive id " + id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Disk systems are looked up by short name on some Haiku versions and by "pretty" name on others.
|
||||
status_t initialize(BPartition* p, const char* shortName, const char* prettyName, const char* name,
|
||||
const char* parameters)
|
||||
{
|
||||
status_t s = p->Initialize(shortName, name, parameters);
|
||||
if (s != B_OK)
|
||||
s = p->Initialize(prettyName, name, parameters);
|
||||
return s;
|
||||
}
|
||||
|
||||
class HaikuUsbBackend final : public UsbBackend {
|
||||
public:
|
||||
std::vector<UsbDrive> list() override
|
||||
{
|
||||
std::vector<UsbDrive> out;
|
||||
BDiskDeviceRoster roster;
|
||||
BDiskDevice device;
|
||||
roster.RewindDevices();
|
||||
while (roster.GetNextDevice(&device) == B_OK) {
|
||||
BPath path;
|
||||
if (device.GetPath(&path) != B_OK || !startsWith(path.Path(), "/dev/disk/usb/"))
|
||||
continue;
|
||||
if (!device.HasMedia() || device.IsReadOnlyMedia() || device.Size() <= 0 || hostsBootVolume(device))
|
||||
continue;
|
||||
UsbDrive d;
|
||||
d.id = std::to_string(device.ID());
|
||||
d.device = path.Path();
|
||||
d.size = static_cast<std::uint64_t>(device.Size());
|
||||
if (device.Name() != nullptr)
|
||||
d.model = device.Name();
|
||||
forEachPartition(&device, [&](BPartition* p) {
|
||||
if (d.label.empty() && p->ContentType() != nullptr)
|
||||
d.label = p->ContentName().String();
|
||||
BPath mp;
|
||||
if (p->IsMounted() && p->GetMountPoint(&mp) == B_OK)
|
||||
d.mountPoints.emplace_back(mp.Path());
|
||||
});
|
||||
out.push_back(std::move(d));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
fs::path formatFat32(const UsbDrive& drive, const std::string& label) override
|
||||
{
|
||||
const partition_id id = parseId(drive.id);
|
||||
BDiskDeviceRoster roster;
|
||||
BDiskDevice device;
|
||||
status_t s = roster.GetDeviceWithID(id, &device);
|
||||
if (s != B_OK)
|
||||
fail("USB drive " + drive.device + " not found", s);
|
||||
BPath path;
|
||||
if (device.GetPath(&path) != B_OK || !startsWith(path.Path(), "/dev/disk/usb/") || hostsBootVolume(device))
|
||||
throw UsbError(UsbError::Kind::Failed, drive.device + " is not a removable USB drive");
|
||||
|
||||
unmountAll(device);
|
||||
if ((s = device.PrepareModifications()) != B_OK)
|
||||
fail("cannot modify " + drive.device, s);
|
||||
try {
|
||||
if (device.ContentType() != nullptr && (s = device.Uninitialize()) != B_OK)
|
||||
fail("cannot erase the partition table", s);
|
||||
if ((s = initialize(&device, "intel", kPartitionTypeIntel, nullptr, nullptr)) != B_OK)
|
||||
fail("cannot create the MBR partition table", s);
|
||||
|
||||
off_t start = kPartitionOffset;
|
||||
off_t size = device.Size() - start;
|
||||
BString name;
|
||||
if ((s = device.ValidateCreateChild(&start, &size, kPartitionTypeFAT32, &name, nullptr)) != B_OK)
|
||||
fail("cannot create the FAT32 partition", s);
|
||||
BPartition* child = nullptr;
|
||||
if ((s = device.CreateChild(start, size, kPartitionTypeFAT32, nullptr, nullptr, &child)) != B_OK)
|
||||
fail("cannot create the FAT32 partition", s);
|
||||
if ((s = initialize(child, "fat", "FAT32 File System", label.c_str(), "fat 32\n")) != B_OK)
|
||||
fail("cannot format the FAT32 partition", s);
|
||||
if ((s = device.CommitModifications()) != B_OK)
|
||||
fail("cannot write the changes to " + drive.device, s);
|
||||
} catch (...) {
|
||||
device.CancelModifications();
|
||||
throw;
|
||||
}
|
||||
|
||||
// Mount the new file system (re-read the device: the committed state is a new snapshot).
|
||||
if ((s = roster.GetDeviceWithID(id, &device)) != B_OK || device.CountChildren() == 0)
|
||||
fail("the new partition is not visible", s != B_OK ? s : B_ENTRY_NOT_FOUND);
|
||||
BPartition* part = device.ChildAt(0);
|
||||
if (!part->IsMounted() && (s = part->Mount()) != B_OK)
|
||||
fail("cannot mount the new file system", s);
|
||||
BPath mountPoint;
|
||||
if ((s = part->GetMountPoint(&mountPoint)) != B_OK)
|
||||
fail("cannot locate the mount point", s);
|
||||
return mountPoint.Path();
|
||||
}
|
||||
|
||||
void eject(const UsbDrive& drive) override
|
||||
{
|
||||
BDiskDeviceRoster roster;
|
||||
BDiskDevice device;
|
||||
if (roster.GetDeviceWithID(parseId(drive.id), &device) != B_OK)
|
||||
return; // already gone
|
||||
unmountAll(device); // flushes the FAT
|
||||
// Not every USB mass-storage device supports the eject command; unmounting is enough then.
|
||||
(void)device.Eject(true);
|
||||
}
|
||||
|
||||
private:
|
||||
static void unmountAll(BDiskDevice& device)
|
||||
{
|
||||
status_t failure = B_OK;
|
||||
forEachPartition(&device, [&](BPartition* p) {
|
||||
if (p->IsMounted()) {
|
||||
const status_t s = p->Unmount();
|
||||
if (s != B_OK)
|
||||
failure = s;
|
||||
}
|
||||
});
|
||||
if (failure != B_OK)
|
||||
throw UsbError(UsbError::Kind::Busy, "cannot unmount the USB drive: " + statusText(failure));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
std::unique_ptr<UsbBackend> makeUsbBackend()
|
||||
{
|
||||
return std::make_unique<HaikuUsbBackend>();
|
||||
}
|
||||
|
||||
} // namespace openpu
|
||||
@@ -16,6 +16,9 @@ find_package(LibArchive REQUIRED)
|
||||
target_link_libraries(openpu_tests PRIVATE openpu_core openpu_warnings LibArchive::LibArchive)
|
||||
if(TARGET doctest::doctest)
|
||||
target_link_libraries(openpu_tests PRIVATE doctest::doctest)
|
||||
else()
|
||||
# Bundled single header (third_party/doctest, MIT) when the system has no doctest package.
|
||||
target_include_directories(openpu_tests SYSTEM PRIVATE ${PROJECT_SOURCE_DIR}/third_party)
|
||||
endif()
|
||||
target_compile_definitions(openpu_tests PRIVATE OPENPU_FIXTURES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/fixtures")
|
||||
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016-2023 Viktor Kirilov
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
doctest 2.4.12 single header (https://github.com/doctest/doctest), MIT license.
|
||||
Used by the unit tests only when no system doctest package is found (e.g. Haiku).
|
||||
Vendored
+7134
File diff suppressed because it is too large.
Load diff
+7
-1
@@ -69,9 +69,15 @@ stop)
|
||||
;;
|
||||
ssh)
|
||||
shift
|
||||
exec ssh -p "$SSH_PORT" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
|
||||
exec ssh -p "$SSH_PORT" -i "$VM_DIR/id_ed25519" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR \
|
||||
user@127.0.0.1 "$@"
|
||||
;;
|
||||
sync)
|
||||
# Copy the working tree (without .git) to ~/openpu in the VM.
|
||||
SRC=$(cd "$(dirname "$0")/.." && pwd)
|
||||
tar -C "$SRC" --exclude=.git -cf - . | "$0" ssh "mkdir -p openpu && tar -xf - -C openpu"
|
||||
echo "synced $SRC -> VM:~/openpu"
|
||||
;;
|
||||
status)
|
||||
running && echo "running (pid $(cat "$PIDFILE"))" || echo "stopped"
|
||||
;;
|
||||
|
||||
+9
-2
@@ -6,6 +6,7 @@ usage: python3 tools/qmp.py [--socket PATH] COMMAND...
|
||||
click X Y [right|double] click at screen pixel X,Y (needs a usb-tablet device)
|
||||
move X Y move the pointer
|
||||
type TEXT type text with a US keyboard layout
|
||||
typefile FILE type the content of FILE (newlines press Return)
|
||||
key COMBO... press key combos, e.g. ret, tab, ctrl-alt-delete, alt-f4
|
||||
powerdown ACPI power button
|
||||
hmp COMMAND human monitor command (prints the answer)
|
||||
@@ -125,8 +126,10 @@ def keys_for(ch):
|
||||
|
||||
|
||||
def press(q, names):
|
||||
q.cmd("send-key", keys=[{"type": "qcode", "data": n} for n in names], **{"hold-time": 50})
|
||||
time.sleep(0.03)
|
||||
# send-key returns before QEMU replays the keys: wait longer than the hold time, or
|
||||
# consecutive keys overlap (and a held shift leaks into the next characters).
|
||||
q.cmd("send-key", keys=[{"type": "qcode", "data": n} for n in names], **{"hold-time": 30})
|
||||
time.sleep(0.08)
|
||||
|
||||
|
||||
def main():
|
||||
@@ -152,6 +155,10 @@ def main():
|
||||
elif cmd == "type":
|
||||
for ch in " ".join(rest):
|
||||
press(q, keys_for(ch))
|
||||
elif cmd == "typefile":
|
||||
with open(rest[0], encoding="utf-8") as f:
|
||||
for ch in f.read():
|
||||
press(q, keys_for(ch))
|
||||
elif cmd == "key":
|
||||
for combo in rest:
|
||||
press(q, combo.split("-"))
|
||||
|
||||
Reference in new issue
Block a user