Skip to content

Commit

Permalink
use SD card serial, where possible. for linux; use dbus id, for nbd t…
Browse files Browse the repository at this point in the history
…he host+port.
  • Loading branch information
folkertvanheusden committed Oct 25, 2024
1 parent d78236f commit 1c16d42
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 55 deletions.
23 changes: 23 additions & 0 deletions backend-file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unistd.h>

#include "backend-file.h"
#include "gen.h"
#include "log.h"
#include "utils.h"

Expand Down Expand Up @@ -223,3 +224,25 @@ backend::cmpwrite_result_t backend_file::cmpwrite(const uint64_t block_nr, const

return result;
}

std::string backend_file::get_serial() const
{
std::string serial(DEFAULT_SERIAL);

FILE *fh = fopen("/var/lib/dbus/machine-id", "r");
if (fh) {
char buffer[128] { 0 };
if (fgets(buffer, sizeof buffer, fh) == nullptr)
DOLOG(logging::ll_error, "backend_file::get_serial", "-", "problem reading from dbus machine-id file: %s", strerror(errno));
fclose(fh);
char *lf = strchr(buffer, '\n');
if (lf)
*lf = 0x00;
serial = buffer;
}
else {
DOLOG(logging::ll_error, "backend_file::get_serial", "-", "cannot open dbus machine-id file: %s", strerror(errno));
}

return serial;
}
5 changes: 3 additions & 2 deletions backend-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class backend_file : public backend

bool begin() override;

uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;
std::string get_serial() const override;
uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;

bool sync() override;

Expand Down
5 changes: 5 additions & 0 deletions backend-nbd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,8 @@ backend::cmpwrite_result_t backend_nbd::cmpwrite(const uint64_t block_nr, const

return result;
}

std::string backend_nbd::get_serial() const
{
return identifier;
}
5 changes: 3 additions & 2 deletions backend-nbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ class backend_nbd : public backend

bool begin() override;

uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;
std::string get_serial() const override;
uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;

bool sync() override;

Expand Down
11 changes: 6 additions & 5 deletions backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,18 @@ class backend
backend(const std::string & identifier);
virtual ~backend();

virtual bool begin() = 0;
virtual bool begin() = 0;

virtual uint64_t get_size_in_blocks() const = 0;
virtual uint64_t get_block_size() const = 0;
virtual std::string get_serial() const = 0;
virtual uint64_t get_size_in_blocks() const = 0;
virtual uint64_t get_block_size() const = 0;

// mainly for thin provisioning
virtual uint8_t get_free_space_percentage();
virtual uint8_t get_free_space_percentage();

virtual std::pair<uint64_t, uint32_t> get_idle_state();

virtual bool sync() = 0;
virtual bool sync() = 0;

void get_and_reset_stats(uint64_t *const bytes_read, uint64_t *const bytes_written, uint64_t *const n_syncs, uint64_t *const n_trims);

Expand Down
4 changes: 4 additions & 0 deletions gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

#include <cstdint>

#define DEFAULT_SERIAL "12345678"

#define FILENAME "test.dat"

static_assert(sizeof(size_t) >= 4);
static_assert(sizeof(int) >= 2);

Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ int main(int argc, char *argv[])
std::string pid_file;
std::string ip_address = "0.0.0.0";
int port = 3260;
std::string dev = "test.dat";
std::string dev = FILENAME;
std::string target_name= "test";
int trim_level = 1;
bool use_snmp = false;
Expand Down
13 changes: 11 additions & 2 deletions microcontrollers/backend-sdcard-teensy41.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#include <unistd.h>

#include "backend-sdcard-teensy41.h"
#include "gen.h"
#include "log.h"
#include "utils.h"

#define FILENAME "test.dat"

extern void write_led(const int gpio, const int state);

Expand Down Expand Up @@ -37,7 +37,7 @@ bool backend_sdcard_teensy41::begin()
file = SD.sdfs.open(FILENAME, O_RDWR);
if (!file)
{
DOLOG(logging::ll_error, "backend_sdcard_teensy41::begin", "-", "Cannot access test.dat on SD-card");
DOLOG(logging::ll_error, "backend_sdcard_teensy41::begin", "-", "Cannot access " FILENAME " on SD-card");
write_led(led_read, LOW);
write_led(led_write, LOW);
return false;
Expand Down Expand Up @@ -251,3 +251,12 @@ void backend_sdcard_teensy41::wait_for_card()
while(file.isBusy())
yield();
}

std::string backend_sdcard_teensy41::get_serial() const
{
cid_t cid { };
if (!SD.sdfs.card()->readCID(&cid))
return DEFAULT_SERIAL;

return myformat("%08x", cid.psn);
}
5 changes: 3 additions & 2 deletions microcontrollers/backend-sdcard-teensy41.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class backend_sdcard_teensy41 : public backend

bool begin() override;

uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;
std::string get_serial() const override;
uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;

bool sync() override;

Expand Down
15 changes: 12 additions & 3 deletions microcontrollers/backend-sdcard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <sys/stat.h>

#include "backend-sdcard.h"
#include "gen.h"
#include "log.h"
#include "utils.h"


#ifdef RP2040W
#define SD_CS 17
#define SDCARD_SPI SPI1
Expand All @@ -19,8 +21,6 @@
// 5 CS
#endif

#define FILENAME "test.dat"

extern void write_led(const int gpio, const int state);

backend_sdcard::backend_sdcard(const int led_read, const int led_write, const int pin_SD_MISO, const int pin_SD_MOSI, const int pin_SD_SCLK, const int pin_SD_CS):
Expand Down Expand Up @@ -91,7 +91,7 @@ bool backend_sdcard::reinit(const bool close_first)
retry:
if (file.open(FILENAME, O_RDWR) == false)
{
DOLOG(logging::ll_error, "backend_sdcard::reinit", "-", "Cannot access test.dat on SD-card");
DOLOG(logging::ll_error, "backend_sdcard::reinit", "-", "Cannot access " FILENAME " on SD-card");
write_led(led_read, LOW);
write_led(led_write, LOW);
return false;
Expand Down Expand Up @@ -326,3 +326,12 @@ void backend_sdcard::wait_for_card()
while(sd.card()->isBusy())
yield();
}

std::string backend_sdcard::get_serial() const
{
cid_t cid { };
if (!sd.card()->readCID(&cid))
return DEFAULT_SERIAL;

return myformat("%08x", cid.psn());
}
23 changes: 12 additions & 11 deletions microcontrollers/backend-sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
class backend_sdcard : public backend
{
private:
SdFs sd;
FsFile file;
uint64_t card_size { 0 };
const int led_read { -1 };
const int led_write { -1 };
const int pin_SD_MISO { -1 };
const int pin_SD_MOSI { -1 };
const int pin_SD_SCLK { -1 };
const int pin_SD_CS { -1 };
mutable SdFs sd;
FsFile file;
uint64_t card_size { 0 };
const int led_read { -1 };
const int led_write { -1 };
const int pin_SD_MISO { -1 };
const int pin_SD_MOSI { -1 };
const int pin_SD_SCLK { -1 };
const int pin_SD_CS { -1 };

#if defined(RP2040W)
mutex_t serial_access_lock;
Expand All @@ -35,8 +35,9 @@ class backend_sdcard : public backend

bool begin() override;

uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;
std::string get_serial() const override;
uint64_t get_size_in_blocks() const override;
uint64_t get_block_size() const override;

bool sync() override;

Expand Down
26 changes: 1 addition & 25 deletions scsi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ const std::map<scsi::scsi_opcode, scsi_opcode_details> scsi_a3_data {
{ scsi::scsi_opcode::o_rep_sup_oper, { { 0xff, 0x1f, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x07 }, 12, "report supported operations" } },
};

#define DEFAULT_SERIAL "12345678"
#if defined(ARDUINO)
#define MAX_WS_LEN 2
#else
Expand All @@ -58,31 +57,8 @@ const std::map<scsi::scsi_opcode, scsi_opcode_details> scsi_a3_data {

constexpr const uint8_t max_compare_and_write_block_count = 1;

scsi::scsi(backend *const b, const int trim_level, io_stats_t *const is) : b(b), trim_level(trim_level), is(is)
scsi::scsi(backend *const b, const int trim_level, io_stats_t *const is) : b(b), trim_level(trim_level), is(is), serial(b->get_serial())
{
#ifdef ESP32
uint64_t temp = ESP.getEfuseMac();
serial = myformat("%" PRIx64, temp);
#elif defined(TEENSY4_1)
uint32_t m1 = HW_OCOTP_MAC1;
uint32_t m2 = HW_OCOTP_MAC0;
serial = myformat("%08x%08x", m1, m2);
#else
FILE *fh = fopen("/var/lib/dbus/machine-id", "r");
if (fh) {
char buffer[128] { 0 };
if (fgets(buffer, sizeof buffer, fh) == nullptr)
serial = DEFAULT_SERIAL;
fclose(fh);
char *lf = strchr(buffer, '\n');
if (lf)
*lf = 0x00;
serial = buffer;
}
else {
serial = DEFAULT_SERIAL;
}
#endif
}

scsi::~scsi()
Expand Down
2 changes: 1 addition & 1 deletion scsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class scsi
void get_and_reset_stats(uint64_t *const bytes_read, uint64_t *const bytes_written, uint64_t *const n_syncs, uint64_t *const n_trims);

scsi_lock_status reserve_device();
bool unlock_device();
bool unlock_device();
scsi_lock_status locking_status();

scsi_rw_result sync();
Expand Down
1 change: 0 additions & 1 deletion todo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
- async writes
- name of disk-image on SD card
- does not send syslog when connected to wifi only
- use serial of SD-card as an iscsi serial!

- leds stay on

Expand Down

0 comments on commit 1c16d42

Please sign in to comment.