Tape driver simulator
  • Go 91.8%
  • Shell 4.8%
  • Go Template 3.4%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-20 15:05:31 -08:00
.pi/work ledger: the LUN/element-map facts that cross the repo boundary 2026-11-20 15:05:31 -08:00
internal The element map a measured library has, so one target can hold the whole shelf 2026-11-20 15:04:53 -08:00
tests The element map a measured library has, so one target can hold the whole shelf 2026-11-20 15:04:53 -08:00
.gitignore Demo configuration works out of the box; README corrected 2026-11-20 12:07:13 -08:00
go.mod Phase 1: gotgt iSCSI target + LTO tape drive emulation, e2e-green against iscsi-go initiator 2026-11-20 01:15:03 -08:00
go.sum Phase 1: gotgt iSCSI target + LTO tape drive emulation, e2e-green against iscsi-go initiator 2026-11-20 01:15:03 -08:00
PLAN.md Initial 2026-11-20 00:30:30 -08:00
README.md The element map a measured library has, so one target can hold the whole shelf 2026-11-20 15:04:53 -08:00
tapesim.json Several iSCSI targets in one process: the LUN-0 workaround done properly 2026-11-20 13:59:29 -08:00

tapesim

Tape library simulator: emulated LTO drives and an MSL-style medium changer, exported over iSCSI (gotgt), observable and controllable over a JSON API and an HTMX UI. Written against the measured behaviours of a real HPE Ultrium 9 — the sense shapes, file-mark semantics, MAM behaviour, and maintenance windows documented in ../ltfs-go/MEMORY.md — so that software built for real tape (LTFS, tar, mtx) sees a device that fails the same way the hardware does.

Run

go build -o tapesim ./cmd/tapesim
./tapesim -config tapesim.json -api 127.0.0.1:8590

The API and the UI share -api (default 127.0.0.1:8590): / is the board, /api/v1/... is the JSON interface. The demo configuration ships with three blank cartridges on the shelf; media/ and library/ are created next to the process, and a cartridge is just a directory in ltfs-go's filebackend format.

iscsi-go (../iscsi-go) can be used as the initiator for manual testing:

cd ../iscsi-go && go run ./cmd/iscsi-tape -target iqn.2026-11.io.github.cyberax:tapesim.demo \
    -portal 127.0.0.1:3260 -lun 0 -status
# HPE ULTRIUM-LTO9 T3Q9 serial SIM0000001
# block size range 1..1048576
# state       ready
# position    partition 0, object 0
# mode 0x10   00 80 ...

Other flags worth knowing: -rewind -count 5 reads five blocks from BOT (-dump DIR writes them out), -locate N seeks, -attr 0x0806 dumps a MAM attribute as hex, -reserve takes a persistent reservation first.

Several targets, one process

The demo configuration exports two targets: the drive on port 3260 and the robot on 3261, each on LUN 0 under its own IQN — the zoning case, with one library model shared between them.

{ "targets": [
    { "targetName": "…:tapesim.demo",       "port": 3260,
      "drives":  [ { "name": "drive0", "lun": 0 } ] },
    { "targetName": "…:tapesim.demo.robot", "port": 3261,
      "changer": { "lun": 0, "slots": 24, "mediaBase": "media",
                   "driveElements": { "drive0": 0 } } } ] }

This split was the layout a client had to use while the initiator wrote the logical unit number into the wrong place on the wire; it is still worth configuring when you want to show a robot reaching across a portal, because the robot's driveElements names drives exported by any target — mtx load 1 on the robot's portal threads a cartridge into a drive the other portal exports, and ltfs-mount on that drive sees it. interop-smoke.sh's second phase is that case: changer unloads through one target, the drive target then refuses to mount, changer loads it back and the files are still there.

Logical units

One target can export the whole library, which is the shape real hardware has:

{ "targets": [ {
    "targetName": "…:tapesim.compact", "port": 3260,
    "drives":  [ { "name": "drive0", "lun": 0 }, { "name": "drive1", "lun": 1 } ],
    "changer": { "lun": 7, "slots": 24, "mediaBase": "media",
                 "driveElements": { "drive0": 0, "drive1": 1 } } } ] }
ltfs-mount  'iscsi://host:3260/…compact?lun=0&initiator=…' /mnt
changer     -f 'iscsi://host:3260/…compact?lun=7&initiator=…' status

It works because the LUN travels the way SAM and RFC 7143 §11.2.1.7 say it does: the field is SAM's eight-byte array with the unit number in LUN[1], which is the byte gotgt reads (and the byte open-iscsi fills). iscsi-go used to write the number as a big-endian integer instead, so LUN[1] stayed zero and every command — INQUIRY, READ, MOVE MEDIUM — addressed logical unit 0 whatever was asked: the robot was unreachable and the drive below it answered. Its codec owns the conversion now, so a LUN is a plain number at every API surface and ?lun=7 means unit 7.

Element addresses

The changer's element map follows the one a measured MSL3040 reports: the medium transport element (the arm) at 0, the drives — data transfer elements — from 1, and the shelf from 1001. Three ranges that neither overlap nor run into each other. A dense map is easier to write and wrong in ways that bite:

  • MODE SENSE page 0x1D is a fixed 18-byte payload of start/count pairs in that order, not a list of typed entries; a client that reads the pairs reads garbage out of an entry-per-type page.
  • READ ELEMENT STATUS for type 1 answers with an empty report on the measured device: the arm is announced in the address map and then says nothing, because it is not a place a cartridge rests.
  • With drives at 0..n-1 the arm and a drive share an address, so a move whose transport and destination are "the same element" is ambiguous — and a client that keeps one lock per drive cannot tell its own handle from somebody else's, which is how ltfs-go's robot ended up refusing its own load.

A target's LUNs live in s.targets[i].drives, so the same LUN number can exist on two targets. The API keeps addressing by LUN on the first target that exports it, and every drive and library endpoint takes ?target=<iqn-or-suffix>:

GET  /api/v1/targets                       — every target, portal and LUN
GET  /api/v1/drives/0?target=demo.robot    — one LUN of one named target
POST /api/v1/library/move?target=…robot    — move through that robot

gotgt numbers every driver's first target TID 0 and dispatches commands by looking that TID up in its process-global target list, so two targets in one process silently answer each other's commands unless they are renumbered; renumberTarget does that in both places gotgt keeps a TID. Two targets on one port is a configuration error rather than an os.Exit, and a wildcard bind is expanded per target. gotgt matches an incoming login's portal string for string, so a wildcard bind is expanded into the concrete addresses of the host (loopback plus every IPv4 interface); registering only 0.0.0.0:3260 would make the target listen on the port and then refuse every login with No TPGT found with IP(127.0.0.1:3260).

Layout

  • internal/tape — the device models: cartridge medium (persisted in the LTFS file-backend directory format ltfs-go reads), the drive (SSC-3/SPC-3 command set with the drive's exact sense shapes, MAM attributes, fault injection), the library (slots/cartridges), and the changer (MOVE MEDIUM, element addressing).
  • internal/sim — binds the models onto a gotgt iSCSI target. gotgt owns the iSCSI state machine; each LUN's PerformCommand is overridden by the model. See the gotgt-quirk comments — the seam needed three workarounds.
  • internal/api — JSON API (/api/v1/state, /api/v1/library, /api/v1/library/move, /api/v1/drives/{lun}[/calibrate]).
  • cmd/tapesim — the daemon.

Simulating the exceptional (what the fault engine is for)

A tape library is mostly about things going wrong slowly, and an LTFS stack has a lot of code for it that a happy-path simulator never exercises.

  • End of tape — "capacityMB": 12 on a drive makes a cartridge hold 12 MB instead of 18 TB, so the end-of-partition handshake is reachable between two keystrokes. The drive signals it the way the measured hardware does: writes inside the programmable early-warning window are accepted but answered NO SENSE / 0x00/0x01 + EOM (the stack ratchets that to metadata only), writes past the end are refused with NO SENSE / 0x00/0x02 + EOM (no space), and READ POSITION long carries bit 0x40 / bit 0x01 for the same two states. ltfs-mount on such a volume prints the reference's own "received low space warning (early warning) in write: the medium is at its end" and stops accepting data, cleanly.
  • Ambient faults — per drive: random calibration windows (02/04, DT busy in LOG SENSE), command stalls, and scheduled unit attentions and errors. Configure with POST /api/v1/drives/:lun/faults, or the per-drive panel in the UI; the same injection API overrides any of it for a deterministic test.

Status

  • iSCSI target on gotgt, multi-LUN
  • Tape drive: READ/WRITE (variable block, SILI, ILI, FM, blank-check EOD), LOCATE16/READ POSITION long with the drive's CP-bit and allocation-length quirks, SPACE, ERASE, LOAD/UNLOAD, MAM read/write attributes, MODE/LOG SENSE, PERSISTENT RESERVE arbitration, RSOC
  • Cartridge persistence in the filebackend dir format (ltfs-go can read the medium directories directly)
  • Fault injection: calibration windows, queued sense, per-opcode latency, connection-drop seam
  • Library + changer: element addressing (0x1D), MOVE MEDIUM, inventory persistence, LOAD/UNLOAD handshake
  • e2e conformance: the test suite drives the target through iscsi-go's real tape initiator — the same code path ltfs-go uses over iSCSI
  • HTMX UI: shelf and drive boards, cartridge memory, per-drive fault panel
  • MAM: READ/WRITE ATTRIBUTE for the LTFS and IBM sets, plus the SSC-defined medium attributes the drive answers from its own model — capacity, manufacturer, serial, barcode and a volume change reference that advances on every load, so a blank cartridge reads like real media instead of a drive with a dead chip (ltfs-mam <dev> list)
  • deeper SSC coverage: READ REVERSE, ERASE, early warning / end-of-tape positioning, compression and capacity mode pages
  • interop: ltfs-go formats, lists, mounts read-write through FUSE, and verifies a simulated volume end-to-end (tests/interop-smoke.sh)
  • changer conformance against ltfs-go's mtx-compatible changer client
  • several targets in one process (drive and robot on separate portals, one library model spanning them) — interop-smoke.sh phase 2
  • one target exporting drives and the robot on separate LUNs, formatted and mounted through ltfs-go's own binaries — interop-smoke.sh phase 3

Known upstream issues found while building this

  • gotgt v0.2.2: data races inside port/iscsit connection handlers (-race fails any two-goroutine traffic); newISCSITarget copies the api.SCSITarget struct so LUN maps bound after target creation are invisible; InSDBBuffer.Resid is used as a transfer count, not a residual; the LU registry is process-global.
  • iscsi-go: fixed (c362e7e). PDU.SetLUN wrote the logical unit number as a big-endian integer into the field. The field's location (BHS 8..15) was right — RFC 7143 §11.2.1.7 says LUN[0] is BHS byte 8 through LUN[7] at byte 15 — but it is SAM's array of eight bytes, whose addressing methods carry the unit number in LUN[1], so the integer form left LUN[1] zero and every command addressed logical unit 0. The same mistake sat in the target's REPORT LUNS answer, and the two were mutually consistent, which is why no test in that repository saw it: a drive and a medium changer on one target could not be told apart, and mtx-style clients reached whatever owned LUN 0. Request.LUN, target configs and URLs are numbers now and the codec converts (see Logical units).
  • ltfs-go: fixed (758a99c). OpenISCSI/OpenISCSIURL/OpenSCSI returned a concrete *ISCSI/*SCSI, so a failed open assigned a typed nil into a BlockDevice variable; if dev != nil { dev.Close() } — the guard in every command's teardown — then passed and dereferenced nil. A drive with no cartridge or a refused login segfaulted instead of printing its error. Found by driving the simulated library: unload a cartridge through the robot, then remount the empty drive. That is precisely the case where the message matters.
  • ltfs-go: fixed (3d63a98). devlock took a fresh flock per call, and flock locks an open file description rather than a process — so the changer, which holds the drive behind the robot and takes the same lock again to guard MOVE MEDIUM, refused its own load: "already held on this host by pid …" naming its own pid. Invisible while a robot and a drive sat on separate targets, which is what tapesim used to force; visible the moment the LUN fix made one-target layouts reachable, and invisible on real hardware too, because a real element map keeps the arm and the drives apart (see Element addresses).
  • ltfs-go: its own element addresses and page 0x1D reading are documented as measured in docs/changer-mtx-2026-10-27.md; tapesim follows that measurement, and the two agree on the arm-at-0 / drives-from-1 / shelf-from-1001 map.