Metadata-Version: 2.4
Name: minaki
Version: 0.1.0
Summary: MinakiLabs command-line tools. v0.1 ships one plugin: a read-only GRUB config check (minaki grub check).
Author: MinakiLabs
License: Apache-2.0
Project-URL: Homepage, https://github.com/MinakiLabs-Official/minaki
Project-URL: Issues, https://github.com/MinakiLabs-Official/minaki/issues
Keywords: minaki,grub,grub.cfg,bootloader,ubuntu,debian,update-grub,sysadmin
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: System :: Boot
Classifier: Topic :: System :: Systems Administration
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Dynamic: license-file

# minaki

MinakiLabs command-line tools: one install, one command. Version 0.1 ships one
command, `minaki grub`, a read-only check that your GRUB config can boot.

```
pip install minaki        # not on PyPI yet; for now: pip install .  (from a clone)
minaki grub check
minaki grub pin
minaki --help
```

Targets Ubuntu 24.04 first, and also Ubuntu 20.04, 22.04 and 26.04 and Debian 12.
It needs Python 3.8 or newer and only the standard library. It reads files and
runs `grub-script-check` (Debian/Ubuntu package `grub-common`). Run it as root,
since `grub.cfg` is often readable only by root.

## `minaki grub check`

`check` reads `/boot/grub/grub.cfg` once and runs every check on that one copy:

| Check | Fails when |
|---|---|
| `exists` | the file is missing (and GRUB is installed), or cannot be read |
| `size` | the file is empty (`--min-size BYTES` raises the floor) |
| `no-nul-bytes` | the file has NUL bytes, as a file zero-filled after a crash does |
| `menuentry` | there is no `menuentry` line, so GRUB has nothing to boot |
| `sections` | a `### BEGIN /etc/grub.d/... ###` section written by grub-mkconfig never reaches its `### END` |
| `grub-script-check` | `grub-script-check` rejects the file (it gets the same bytes on stdin) |

The required checks are: the file exists, is not empty, has a `menuentry`, and
`grub-script-check` accepts it. Two more checks are added. `no-nul-bytes`
catches a file that was zero-filled after a crash. `sections` catches a file
that grub-mkconfig started but that ends before a section it opened is closed.
A hand-written config with no section markers skips that check.

Exit codes:

| Code | Meaning |
|---|---|
| 0 | every check passed |
| 1 | the file is bad (the output names what failed and where the last-good copy is) |
| 2 | could not check: bad usage, `grub-script-check` missing, permission denied, or not applicable |

On a system that boots with systemd-boot, or that has no GRUB at all, there is no
GRUB config to check. `check` then reports `NOT APPLICABLE`, exits 2, and does
not crash.

It also warns, without failing, when a `grub-mkconfig` or `update-grub` is
running at that moment (read from `/proc`) or when `grub.cfg.new` exists. Either
one means the file may be in the middle of being written.

A healthy Ubuntu 24.04 system (real output from a lab VM):

```
$ sudo minaki grub check
minaki 0.1.0 grub check: /boot/grub/grub.cfg
  PASS   exists             7918 bytes, modified 2026-11-30T05:22:46Z
  PASS   size               7918 bytes, not empty
  PASS   no-nul-bytes       no NUL bytes
  PASS   menuentry          6 menuentry lines
  PASS   sections           12 generated sections, all closed
  PASS   grub-script-check  /usr/bin/grub-script-check exit 0
Result: OK (exit 0)
```

A syntax error:

```
$ sudo minaki grub check
minaki 0.1.0 grub check: /boot/grub/grub.cfg
  PASS   exists             7917 bytes, modified 2026-11-30T05:23:16Z
  PASS   size               7917 bytes, not empty
  PASS   no-nul-bytes       no NUL bytes
  PASS   menuentry          6 menuentry lines
  PASS   sections           12 generated sections, all closed
  FAIL   grub-script-check  exit 1: Syntax error at line 54
Result: FAILED: grub-script-check (exit 1)
Last good copy: /var/lib/minaki/grub.cfg.last (pinned 2026-11-30T05:23:16Z, 7918 bytes, sha256 04b7dfc37181d1d8)
```

## `minaki grub pin`

`pin` runs `check`. If every check passes, it saves those exact bytes to
`/var/lib/minaki/grub.cfg.last`, with a small `grub.cfg.last.json` holding the
sha256, size and time. Because it writes the bytes it checked, a file that
changes in between can never be pinned unchecked. The directory is mode 0700
and the files are 0600, since a GRUB config can hold password hashes. If the
copy already matches, it reports `unchanged`. If `check` fails, nothing is
written and the old last-good copy stays. It refuses a state directory under
`/boot`.

A good time to pin is after a kernel update that booted fine.

If `check` fails, run `sudo update-grub` once, with no other package manager
or `update-grub` running, then run `minaki grub check` again. The last-good copy
is there to compare against, or to restore by hand as a last resort. Before you
restore it, read it: it may name kernels that have since been removed.

## `--json`

Both commands take `--json` and print one object:

```json
{
  "tool": "minaki", "plugin": "grub", "version": "0.1.0", "command": "check",
  "status": "ok | failed | error | not_applicable", "exit_code": 0,
  "config": {"path": "/boot/grub/grub.cfg", "size": 9123, "sha256": "...", "mtime": "..."},
  "checks": [{"name": "exists", "status": "pass | fail | skip | error", "detail": "..."}],
  "failed": [], "errors": [], "warnings": [], "writers": [],
  "last_good": {"path": "/var/lib/minaki/grub.cfg.last", "sha256": "...", "pinned_at": "...", "matches_current": true}
}
```

`pin` adds `pinned` (true/false) and `pin_detail`.

## What it never does

- It never writes to `/boot`. It never runs `update-grub`, `grub-mkconfig` or
  `grub-install`, and never installs wrappers or hooks.
- It never touches the network: no telemetry, no update check, no account.
- It has no mode that runs two GRUB config writers at once. It reports on the
  race below; it is not a way to reproduce it.

What it does read: `grub.cfg`, `/proc/*/cmdline` (to spot running writers), and
the usual ESP paths (to spot systemd-boot). What it runs: `grub-script-check`,
which reads the config on stdin.

## The concurrent update-grub race

Two GRUB config writers running at the same time (for example a package hook
and a snapshot daemon, or two services at boot, each running `update-grub` or
`grub-mkconfig`) can leave the boot config broken or missing. Public reports
of this class of problem:

- [grub-btrfs issue #288](https://github.com/Antynea/grub-btrfs/issues/288)
  (2023): with `grub-btrfsd` running, a config rebuild during a pacman update
  failed with "cannot open `/boot/grub/grub.cfg.new'". The reporter suspected
  the daemon and the package hook were rebuilding at the same time.
- [TrueNAS middleware commit a50f509](https://github.com/truenas/middleware/commit/a50f509e0595b54010f88ee9016ecb75077a237b)
  (May 2026): two boot-time listeners both ran `grub-mkconfig` at the same
  time, "racing on `/boot/grub/grub.cfg.tmp(.new)`". The fix left one writer.
- [Launchpad #396381](https://bugs.launchpad.net/bugs/396381) (2009):
  `update-grub` ran twice during one package operation; the report suggests a
  dpkg trigger so it runs once.

`minaki grub` does not prevent this race. It tells you whether the file on disk
right now will boot, and keeps a copy of the last one that passed.

## Plugins

`minaki` parses only its own flags (`--help`, `--version`, `--plugins`) and
the command name. Everything after `minaki <command>` goes to that command
unchanged. Commands are plugins registered on the Python entry-point group
`minaki.plugins`. The core asks that group what is installed and never imports a
plugin by name.

Every plugin MinakiLabs writes ships inside the `minaki` package, so
`pip install minaki` gets all of them. Anyone can publish their own
`minaki-<something>` package that depends on `minaki` and registers on the same
group. There is no plugin store, downloader or auto-install.

Plugins build on a small SDK, `minaki.sdk`: an optional base class, the exit
codes, an argument parser named `minaki <name>`, and the standard `--json`. The
plugin API is versioned (now 1); a plugin that needs a newer API is refused with
a message to upgrade. `minaki --plugins [--json]` lists every registered
plugin, where it came from, and whether it loads.

- Guide and contract: [docs/PLUGINS.md](docs/PLUGINS.md)
- A complete plugin to copy: [examples/minaki-hello](examples/minaki-hello)

If two packages register the same name, the one inside `minaki` wins, and
`minaki --help` shows the clash. A plugin that fails to load is listed with its
error, and the other commands still work.

## Development

```
python3 -m unittest discover -s tests -v     # standard library only
```

The real `grub-script-check` tests run when `grub-common` is installed. CI runs
the suite inside Ubuntu 20.04, 22.04, 24.04 and 26.04 and Debian 12 containers,
as root and as a normal user. `tests/lab/` holds the scripts we run on
throwaway VMs. Those scripts break `grub.cfg` on purpose, so never run them on a
machine you care about.

## Tested on

Every case on a throwaway VM, 2026-11-30, minaki 0.1.0 installed from the wheel.
The cases: the real generated config passes, both as text and as JSON. `pin`,
then `pin` again, which reports unchanged. `/boot` is unchanged by the tool, and
the pinned copy is mode 0600 and byte-identical. Then the harness breaks the
file: empty, cut in half, cut at a clean line, syntax error, zero-filled,
missing, `grub.cfg.new` present, and `grub-script-check` removed. A run as
`nobody`, one real `update-grub`, and a final restore that is byte-identical
close it out. On the systemd-boot VM, `check` and `pin` report not applicable.

| System | Kernel | Python | Lab cases |
|---|---|---|---|
| Ubuntu 20.04 | 5.4.0-216-generic | 3.8.10 | 30 of 30 passed |
| Ubuntu 22.04 | 5.15.0-203-generic | 3.10.12 | 30 of 30 passed |
| Ubuntu 24.04 | 6.8.0-147-generic | 3.12.3 | 30 of 30 passed |
| Ubuntu 26.04 | 7.0.0-43-generic | 3.14.4 | 30 of 30 passed |
| Debian 12 | 6.1.0-56-cloud-amd64 | 3.11.2 | 30 of 30 passed |
| Ubuntu 24.04, UEFI, systemd-boot, GRUB removed | 6.8.0-151-generic | 3.12.3 | 6 of 6 passed |

## License

Apache-2.0. Copyright 2026 MinakiLabs LLC. See [LICENSE](LICENSE).

This checks the bootloader file and does not remove packages; Minaki Tuner ([minakilabs.com/tuner](https://minakilabs.com/tuner)) is the product that does.
