<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: metis atelier</title>
    <description>The latest articles on DEV Community by metis atelier (@metis_atelier_studio).</description>
    <link>https://dev.to/metis_atelier_studio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4078619%2F993238ec-d73d-4ccc-831c-1bb6e65cebe3.png</url>
      <title>DEV Community: metis atelier</title>
      <link>https://dev.to/metis_atelier_studio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/metis_atelier_studio"/>
    <language>en</language>
    <item>
      <title>Running Two Local GGUF Models on a Home Server, Switched from Telegram</title>
      <dc:creator>metis atelier</dc:creator>
      <pubDate>Tue, 24 Nov 2026 11:06:24 +0000</pubDate>
      <link>https://dev.to/metis_atelier_studio/running-two-local-gguf-models-on-a-home-server-switched-from-telegram-3jl9</link>
      <guid>https://dev.to/metis_atelier_studio/running-two-local-gguf-models-on-a-home-server-switched-from-telegram-3jl9</guid>
      <description>&lt;p&gt;Background&lt;/p&gt;

&lt;p&gt;I run a personal AI assistant (Hermes Agent) on a VPS. The local models run on a home server with two GPUs: an RTX 3060 (12 GB) and an RTX 3070 (8 GB). The 3070 mines when idle. The 3060 normally serves a local Gemma model through llama.cpp. When the home server is off or unreachable, the assistant falls back to OpenRouter.&lt;/p&gt;

&lt;p&gt;This post covers how I added a second local model (Inkling-2-Mini Uncensored, IQ2_M quantization, about 8.7 GB) and made it possible to switch between the two models from Telegram.&lt;/p&gt;

&lt;p&gt;Goals&lt;/p&gt;

&lt;p&gt;• Pick Gemma or Inkling from Telegram's /model menu&lt;br&gt;
• Serve both models from the same llama.cpp server on port 8080&lt;br&gt;
• Keep a 64k context, which the assistant needs for summarization&lt;br&gt;
• Keep mining idle-only, and give the LLM priority whenever it is needed&lt;/p&gt;

&lt;p&gt;What Went Wrong (and Why)&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The build ran out of memory&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Running cmake --build with -j and no number runs unlimited parallel jobs. CUDA compilation is memory-heavy, so the kernel's OOM killer stopped the compiler. The fix was an explicit job count (-j 1) and a background build with nohup.&lt;/p&gt;

&lt;p&gt;Lesson: A bare -j does not mean "use all cores." It means unlimited.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;cudaMalloc failed with plenty of free VRAM&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The 3060 showed about 11.9 GB free, yet loading an 8 GB model failed. I tried several wrong fixes first: batch size, multimodal offload, and locked-memory limits.&lt;/p&gt;

&lt;p&gt;The real cause was that the production service set CUDA_DEVICE_ORDER=PCI_BUS_ID, but my interactive shell did not. Without it, CUDA numbers GPUs by speed, so CUDA_VISIBLE_DEVICES=0 pointed at the 3070. nvidia-smi lists GPUs in PCI order, which made the mismatch hard to see.&lt;/p&gt;

&lt;p&gt;Lesson: When CUDA reports out of memory on a device that has free memory, first check which physical GPU the process actually uses.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The model list shows only the running model&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Telegram, /model for a custom provider lists the models returned by the endpoint's /v1/models. The server runs one model at a time, so the list showed only whichever model was loaded. That looked odd next to the OpenRouter list, which is a fixed catalog.&lt;/p&gt;

&lt;p&gt;Reading the Hermes source showed that a custom provider's list comes from /models unless you set discover_models: false. With that flag, Hermes uses the models: dictionary in config.yaml.&lt;/p&gt;

&lt;p&gt;Entries that share the same URL are also merged into one picker row. So I put both models under a single provider entry.&lt;/p&gt;

&lt;p&gt;Lesson: For a fixed picker list on a custom provider, use discover_models: false with an explicit models: dictionary.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A 64k context needs more VRAM than the 3060 had&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hermes requires at least 64,000 tokens of context for summarization. Inkling at 8k could not handle that step.&lt;/p&gt;

&lt;p&gt;Running Inkling at 64k on the 3060 took two changes:&lt;/p&gt;

&lt;p&gt;• Batch size -b 128 and micro-batch -ub 128, to shrink the compute buffer&lt;br&gt;
• Quantized KV cache (--cache-type-k q8_0 --cache-type-v q8_0), to shrink the KV cache&lt;/p&gt;

&lt;p&gt;Inkling then ran at about 11.3 GB with a 64k context. Only about 1 GB stayed free, so long conversations are the next thing to watch.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hidden restarts and timers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Several things start or stop the LLM server without a manual command:&lt;/p&gt;

&lt;p&gt;• An idle monitor switches the 3060 to mining after 10 minutes without GPU load&lt;br&gt;
• A wake hook starts the server before each model call (by design, so the LLM takes priority over mining)&lt;br&gt;
• systemd restarts a failing service&lt;/p&gt;

&lt;p&gt;I disabled the idle timer during tests and re-enabled it afterward.&lt;/p&gt;

&lt;p&gt;Final Architecture&lt;/p&gt;

&lt;p&gt;• One llama-server unit on port 8080. Model arguments come from an environment file (gemma.env or inkling.env).&lt;br&gt;
• One binary (llama.cpp build 12652) for both models.&lt;br&gt;
• A root-owned switch script that restarts the server only when the requested model differs from the running one.&lt;br&gt;
• A restricted SSH command channel that the assistant uses to call wake-llama or wake-inkling.&lt;br&gt;
• A per-turn wake plugin that picks the wake command from the model path in config.yaml.&lt;/p&gt;

&lt;p&gt;Provider Configuration Example&lt;/p&gt;

&lt;p&gt;Here is a sanitized example of the config.yaml sections that make the picker and the switching work. Replace the placeholders with your own values. Do not put API keys in the file. Use an environment variable instead.&lt;/p&gt;

&lt;p&gt;model:&lt;br&gt;
  default: /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf   # the model in use now&lt;br&gt;
  provider: custom:home-server                                         # the provider name shown in /model&lt;br&gt;
  base_url: http://:8080/v1&lt;br&gt;
  context_length: 65536                                                # global default (lost on /model switch)&lt;/p&gt;

&lt;p&gt;providers:&lt;br&gt;
  custom:home-server:&lt;br&gt;
    base_url: http://:8080/v1&lt;br&gt;
    key_env: OPENAI_API_KEY                  # key comes from the environment, not from this file&lt;br&gt;
    default_model: /models/gemma4-12b-qat-q4_k_m.gguf                    # listed first in the picker&lt;br&gt;
    discover_models: false                   # do not ask the server for its model list&lt;br&gt;
    models:&lt;br&gt;
      /models/gemma4-12b-qat-q4_k_m.gguf:&lt;br&gt;
        context_length: 65536&lt;br&gt;
      /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:&lt;br&gt;
        context_length: 65536&lt;br&gt;
    request_timeout_seconds: 120&lt;/p&gt;

&lt;p&gt;custom_providers:                            # per-model overrides that survive /model switches&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name: home-server
base_url: http://:8080/v1
discover_models: false
models:
  /models/gemma4-12b-qat-q4_k_m.gguf:
    context_length: 65536
  /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:
    context_length: 65536&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;fallback_providers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;provider: openrouter
model: deepseek/deepseek-v4-flash-0731&lt;/li&gt;
&lt;li&gt;provider: openrouter
model: openrouter/free&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What each key does&lt;/p&gt;

&lt;p&gt;• providers.custom:.discover_models: false stops Hermes from asking the server for its model list. The picker then shows only the models: you wrote.&lt;br&gt;
• models: is a dictionary keyed by model path. The first entry in the list comes from default_model.&lt;br&gt;
• custom_providers with the same URL is merged into the same picker row. Use it for per-model context_length, because model.context_length alone is lost after a /model switch.&lt;br&gt;
• key_env names an environment variable that holds the key. The key itself stays out of the YAML.&lt;/p&gt;

&lt;p&gt;How to Switch Models from Telegram&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open the menu. Send /model to the bot. It shows the current model and a list of providers.&lt;/li&gt;
&lt;li&gt;Pick the provider. Choose custom:home-server from the provider buttons. It appears as a single button.&lt;/li&gt;
&lt;li&gt;Pick a model. The model screen shows both local models as separate buttons: Gemma and Inkling. Choose one. Hermes saves the choice to config.yaml (model.default and model.provider).&lt;/li&gt;
&lt;li&gt;Send a message. The first message after a switch triggers the wake hook, which tells the home server to load the chosen model. Loading takes roughly 30 to 60 seconds. During that time, the assistant may answer through OpenRouter.&lt;/li&gt;
&lt;li&gt;Check the result. On the server, confirm which model is active and how much VRAM it uses:
readlink /home//llama-models/active.env
nvidia-smi --query-gpu=index,memory.used --format=csv
◦ gemma.env with about 9.2 GB on the 3060 means Gemma.
◦ inkling.env with about 11.3 GB on the 3060 means Inkling.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Notes&lt;/p&gt;

&lt;p&gt;• Pressing Cancel leaves the current setting unchanged.&lt;br&gt;
• Choosing a model only records it. The model loads on the next message.&lt;br&gt;
• If the home server is off, the assistant falls back to OpenRouter.&lt;br&gt;
• Keep the menu's selection and the server's active model in agreement. If they differ, the wrong model can answer or the request can fail.&lt;/p&gt;

&lt;p&gt;Takeaways&lt;/p&gt;

&lt;p&gt;• Read the source of your tools before guessing. I spent hours tuning VRAM settings, but the picker problem was solved in minutes once I read the code.&lt;br&gt;
• Keep "what the config says" and "what is running now" separate in your head, and check both.&lt;br&gt;
• Back up before every config change, and keep a restore command next to each change.&lt;/p&gt;

&lt;p&gt;--&lt;br&gt;
Telegram からモデルを切り替える、自宅サーバーでのローカルLLM運用&lt;/p&gt;

&lt;p&gt;背景&lt;/p&gt;

&lt;p&gt;VPS で AI アシスタント（Hermes Agent）を動かしています。ローカルの LLM は、自宅サーバーの2枚の GPU（RTX 3060 12GB と RTX 3070 8GB）で動かしています。3070 はアイドル時に採掘に使い、3060 は通常、llama.cpp で Gemma を動かします。自宅サーバーに繋がらないときは、OpenRouter に自動で切り替わります。&lt;/p&gt;

&lt;p&gt;この記事では、2つ目のローカルモデル（Inkling-2-Mini Uncensored、IQ2_M、約8.7GB）を追加して、Telegram から2つのモデルを切り替えられるようにした話をまとめます。&lt;/p&gt;

&lt;p&gt;目的&lt;/p&gt;

&lt;p&gt;• Telegram の /model から、Gemma と Inkling を選べるようにする&lt;br&gt;
• 両方を、同じ llama.cpp サーバー（8080番）で動かす&lt;br&gt;
• アシスタントが要約に使う、64k のコンテキストを確保する&lt;br&gt;
• マイニングは隙間時間だけ。LLM が必要なときは LLM を優先する&lt;/p&gt;

&lt;p&gt;つまずき（と原因）&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;ビルドがメモリ不足で止まった&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;cmake --build で -j に数字を付けずに実行すると、並列数が無制限になります。CUDA のコンパイルはメモリを多く使うので、カーネルの OOM キラーがコンパイラを止めました。-j 1 と明示し、nohup でバックグラウンド実行して解決しました。&lt;/p&gt;

&lt;p&gt;教訓： -j の後の数字は省略できません。省略すると「全コア使用」ではなく「無制限」になります。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;空きVRAMがあるのに cudaMalloc failed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3060 に約11.9GBの空きがあるのに、8GBのモデルの読み込みが失敗しました。バッチサイズや mmproj、ロック可能メモリの上限を疑いましたが、どれも外れでした。&lt;/p&gt;

&lt;p&gt;本当の原因は、サービス（systemd）には CUDA_DEVICE_ORDER=PCI_BUS_ID が設定されていたのに、手作業のシェルには設定されていなかったことです。この変数がないと、CUDA は GPU を速い順に番号付けします。その結果、CUDA_VISIBLE_DEVICES=0 が 3060 ではなく 3070 を指していました。nvidia-smi は PCI 順で表示するので、食い違いに気づきにくかったです。&lt;/p&gt;

&lt;p&gt;教訓： CUDA の out of memory が出たら、パラメータを調整する前に、プロセスが実際にどの物理GPUを使っているかを確認しましょう。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;モデル一覧に「今動いているモデル」しか出ない&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Telegram で custom provider の /model を開くと、エンドポイントの /v1/models が返すモデルが一覧に出ます。サーバーは同時に1つのモデルしか動かさないので、一覧には今読み込まれているモデルしか出ませんでした。OpenRouter の一覧は固定なので、この違いが気になりました。&lt;/p&gt;

&lt;p&gt;Hermes のソースを読むと、custom provider の一覧は discover_models: false を設定しない限り /models から作られることが分かりました。false にすると、config.yaml の models:（モデル名をキーにした辞書）をそのまま一覧に使います。&lt;/p&gt;

&lt;p&gt;同じURLの entry は、1つの行にまとめられることも分かりました。そこで、Gemma と Inkling を、1つの provider の中に入れることにしました。&lt;/p&gt;

&lt;p&gt;教訓： custom provider で一覧を固定するには、discover_models: false と、明示的な models: の辞書を使います。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;64k のコンテキストは、3060 には重かった&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hermes は、要約（圧縮）のために少なくとも 64,000 トークンのコンテキストを必要とします。8k で動かしていた Inkling では、この条件を満たせませんでした。&lt;/p&gt;

&lt;p&gt;Inkling を 64k で3060に載せるには、次の2つを組み合わせました。&lt;/p&gt;

&lt;p&gt;• バッチサイズ -b 128 と、マイクロバッチ -ub 128（計算用バッファを小さくする）&lt;br&gt;
• KV キャッシュの量子化 --cache-type-k q8_0 --cache-type-v q8_0（KV の領域を小さくする）&lt;/p&gt;

&lt;p&gt;結果として、Inkling は 64k で約11.3GB を使って動きました。空きは1GB程度しかないので、長い会話をしたときは注意が必要です。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;勝手に止まる・起きる仕組み&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;LLM のサーバーは、次の仕組みで自動的に止まったり起きたりします。&lt;/p&gt;

&lt;p&gt;• アイドル監視：GPU の負荷が10分間ないと、3060 をマイニングに切り替える&lt;br&gt;
• wake フック：LLM を呼ぶ前に、サーバーを起こす（LLM を採掘より優先するため、毎ターン送っています）&lt;br&gt;
• systemd：失敗したサービスを再起動する&lt;/p&gt;

&lt;p&gt;テスト中は、アイドル監視のタイマーを止めておき、終わったら戻しました。&lt;/p&gt;

&lt;p&gt;最終的な構成&lt;/p&gt;

&lt;p&gt;• llama-server の unit は1つ（8080番）。モデルの引数は、環境ファイル（gemma.env / inkling.env）から読む&lt;br&gt;
• バイナリは1つ（llama.cpp の build 12652）で、両方のモデルを動かす&lt;br&gt;
• root 所有の切り替えスクリプトが、要求されたモデルと今のモデルが違うときだけ、サーバーを止めて起動し直す&lt;br&gt;
• 制限された SSH コマンドから、wake-llama か wake-inkling を呼ぶ&lt;br&gt;
• 毎ターンの wake プラグインが、config.yaml のモデルのパスから、どちらを呼ぶかを決める&lt;/p&gt;

&lt;p&gt;プロバイダー設定の例（YAML）&lt;/p&gt;

&lt;p&gt;Telegram の一覧と切り替えを動かしている config.yaml の部分を、伏せた形で載せます。 などは、自分の値に置き換えてください。APIキーはファイルに書かず、環境変数（key_env）で渡します。&lt;/p&gt;

&lt;p&gt;model:&lt;br&gt;
  default: /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf   # 今使っているモデル&lt;br&gt;
  provider: custom:home-server                                         # /model に出る provider 名&lt;br&gt;
  base_url: http://:8080/v1&lt;br&gt;
  context_length: 65536                                                # 全体の既定値（/model で切り替えると消える）&lt;/p&gt;

&lt;p&gt;providers:&lt;br&gt;
  custom:home-server:&lt;br&gt;
    base_url: http://:8080/v1&lt;br&gt;
    key_env: OPENAI_API_KEY                  # キーは環境変数から読む。ファイルには書かない&lt;br&gt;
    default_model: /models/gemma4-12b-qat-q4_k_m.gguf                    # 一覧の先頭に出る&lt;br&gt;
    discover_models: false                   # サーバーにモデル一覧を聞きに行かない&lt;br&gt;
    models:&lt;br&gt;
      /models/gemma4-12b-qat-q4_k_m.gguf:&lt;br&gt;
        context_length: 65536&lt;br&gt;
      /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:&lt;br&gt;
        context_length: 65536&lt;br&gt;
    request_timeout_seconds: 120&lt;/p&gt;

&lt;p&gt;custom_providers:                            # /model の切り替え後も残る、モデルごとの設定&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name: home-server
base_url: http://:8080/v1
discover_models: false
models:
  /models/gemma4-12b-qat-q4_k_m.gguf:
    context_length: 65536
  /models/inkling2/Inkling-2-Mini-Uncensored-AD-IQ2_M.gguf:
    context_length: 65536&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;fallback_providers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;provider: openrouter
model: deepseek/deepseek-v4-flash-0731&lt;/li&gt;
&lt;li&gt;provider: openrouter
model: openrouter/free&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;各項目の意味&lt;/p&gt;

&lt;p&gt;• providers.custom:&amp;lt;名前&amp;gt;.discover_models: false：サーバーにモデル一覧を聞きに行かず、models: に書いたものだけを一覧に出す。&lt;br&gt;
• models: はモデルのパスをキーにした辞書。一覧の先頭は default_model で決まる。&lt;br&gt;
• 同じURLの custom_providers は、同じ行にまとめられる。モデルごとの context_length は、ここに書くと /model で切り替えても残る。&lt;br&gt;
• key_env は、キーが入った環境変数の名前を指定する。キーそのものは YAML に書かない。&lt;/p&gt;

&lt;p&gt;Telegram からモデルを切り替える方法&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;メニューを開く：ボットに /model と送ります。現在のモデルと、provider の一覧が出ます。&lt;/li&gt;
&lt;li&gt;provider を選ぶ：custom:home-server を選びます。ローカルの2つのモデル（Gemma と Inkling）が並びます。&lt;/li&gt;
&lt;li&gt;モデルを選ぶ：Gemma か Inkling を選びます。Hermes は選んだ内容を config.yaml（model.default と model.provider）に保存します。&lt;/li&gt;
&lt;li&gt;プロバイダーを選ぶ：プロバイダーのボタン一覧から custom:home-server を押します。ここでは1つのボタンとして表示されます。&lt;/li&gt;
&lt;li&gt;モデルを選ぶ：押したあとの画面に、ローカルの2つのモデル（Gemma と Inkling）が別々のボタンで並びます。どちらかを押すと、Hermes が選んだ内容を config.yaml（model.default と model.provider）に保存します。&lt;/li&gt;
&lt;li&gt;メッセージを送る：切り替え後の最初のメッセージで、wake フックが走り、自宅サーバーが選んだモデルを読み込みます。読み込みには30〜60秒ほどかかり、その間は OpenRouter で応答することがあります。&lt;/li&gt;
&lt;li&gt;結果を確認する：サーバーで、今のモデルと VRAM の使用量を確認します。
readlink /home/&amp;lt;ユーザー名&amp;gt;/llama-models/active.env
nvidia-smi --query-gpu=index,memory.used --format=csv
◦ gemma.env で、3060 が約9.2GB なら Gemma です。
◦ inkling.env で、3060 が約11.3GB なら Inkling です。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;補足&lt;/p&gt;

&lt;p&gt;• Cancel を押すと、設定は変わりません。&lt;br&gt;
• モデルを選んだだけでは、読み込まれません。読み込まれるのは、次のメッセージを送ったときです。&lt;br&gt;
• 自宅サーバーが落ちているときは、OpenRouter に自動で切り替わります。&lt;br&gt;
• メニューで選んだモデルと、サーバーで今動いているモデルは、必ず揃えておきましょう。ずれると、違うモデルが応答したり、リクエストが失敗したりします。&lt;/p&gt;

&lt;p&gt;まとめ&lt;/p&gt;

&lt;p&gt;• ツールの動きは、推測よりソースを読むほうが早いです。VRAM の調整に何時間もかけましたが、一覧の問題は、コードを読んだら数分で分かりました。&lt;br&gt;
• 「設定に書いてあること」と「今動いていること」は、分けて考えて、両方を確認しましょう。&lt;br&gt;
• 設定を変えるたびにバックアップを取り、元に戻すコマンドも用意しておきましょう。&lt;/p&gt;

</description>
      <category>ai</category>
      <category>hardware</category>
      <category>llm</category>
    </item>
    <item>
      <title>Hermes Agent + local Gemma(llama)</title>
      <dc:creator>metis atelier</dc:creator>
      <pubDate>Tue, 24 Nov 2026 00:14:47 +0000</pubDate>
      <link>https://dev.to/metis_atelier_studio/hermes-agent-local-gemmallama-593p</link>
      <guid>https://dev.to/metis_atelier_studio/hermes-agent-local-gemmallama-593p</guid>
      <description>&lt;h1&gt;
  
  
  Switching Between a Home LLM and a Free Cloud Model — Safely
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;A practical setup with llama.cpp + Tailscale + OpenRouter + Hermes Agent&lt;/em&gt;&lt;br&gt;
&lt;em&gt;By Matis atelier&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;About the date of this information&lt;/strong&gt;&lt;br&gt;
Everything here reflects the state of the software &lt;strong&gt;as of August 2026&lt;/strong&gt;, when I read the Hermes Agent source code directly because I could not find this behavior documented. Official documentation has probably improved since then. &lt;strong&gt;Check the current docs first&lt;/strong&gt;; treat this article as the "why it behaves that way" companion, not as the final word. Config key names may have changed.&lt;br&gt;
All IPs, keys, hostnames and paths below are placeholders.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  1. The goal
&lt;/h2&gt;

&lt;p&gt;I run an LLM at home (llama.cpp, Gemma 4 12B) and an agent (Hermes Agent) on a VPS. I wanted:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Home server is up&lt;/td&gt;
&lt;td&gt;Use the home LLM&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Home server is down&lt;/td&gt;
&lt;td&gt;Use a free cloud model (OpenRouter)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;The free model also fails&lt;/td&gt;
&lt;td&gt;Retry, and surface an error if it still fails&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Why bother? The home box is not on 24/7 (power schedule), and free cloud models are rate-limited when you need them most. Combining both is cheap and resilient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Telegram / cron
       │
       ▼
┌──────────────┐   Tailscale (encrypted)   ┌────────────────────┐
│ Agent (VPS)  │ ────────────────────────► │ Home LLM server    │
│              │   Bearer token required   │ llama.cpp          │
└──────┬───────┘                           └────────────────────┘
       │ if unreachable / failing
       ▼
  OpenRouter  (openrouter/free)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. Step 1 — Lock down the local server first
&lt;/h2&gt;

&lt;p&gt;A local LLM endpoint is an unauthenticated compute resource unless you protect it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) Require an API key in llama.cpp&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;llama serve -m &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;model.gguf&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --host &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;bind-address&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --port 8080 \
  --api-key &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;long-random-token&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generate the token with &lt;code&gt;openssl rand -hex 32&lt;/code&gt;. In systemd, remember the trailing &lt;code&gt;\&lt;/code&gt; on every continued &lt;code&gt;ExecStart=&lt;/code&gt; line, or the flag is silently dropped. Then &lt;code&gt;daemon-reload&lt;/code&gt; and restart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) Reach it over a private network, not the open internet&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Tailscale (or WireGuard). Important: &lt;strong&gt;both ends need the client installed.&lt;/strong&gt; I assumed the VPS side already had it; it did not, so every connection timed out until I installed it. Verify with &lt;code&gt;tailscale status&lt;/code&gt; on &lt;em&gt;both&lt;/em&gt; machines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c) Don't bind more widely than you need&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--host 0.0.0.0&lt;/code&gt; listens on every interface. The API key is a good last line of defense, but prefer binding to the Tailscale address (or firewalling port 8080 to the Tailscale interface) and restricting who can reach it with Tailscale ACLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;d) Keep secrets out of chat logs, tickets and screenshots.&lt;/strong&gt; If a token ever lands somewhere public, rotate it.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Step 2 — Register the server in the agent (the gotcha that cost me hours)
&lt;/h2&gt;

&lt;p&gt;The obvious config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;custom&lt;/span&gt;
  &lt;span class="na"&gt;base_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://&amp;lt;tailscale-ip&amp;gt;:8080/v1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;plus &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; in the environment. Result: &lt;strong&gt;&lt;code&gt;401 Invalid API Key&lt;/code&gt;&lt;/strong&gt; — while &lt;code&gt;curl&lt;/code&gt; with the same key from inside the same container returned &lt;code&gt;200&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Reading the credential resolver showed why. For a bare &lt;code&gt;provider: custom&lt;/code&gt; with a &lt;code&gt;base_url&lt;/code&gt;, the generic &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; fallback was only used when the host was &lt;code&gt;openai.com&lt;/code&gt; / &lt;code&gt;openai.azure.com&lt;/code&gt;. For any other host the key was silently dropped and replaced by a &lt;code&gt;"no-key-required"&lt;/code&gt; placeholder. This looks like a deliberate guard against leaking a generic key to arbitrary endpoints — sensible, but silent.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix: register a &lt;em&gt;named&lt;/em&gt; custom provider with an explicit &lt;code&gt;key_env&lt;/code&gt;:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;model-name-or-path&amp;gt;&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt;
  &lt;span class="na"&gt;api_mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;chat_completions&lt;/span&gt;

&lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;"custom:home-llm"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;base_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://&amp;lt;tailscale-ip&amp;gt;:8080/v1&lt;/span&gt;
    &lt;span class="na"&gt;key_env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;OPENAI_API_KEY&lt;/span&gt;        &lt;span class="c1"&gt;# name of the env var that holds the token
&lt;/span&gt;    &lt;span class="na"&gt;default_model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;model-name-or-path&amp;gt;&lt;/span&gt;
    &lt;span class="na"&gt;request_timeout_seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;   &lt;span class="c1"&gt;# see "Unresolved" below
&lt;/span&gt;
&lt;span class="na"&gt;fallback_model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;openrouter&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;openrouter/free&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Named providers resolve the key through the explicit &lt;code&gt;key_env&lt;/code&gt; lookup and skip the hostname gate. I used &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; as the variable name; the code suggests any name works, but I did not test other names.&lt;/p&gt;

&lt;p&gt;Note the model name: llama.cpp reports the model's file path as its ID in &lt;code&gt;/v1/models&lt;/code&gt;, so that is what you put in &lt;code&gt;default&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Step 3 — The fallback
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fallback_model&lt;/code&gt; takes a single provider/model (a list is also accepted for a chain). When the primary exhausts its retries on a connection or rate-limit error, the agent switches over automatically. I observed it working:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Max retries (3) exhausted — trying fallback...
Primary model failed — switching to fallback ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Choosing the fallback model mattered:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A specific model that was retired (&lt;code&gt;HTTP 404: No endpoints found&lt;/code&gt;) → total failure.&lt;/li&gt;
&lt;li&gt;A specific free model that was busy (&lt;code&gt;HTTP 429&lt;/code&gt;) → total failure.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;openrouter/free&lt;/code&gt;&lt;/strong&gt; — OpenRouter's router that picks from currently available free models and filters for features your request needs → far more resilient.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Caveat: free-tier rate limits are account-wide, so &lt;code&gt;openrouter/free&lt;/code&gt; reduces, but does not eliminate, 429s. If the free fallback also fails, the request errors out after its retries (it does not wait forever); a scheduled job simply tries again on its next run.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Pitfall checklist
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Symptom&lt;/th&gt;
&lt;th&gt;Cause&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Timeouts to the home server&lt;/td&gt;
&lt;td&gt;Tailscale missing on one end&lt;/td&gt;
&lt;td&gt;Install and join on both machines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;401&lt;/code&gt; with a correct key&lt;/td&gt;
&lt;td&gt;Hostname gate on generic env key&lt;/td&gt;
&lt;td&gt;Named provider + &lt;code&gt;key_env&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.env&lt;/code&gt; change ignored&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker restart&lt;/code&gt; does not reload env; compose had no &lt;code&gt;env_file&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Add &lt;code&gt;env_file: .env&lt;/code&gt;, then &lt;code&gt;docker compose up -d --force-recreate&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Key looks like it ends in &lt;code&gt;\r&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker exec -it&lt;/code&gt; pseudo-TTY adds it to output&lt;/td&gt;
&lt;td&gt;Re-check without &lt;code&gt;-t&lt;/code&gt;; the value was fine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;systemd&lt;/code&gt; flag has no effect&lt;/td&gt;
&lt;td&gt;Missing trailing &lt;code&gt;\&lt;/code&gt; in &lt;code&gt;ExecStart=&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Fix continuation lines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Every request times out with long prompts&lt;/td&gt;
&lt;td&gt;Draft-model / speculative decoding overhead and small context&lt;/td&gt;
&lt;td&gt;I removed the draft model and raised &lt;code&gt;-c&lt;/code&gt; to 65536&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  6. Step 4 — Don't wait minutes when the server is powered off
&lt;/h2&gt;

&lt;p&gt;When the home machine is &lt;em&gt;powered off&lt;/em&gt;, nothing answers the TCP SYN, so there is no quick "connection refused". Each attempt waited roughly 30 seconds, and with retries plus an extra primary retry round, switching took &lt;strong&gt;about 3 minutes&lt;/strong&gt;. Painful for every message sent while the box is off.&lt;/p&gt;

&lt;p&gt;(&lt;code&gt;request_timeout_seconds&lt;/code&gt; did not seem to change this. See "Unresolved".)&lt;/p&gt;

&lt;p&gt;A fixed-time schedule is fragile: boot takes a while before the model is ready, and shutdown (mine is triggered by a power-consumption watchdog) is not at a precise time. So instead of trusting the clock, &lt;strong&gt;health-check the server and let reality drive the switch.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The agent CLI can change config at runtime — no container recreation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt;
hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"&amp;lt;model-name-or-path&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both keys must move together; changing only the provider leaves a local file path as the model name, which the cloud provider rejects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Watchdog (run from cron every 3 minutes):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# home-llm-watchdog.sh&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -u
&lt;span class="nb"&gt;source&lt;/span&gt; /etc/home-llm-watchdog.env   &lt;span class="c"&gt;# chmod 600; defines HOME_LLM_URL, HOME_LLM_KEY, LOCAL_MODEL&lt;/span&gt;
&lt;span class="nv"&gt;STATE_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/lib/home-llm-watchdog.state
&lt;span class="nv"&gt;LOG_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/log/home-llm-watchdog.log

&lt;span class="nv"&gt;CURRENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dead
&lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;curl -s -o /dev/null -w &lt;span class="s1"&gt;'%{http_code}'&lt;/span&gt; --max-time 3 \
  -H &lt;span class="s2"&gt;"Authorization: Bearer ${HOME_LLM_KEY}"&lt;/span&gt; &lt;span class="s2"&gt;"${HOME_LLM_URL}/models"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$code"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"200"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;CURRENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;alive

&lt;span class="nv"&gt;PREVIOUS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;cat &lt;span class="s2"&gt;"$STATE_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; unknown&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; != &lt;span class="s2"&gt;"$PREVIOUS"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; alive &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"$LOCAL_MODEL"&lt;/span&gt;    &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
  &lt;span class="k"&gt;else&lt;/span&gt;
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"openrouter"&lt;/span&gt;      &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"openrouter/free"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
  &lt;span class="k"&gt;fi&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[$(date '+%F %T')] $PREVIOUS -&amp;gt; $CURRENT"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$STATE_FILE"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*/3 * * * * /usr/local/bin/home-llm-watchdog.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Design notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;State file&lt;/strong&gt; → only acts when the state flips; no log noise or needless writes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the built-in fallback too.&lt;/strong&gt; If the server is &lt;em&gt;supposed&lt;/em&gt; to be up but crashed, the watchdog still detects it, and the retry-then-fallback path remains a safety net in between checks.&lt;/li&gt;
&lt;li&gt;The secret lives in a &lt;code&gt;600&lt;/code&gt; file instead of the script. (The first version I ran had it inline; don't copy that.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Verified end-to-end: the machine powered on by its timer, and within 3 minutes the agent switched back to the local model by itself.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Security checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] API key required on the local server&lt;/li&gt;
&lt;li&gt;[ ] Private network (Tailscale/WireGuard), installed on &lt;strong&gt;both&lt;/strong&gt; ends&lt;/li&gt;
&lt;li&gt;[ ] Bind address / firewall narrower than &lt;code&gt;0.0.0.0&lt;/code&gt; on the open internet&lt;/li&gt;
&lt;li&gt;[ ] Tailscale ACL limits which nodes can reach the port&lt;/li&gt;
&lt;li&gt;[ ] Secrets in &lt;code&gt;600&lt;/code&gt; files, never in scripts, chat logs or screenshots&lt;/li&gt;
&lt;li&gt;[ ] Rotate any token that was exposed&lt;/li&gt;
&lt;li&gt;[ ] Prefer explicit &lt;code&gt;key_env&lt;/code&gt; per provider over a generic key that could be sent to the wrong host&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  8. Unresolved
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;request_timeout_seconds&lt;/code&gt; appeared to have no effect on the effective wait; I did not find the cause. Renaming the &lt;code&gt;providers&lt;/code&gt; key to match &lt;code&gt;custom:&amp;lt;name&amp;gt;&lt;/code&gt; did not change it. Newer versions may differ.&lt;/li&gt;
&lt;li&gt;A harmless warning about missing endpoint credentials appeared from an auxiliary (non-chat) task; it did not affect normal conversations.&lt;/li&gt;
&lt;li&gt;Selecting &lt;em&gt;which&lt;/em&gt; local model (e.g. Gemma vs. Qwen) is a separate layer from this up/down switch; I kept this to liveness only.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Secure the local server (token + private network), on both ends.&lt;/li&gt;
&lt;li&gt;Register it as a &lt;strong&gt;named&lt;/strong&gt; custom provider with &lt;code&gt;key_env&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;openrouter/free&lt;/code&gt; as the fallback.&lt;/li&gt;
&lt;li&gt;If the box is power-scheduled, add a health-check watchdog that flips config with &lt;code&gt;hermes config set&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Hermes Agent behavior described here was observed in August 2026. Check current official docs before relying on it.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  自宅のローカルLLMと無料クラウドモデルを安全に切り替える
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;llama.cpp + Tailscale + OpenRouter + Hermes Agent で作る実践構成&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Matis atelier&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;この情報の時点について&lt;/strong&gt;&lt;br&gt;
本記事の内容は &lt;strong&gt;2026年8月時点&lt;/strong&gt; のものです。公式ドキュメントに該当の記述が見つからなかったため、Hermes Agent のソースコードを直接読んで挙動を確認しました。その後、公式情報は充実している可能性が高いです。&lt;strong&gt;まず最新の公式ドキュメントを確認してください。&lt;/strong&gt; 本記事は「なぜそう動くのか」を補う読み物として使い、設定キー名などは変わっている可能性がある点にご注意ください。&lt;br&gt;
文中のIP・キー・ホスト名・パスはすべてダミーです。&lt;/p&gt;
&lt;/blockquote&gt;


&lt;h2&gt;
  
  
  1. 目的
&lt;/h2&gt;

&lt;p&gt;自宅でLLM（llama.cpp / Gemma 4 12B）を動かし、VPS上のエージェント（Hermes Agent）から使います。やりたいことは次の通りです。&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;状況&lt;/th&gt;
&lt;th&gt;動作&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;自宅サーバーが起動している&lt;/td&gt;
&lt;td&gt;自宅のLLMを使う&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;自宅サーバーが止まっている&lt;/td&gt;
&lt;td&gt;無料のクラウドモデル（OpenRouter）を使う&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;無料モデルも失敗した&lt;/td&gt;
&lt;td&gt;リトライし、それでもダメならエラーを返す&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;自宅サーバーは電気代節約のため常時起動ではなく、無料クラウドモデルは混雑すると429で弾かれます。両方を組み合わせると、安く、かつ止まりにくくなります。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Telegram / cron
       │
       ▼
┌──────────────┐   Tailscale（暗号化）   ┌────────────────────┐
│ エージェント │ ──────────────────────► │ 自宅LLMサーバー    │
│   (VPS)      │   Bearerトークン必須    │ llama.cpp          │
└──────┬───────┘                         └────────────────────┘
       │ 到達できない／失敗したとき
       ▼
  OpenRouter（openrouter/free）
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  2. 手順1：まずローカルサーバーを守る
&lt;/h2&gt;

&lt;p&gt;ローカルLLMのエンドポイントは、保護しなければ「誰でも使える計算資源」になってしまいます。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) llama.cpp にAPIキーを要求させる&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;llama serve -m &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;model.gguf&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --host &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;待ち受けアドレス&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; --port 8080 \
  --api-key &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;長いランダムなトークン&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;トークンは &lt;code&gt;openssl rand -hex 32&lt;/code&gt; で生成します。systemdで複数行の &lt;code&gt;ExecStart=&lt;/code&gt; を書く場合は、&lt;strong&gt;継続行の行末に &lt;code&gt;\&lt;/code&gt; が必要&lt;/strong&gt;です。抜けると、そのオプションが黙って無視されます。編集後は &lt;code&gt;daemon-reload&lt;/code&gt; と再起動を行ってください。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b) 公開インターネットではなく、閉じたネットワークで繋ぐ&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tailscale（またはWireGuard）を使います。重要なのは &lt;strong&gt;両端にクライアントが必要&lt;/strong&gt; という点です。私は「VPS側にもTailscaleが入っている」と思い込んでいて、実際は未インストールでした。そのせいで接続がずっとタイムアウトしていました。両方のマシンで &lt;code&gt;tailscale status&lt;/code&gt; を確認しましょう。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c) 必要以上に広く待ち受けない&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--host 0.0.0.0&lt;/code&gt; は全インターフェースで待ち受けます。APIキーは最後の砦として有効ですが、できればTailscaleのアドレスに限定するか、ファイアウォールで8080番をTailscaleインターフェースだけに絞ってください。TailscaleのACLで、到達できるノードも制限できます。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;d) 秘密情報をチャットログ・チケット・スクリーンショットに残さない。&lt;/strong&gt; もし公開の場に出てしまったら、トークンを再発行してください。&lt;/p&gt;




&lt;h2&gt;
  
  
  3. 手順2：エージェントにサーバーを登録する（数時間ハマったポイント）
&lt;/h2&gt;

&lt;p&gt;最初に書いた、ありがちな設定はこうです。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;custom&lt;/span&gt;
  &lt;span class="na"&gt;base_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://&amp;lt;tailscale-ip&amp;gt;:8080/v1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;これに加えて環境変数 &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; を設定しました。結果は &lt;strong&gt;&lt;code&gt;401 Invalid API Key&lt;/code&gt;&lt;/strong&gt;。ところが、同じコンテナの中から同じキーで &lt;code&gt;curl&lt;/code&gt; すると &lt;code&gt;200&lt;/code&gt; が返ります。&lt;/p&gt;

&lt;p&gt;認証情報を解決するコードを読んで理由が分かりました。&lt;code&gt;provider: custom&lt;/code&gt; と &lt;code&gt;base_url&lt;/code&gt; だけを書いた場合、汎用の &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; は &lt;strong&gt;ホストが &lt;code&gt;openai.com&lt;/code&gt; / &lt;code&gt;openai.azure.com&lt;/code&gt; のときにしか使われません&lt;/strong&gt;。それ以外のホストでは、キーが黙って捨てられ、&lt;code&gt;"no-key-required"&lt;/code&gt; というプレースホルダーに置き換わります。これは「汎用キーが無関係な接続先に漏れないようにする」ための意図的なガードだと思われます。理屈は妥当ですが、何も警告が出ない点が厄介です。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;対処：&lt;code&gt;key_env&lt;/code&gt; を明示した「名前付き」カスタムプロバイダーとして登録します。&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;モデル名またはパス&amp;gt;&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt;
  &lt;span class="na"&gt;api_mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;chat_completions&lt;/span&gt;

&lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;"custom:home-llm"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;base_url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://&amp;lt;tailscale-ip&amp;gt;:8080/v1&lt;/span&gt;
    &lt;span class="na"&gt;key_env&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;OPENAI_API_KEY&lt;/span&gt;        &lt;span class="c1"&gt;# トークンを入れた環境変数の「名前」
&lt;/span&gt;    &lt;span class="na"&gt;default_model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&amp;lt;モデル名またはパス&amp;gt;&lt;/span&gt;
    &lt;span class="na"&gt;request_timeout_seconds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt;   &lt;span class="c1"&gt;# 後述「未解決」を参照
&lt;/span&gt;
&lt;span class="na"&gt;fallback_model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;openrouter&lt;/span&gt;
  &lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;openrouter/free&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;名前付きプロバイダーは &lt;code&gt;key_env&lt;/code&gt; の明示的な参照でキーを解決するため、ホスト名によるガードを通りません。環境変数名は &lt;code&gt;OPENAI_API_KEY&lt;/code&gt; を使いましたが、コード上は任意の名前で動きそうです（他の名前は未検証です）。&lt;/p&gt;

&lt;p&gt;モデル名にも注意してください。llama.cpp は &lt;code&gt;/v1/models&lt;/code&gt; でモデルの &lt;strong&gt;ファイルパスをそのままIDとして返す&lt;/strong&gt; ので、&lt;code&gt;default&lt;/code&gt; にはそれを書きます。&lt;/p&gt;




&lt;h2&gt;
  
  
  4. 手順3：フォールバックの設定
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fallback_model&lt;/code&gt; には1組のプロバイダー/モデルを書きます（リスト形式でチェーンにもできます）。メインがリトライを使い切って、接続エラーやレート制限になると、自動で切り替わります。実際にログで確認できました。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Max retries (3) exhausted — trying fallback...
Primary model failed — switching to fallback ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;フォールバック先の選び方で、結果が大きく変わりました。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;提供終了したモデルを指定 → &lt;code&gt;HTTP 404: No endpoints found&lt;/code&gt; で全滅&lt;/li&gt;
&lt;li&gt;特定の無料モデルを指定 → 混雑して &lt;code&gt;HTTP 429&lt;/code&gt; で全滅&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;openrouter/free&lt;/code&gt;&lt;/strong&gt; を指定 → OpenRouterのルーターが、その時使える無料モデルから、リクエストに必要な機能（画像入力やツール呼び出しなど）を満たすものを選ぶ。圧倒的に安定&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;注意点として、無料枠のレート制限はアカウント単位なので、&lt;code&gt;openrouter/free&lt;/code&gt; でも429が完全になくなるわけではありません。無料フォールバックも失敗した場合は、リトライ後にエラーになります（永遠に待つわけではありません）。定期ジョブなら、次回の実行で再挑戦されます。&lt;/p&gt;




&lt;h2&gt;
  
  
  5. ハマりどころ一覧
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;症状&lt;/th&gt;
&lt;th&gt;原因&lt;/th&gt;
&lt;th&gt;対処&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;自宅サーバーへの接続がタイムアウトする&lt;/td&gt;
&lt;td&gt;片方にTailscaleが未インストール&lt;/td&gt;
&lt;td&gt;両方に入れてネットワークに参加させる&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;キーが正しいのに &lt;code&gt;401&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;汎用環境変数キーのホスト名ガード&lt;/td&gt;
&lt;td&gt;名前付きプロバイダー + &lt;code&gt;key_env&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;.env&lt;/code&gt; の変更が反映されない&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker restart&lt;/code&gt; は環境変数を再読込しない／composeに &lt;code&gt;env_file&lt;/code&gt; が無い&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;env_file: .env&lt;/code&gt; を追加し、&lt;code&gt;docker compose up -d --force-recreate&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;キーの末尾に &lt;code&gt;\r&lt;/code&gt; が付いて見える&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;docker exec -it&lt;/code&gt; の疑似TTYが出力に付けていた&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-t&lt;/code&gt; を外して再確認。値自体は正常だった&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;systemdのオプションが効かない&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ExecStart=&lt;/code&gt; の継続行の &lt;code&gt;\&lt;/code&gt; 漏れ&lt;/td&gt;
&lt;td&gt;継続行を直す&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;長いプロンプトで毎回タイムアウト&lt;/td&gt;
&lt;td&gt;ドラフトモデル（投機的デコード）の負荷、コンテキスト不足&lt;/td&gt;
&lt;td&gt;ドラフトモデルを外し、&lt;code&gt;-c&lt;/code&gt; を65536に増やした&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  6. 手順4：電源が切れている間、何分も待たされないようにする
&lt;/h2&gt;

&lt;p&gt;自宅マシンの &lt;strong&gt;電源が落ちている&lt;/strong&gt; と、TCPのSYNに誰も応答しないため、「接続拒否」がすぐに返ってきません。1回の試行で約30秒待たされ、リトライや追加のリトライラウンドも重なって、&lt;strong&gt;切り替わるまで約3分&lt;/strong&gt; かかっていました。電源が落ちている間は、メッセージを送るたびにこれが発生します。&lt;/p&gt;

&lt;p&gt;（&lt;code&gt;request_timeout_seconds&lt;/code&gt; はこの待ち時間に効いていないようでした。「未解決」を参照。）&lt;/p&gt;

&lt;p&gt;時刻で固定的に切り替える方式は脆いです。起動後、モデルが使えるようになるまで時間がかかりますし、シャットダウンも（私の環境では消費電力監視で）ぴったりの時刻に行われません。そこで、&lt;strong&gt;時計を信用せず、サーバーの生死を実際に確認して、その結果で切り替えます。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;エージェントのCLIには、実行中に設定を書き換えるコマンドがあり、コンテナの再作成は不要でした。&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt;
hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"&amp;lt;モデル名またはパス&amp;gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2つのキーは &lt;strong&gt;必ずセットで&lt;/strong&gt; 変えます。プロバイダーだけ変えると、ローカルのファイルパスがモデル名として残り、クラウド側に拒否されます。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ウォッチドッグ（cronで3分おきに実行）：&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
&lt;span class="c"&gt;# home-llm-watchdog.sh&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; -u
&lt;span class="nb"&gt;source&lt;/span&gt; /etc/home-llm-watchdog.env   &lt;span class="c"&gt;# chmod 600。HOME_LLM_URL, HOME_LLM_KEY, LOCAL_MODEL を定義&lt;/span&gt;
&lt;span class="nv"&gt;STATE_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/lib/home-llm-watchdog.state
&lt;span class="nv"&gt;LOG_FILE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/log/home-llm-watchdog.log

&lt;span class="nv"&gt;CURRENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dead
&lt;span class="nv"&gt;code&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;curl -s -o /dev/null -w &lt;span class="s1"&gt;'%{http_code}'&lt;/span&gt; --max-time 3 \
  -H &lt;span class="s2"&gt;"Authorization: Bearer ${HOME_LLM_KEY}"&lt;/span&gt; &lt;span class="s2"&gt;"${HOME_LLM_URL}/models"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$code"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"200"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;CURRENT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;alive

&lt;span class="nv"&gt;PREVIOUS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;cat &lt;span class="s2"&gt;"$STATE_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; unknown&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; != &lt;span class="s2"&gt;"$PREVIOUS"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; alive &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"custom:home-llm"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"$LOCAL_MODEL"&lt;/span&gt;    &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
  &lt;span class="k"&gt;else&lt;/span&gt;
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.provider &lt;span class="s2"&gt;"openrouter"&lt;/span&gt;      &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
    docker &lt;span class="nb"&gt;exec&lt;/span&gt; hermes-agent hermes config &lt;span class="nb"&gt;set&lt;/span&gt; model.default  &lt;span class="s2"&gt;"openrouter/free"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt; 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1
  &lt;span class="k"&gt;fi&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"[$(date '+%F %T')] $PREVIOUS -&amp;gt; $CURRENT"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$LOG_FILE"&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$CURRENT"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"$STATE_FILE"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="o"&gt;*/&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;usr&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;local&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;home&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;watchdog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;設計のポイント：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;状態ファイル&lt;/strong&gt;で、状態が変わったときだけ動作します。無駄な書き込みやログの増加を避けられます。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;標準のフォールバックも残す。&lt;/strong&gt; 「起動しているはずの時間帯に落ちた」場合も、ウォッチドッグが検知しますし、検知までの間は従来のリトライ→フォールバックが安全網になります。&lt;/li&gt;
&lt;li&gt;秘密情報はスクリプトに直書きせず、&lt;code&gt;600&lt;/code&gt; のファイルに置きます。（私が最初に実行したスクリプトは直書きでした。真似しないでください。）&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;実機で確認できました。電源タイマーでマシンが起動し、3分以内にエージェントが自動でローカルモデルへ戻りました。&lt;/p&gt;




&lt;h2&gt;
  
  
  7. セキュリティチェックリスト
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] ローカルサーバーにAPIキーを要求させている&lt;/li&gt;
&lt;li&gt;[ ] 閉じたネットワーク（Tailscale/WireGuard）を &lt;strong&gt;両端&lt;/strong&gt; に入れている&lt;/li&gt;
&lt;li&gt;[ ] 待ち受けを &lt;code&gt;0.0.0.0&lt;/code&gt; のままにせず、バインドアドレスかファイアウォールで絞っている&lt;/li&gt;
&lt;li&gt;[ ] TailscaleのACLで、到達できるノードを制限している&lt;/li&gt;
&lt;li&gt;[ ] 秘密情報は &lt;code&gt;600&lt;/code&gt; のファイルに置き、スクリプト・チャットログ・スクショに載せない&lt;/li&gt;
&lt;li&gt;[ ] 露出してしまったトークンは再発行する&lt;/li&gt;
&lt;li&gt;[ ] 誤った接続先に送られうる汎用キーより、プロバイダーごとの明示的な &lt;code&gt;key_env&lt;/code&gt; を使う&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  8. 未解決の点
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;request_timeout_seconds&lt;/code&gt; が実際の待ち時間に効いていないように見えましたが、原因は特定できていません。&lt;code&gt;providers&lt;/code&gt; のキー名を &lt;code&gt;custom:&amp;lt;name&amp;gt;&lt;/code&gt; に合わせても変化はありませんでした。新しいバージョンでは挙動が違う可能性があります。&lt;/li&gt;
&lt;li&gt;補助タスク（チャット以外の処理）で「エンドポイントの認証情報が見つからない」という警告が出ましたが、通常の会話には影響しませんでした。&lt;/li&gt;
&lt;li&gt;どのローカルモデル（GemmaかQwenか）を使うかは、今回の生死切り替えとは別のレイヤーです。今回は生死判定だけに絞りました。&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  まとめ
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;ローカルサーバーを守る（トークン + 閉じたネットワーク）。しかも &lt;strong&gt;両端&lt;/strong&gt; に。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;名前付き&lt;/strong&gt; カスタムプロバイダーとして &lt;code&gt;key_env&lt;/code&gt; 付きで登録する。&lt;/li&gt;
&lt;li&gt;フォールバックには &lt;code&gt;openrouter/free&lt;/code&gt; を使う。&lt;/li&gt;
&lt;li&gt;電源スケジュールがあるなら、生死判定のウォッチドッグで &lt;code&gt;hermes config set&lt;/code&gt; を使って切り替える。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;本記事のHermes Agentの挙動は2026年8月時点で確認したものです。利用の際は最新の公式ドキュメントを確認してください。&lt;/em&gt;&lt;/p&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>cloud</category>
      <category>llm</category>
    </item>
    <item>
      <title>Unlocking the RTX 3060 for Local LLM Inference: Worth It?</title>
      <dc:creator>metis atelier</dc:creator>
      <pubDate>Sat, 21 Nov 2026 14:22:01 +0000</pubDate>
      <link>https://dev.to/metis_atelier_studio/unlocking-the-rtx-3060-for-local-llm-inference-worth-it-3ijn</link>
      <guid>https://dev.to/metis_atelier_studio/unlocking-the-rtx-3060-for-local-llm-inference-worth-it-3ijn</guid>
      <description>&lt;p&gt;Unlocking the RTX 3060 for Local LLM Inference: Worth It? (Spoiler: Not for Q4 GGUF)&lt;br&gt;
I run a home server with two NVIDIA cards: an RTX 3060 (12 GB) for local inference and an RTX 3070 (8 GB) for mining during idle hours. The inference model is Gemma 4 12B QAT (Q4_K_M) served by llama.cpp. When I read about a driver-level FP16 accumulate limit on GeForce Ampere cards, I wanted to see if removing it would speed things up.&lt;br&gt;
What the limit is&lt;br&gt;
According to the reports I read, GeForce cards run FP16 matrix operations with FP32 accumulation at half speed, while professional cards do not. The gpu-unlock project (Linux kernel module, v2.0.0) patches this at boot via a kernel command-line parameter. It lists the RTX 3060 as stable, based on user reports. It is a community project, not an NVIDIA project.&lt;br&gt;
What I did&lt;br&gt;
Confirmed the baseline with llama-bench on the 3060 alone (pp512, tg128) and with server timings for MTP (speculative decoding).&lt;br&gt;
Checked secure boot (disabled), kernel headers, and PCI addresses.&lt;br&gt;
Backed up GRUB config, systemd units, initramfs, and the kernel command line.&lt;br&gt;
Built and installed the DKMS module for three kernels, and set gpu_unlock.targets to the 3060 and 3070 PCI addresses.&lt;br&gt;
Cold-booted (power off, wait, power on).&lt;br&gt;
The kernel log showed both cards unlocked with ret=0, and the NVIDIA driver attached normally.&lt;br&gt;
Results&lt;br&gt;
Metric&lt;br&gt;
Before&lt;br&gt;
After&lt;br&gt;
pp512 (prompt processing, llama-bench)&lt;br&gt;
1276 t/s&lt;br&gt;
1280 t/s&lt;br&gt;
tg128 (generation, llama-bench)&lt;br&gt;
39.5 t/s&lt;br&gt;
39.48 t/s&lt;br&gt;
Prompt processing moved by 0.3%, which is within noise. Generation did not change, which is expected: decode is memory-bandwidth bound.&lt;br&gt;
The reason is simple. GGUF Q4 kernels do not use FP16 accumulation, so the unlocked path is not on the critical path for this model. The project's own docs suggest that the benefit shows up in FP16/BF16 workloads such as unquantized models or diffusion pipelines. I have not verified that yet.&lt;br&gt;
Lessons&lt;br&gt;
Read the right README. I initially mixed up two similar projects, one of which had a diagnostics tool and different GPU support status. Check the repo you actually installed.&lt;br&gt;
Check auto-switching before stopping a service. My own idle-monitor timer started mining on the 3060 as soon as I stopped the inference server. Stop the timer first.&lt;br&gt;
Kernel updates can silently drop drivers. After a kernel bump, the NVIDIA module for the new kernel was missing because the metapackage was held. Confirm modprobe nvidia on the new kernel before rebooting.&lt;br&gt;
Do not run nvidia-smi between building an unlock module and the cold boot. The project documentation warns about this.&lt;br&gt;
Keep a stock GRUB entry. It is the fallback if a boot fails.&lt;br&gt;
Rotate API keys that appear in logs or unit files. I replaced mine after exposing it in a shared output.&lt;br&gt;
Reproducing the baseline&lt;br&gt;
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \&lt;br&gt;
llama-bench -m .gguf -ngl 99 -fa on -ctk q8_0 -ctv q8_0 \&lt;br&gt;
  -b 512 -ub 512 -p 512 -n 128 -r 3 -o md&lt;br&gt;
CUDA_DEVICE_ORDER=PCI_BUS_ID matters. Without it, CUDA device indices can be swapped relative to nvidia-smi.&lt;br&gt;
Status: unlock is installed and stable, but it does not change Q4 inference speed. I will revisit it with FP16/BF16 workloads.&lt;/p&gt;

&lt;p&gt;RTX 3060のunlockはローカルLLMに効くのか？（結論：Q4のGGUFには効かない）&lt;br&gt;
自宅サーバーにNVIDIAのGPUを2枚載せています。RTX 3060（12GB）は推論用、RTX 3070（8GB）は夜間などの空き時間にマイニングに使っています。推論には、llama.cppで動かしたGemma 4 12B QAT（Q4_K_M）を使っています。GeForceのAmpere世代には、FP16行列演算がFP32アキュムレートで半減される制限があると知り、それを外せば速くなるか試しました。&lt;br&gt;
制限の中身&lt;br&gt;
報告によると、GeForceではFP16の行列演算をFP32で積み上げる（アキュムレート）と、速度が半分に抑えられています。一方、プロ向けのカードでは抑えられていません。gpu-unlock（Linuxのカーネルモジュール、v2.0.0）は、起動時にカーネルのコマンドラインで指定すると、この制限を外します。作者のREADMEでは、RTX 3060は「安定（ユーザー報告100件以上）」と書かれています。NVIDIAの公式プロジェクトではなく、個人のコミュニティ製です。&lt;br&gt;
やったこと&lt;br&gt;
unlock前のベースラインを取る。llama-bench で3060単独のpp512とtg128、サーバー経由でMTP（投機的デコード）の速度を測る。&lt;br&gt;
セキュアブートの状態（無効）、カーネルヘッダ、PCIアドレスを確認する。&lt;br&gt;
GRUBの設定、systemdのユニット、initramfs、カーネルのコマンドラインをバックアップする。&lt;br&gt;
3つのカーネル向けにDKMSモジュールをビルドしてインストールし、gpu_unlock.targets に3060と3070のPCIアドレスを指定する。&lt;br&gt;
コールドブート（電源を完全に切ってから入れる）で起動する。&lt;br&gt;
カーネルログには、両方のカードが unlocked（ret=0）と出て、NVIDIAドライバも正常に接続されました。&lt;br&gt;
結果&lt;br&gt;
項目&lt;br&gt;
unlock前&lt;br&gt;
unlock後&lt;br&gt;
pp512（プロンプト処理、llama-bench）&lt;br&gt;
1276 t/s&lt;br&gt;
1280 t/s&lt;br&gt;
tg128（生成、llama-bench）&lt;br&gt;
39.5 t/s&lt;br&gt;
39.48 t/s&lt;br&gt;
プロンプト処理は0.3%の差で、誤差の範囲です。生成は変わりませんでした。生成はメモリ帯域で律速しているので、これは想定どおりです。&lt;br&gt;
理由は単純で、GGUFのQ4カーネルはFP16アキュムレートを使わないからです。つまり、unlockで開く経路が、このモデルの計算では使われていません。プロジェクトの説明でも、効果が出るのはFP16/BF16の処理（非量子化モデルや画像生成など）とされています。そこはまだ検証できていません。&lt;br&gt;
学んだこと&lt;br&gt;
正しいREADMEを読む。 似た名前の2つのプロジェクトを混同してしまいました。実際にインストールしたリポジトリの説明を確認しましょう。&lt;br&gt;
サービスを止める前に、自動切り替えを確認する。 私が推論サーバーを止めた途端、自作のidle-monitorタイマーが3060でマイニングを始めました。先にタイマーを止めるべきでした。&lt;br&gt;
カーネル更新で、ドライバが抜けることがある。 カーネルを上げたら、新しいカーネル用のNVIDIAモジュールが入っていませんでした。メタパッケージがholdされていたのが原因です。再起動の前に、新しいカーネルで modprobe nvidia が通るかを確認しましょう。&lt;br&gt;
unlockモジュールをビルドしてからコールドブートするまで、nvidia-smi は叩かない。 プロジェクトのドキュメントにも注意がありました。&lt;br&gt;
元に戻せるGRUBエントリを残す。 起動に失敗した時の保険になります。&lt;br&gt;
ログや設定ファイルに出たAPIキーは、ローテーションする。 共有した出力に表示してしまったので、差し替えました。&lt;br&gt;
ベースラインの再現&lt;br&gt;
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=0 \&lt;br&gt;
llama-bench -m .gguf -ngl 99 -fa on -ctk q8_0 -ctv q8_0 \&lt;br&gt;
  -b 512 -ub 512 -p 512 -n 128 -r 3 -o md&lt;br&gt;
CUDA_DEVICE_ORDER=PCI_BUS_ID は必須です。これがないと、CUDAの番号と nvidia-smi の番号が入れ替わることがあります。&lt;br&gt;
現状： unlockはインストール済みで、安定して動いています。ただ、Q4の推論速度は変わりません。FP16/BF16の用途（ComfyUIなど）で、改めて検証する予定です。&lt;/p&gt;

</description>
      <category>hardware</category>
      <category>llm</category>
      <category>nvidia</category>
      <category>performance</category>
    </item>
    <item>
      <title>Home Server Recovery and a Weekly Clone: Blog Post and Reproduction Runbook</title>
      <dc:creator>metis atelier</dc:creator>
      <pubDate>Tue, 03 Nov 2026 13:03:50 +0000</pubDate>
      <link>https://dev.to/metis_atelier_studio/home-server-recovery-and-a-weekly-clone-blog-post-and-reproduction-runbook-2jh8</link>
      <guid>https://dev.to/metis_atelier_studio/home-server-recovery-and-a-weekly-clone-blog-post-and-reproduction-runbook-2jh8</guid>
      <description>&lt;p&gt;This file contains:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Part 1: the blog post in English&lt;/li&gt;
&lt;li&gt;Part 2: the blog post in Japanese&lt;/li&gt;
&lt;li&gt;Part 3: a reproduction runbook, with placeholders, that you can follow together with an AI assistant&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Replace every &lt;code&gt;&amp;lt;PLACEHOLDER&amp;gt;&lt;/code&gt; with your own values. Never paste secrets (passwords, API keys, tokens) into a chat.&lt;/p&gt;




&lt;h1&gt;
  
  
  Part 1 (English)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  From a hard disk to an SSD: a bootable spare and a weekly clone on a dual-GPU home server
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Background
&lt;/h3&gt;

&lt;p&gt;This home server has two NVIDIA GPUs in one machine. An RTX 3060 runs local LLM inference: a 12B quantized model served by llama.cpp as a systemd service. An RTX 3070 mines while the machine is idle. The system originally ran from a single 500 GB SATA hard disk, which held the OS, the models, and all service data.&lt;/p&gt;

&lt;p&gt;The project had two goals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; shorten boot time, service start-up time, and model load time, which I expected to be limited by random I/O on the hard disk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Availability:&lt;/strong&gt; stop depending on a single disk, and keep a second disk that can boot the system on its own.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This post covers the migration, the update that exposed the lack of a fallback, and the weekly clone that now keeps the spare current.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Moving the root filesystem from the hard disk to NVMe
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Why the hard disk was expected to be the bottleneck.&lt;/strong&gt; The services on this machine issue many small reads: Python imports, shared libraries, systemd units, and the model file. On a mechanical disk, those reads are bound by seek latency. NVMe removes most of that latency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Target layout.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Disk&lt;/th&gt;
&lt;th&gt;Role after migration&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;NVMe SSD (primary)&lt;/td&gt;
&lt;td&gt;EFI system partition and root filesystem; boots by default&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SATA hard disk (spare)&lt;/td&gt;
&lt;td&gt;Previous system kept as a cold fallback; mounted read-only with &lt;code&gt;nofail&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;What had to change.&lt;/strong&gt; Copying files is not enough to make a system boot. Four things decide it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Every entry in &lt;code&gt;/etc/fstab&lt;/code&gt; must use a UUID, not a device name such as &lt;code&gt;/dev/sda2&lt;/code&gt;. Device names can change when disks are added or removed.&lt;/li&gt;
&lt;li&gt;The EFI partition must live on the new disk, and its boot entry must point at the new loader.&lt;/li&gt;
&lt;li&gt;GRUB's configuration must reference the new root UUID. A copied configuration still points at the old one.&lt;/li&gt;
&lt;li&gt;The old disk must not be mounted writable at boot, so that no boot writes to it by accident.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Verify before rebooting.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;findmnt &lt;span class="nt"&gt;-no&lt;/span&gt; SOURCE /     &lt;span class="c"&gt;# which device is the running root&lt;/span&gt;
lsblk &lt;span class="nt"&gt;-f&lt;/span&gt;                 &lt;span class="c"&gt;# filesystems, labels, and UUIDs&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;blkid               &lt;span class="c"&gt;# confirm each UUID&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; /etc/fstab           &lt;span class="c"&gt;# every entry should be UUID-based, or intentionally absent&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep the old disk attached but unmounted until the first successful boot from the new disk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Results.&lt;/strong&gt; I did not record benchmarks before or after the migration, so this post makes no numeric claims about speed. Subjectively, the improvement was noticeable for only a few days before it became the new normal. The lesson: if performance is the goal, record a baseline before the change, not after it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gotcha.&lt;/strong&gt; The old disk still held a complete, valid system. If its old UUID stayed in &lt;code&gt;fstab&lt;/code&gt; as a writable mount, the next boot would try to mount it. Remove the entry, or mark it &lt;code&gt;nofail&lt;/code&gt; and read-only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the spare mattered
&lt;/h3&gt;

&lt;p&gt;A routine system update went wrong partway through, and the primary disk could no longer be trusted to keep running as before. The spare was not bootable, so I had no working fallback. Closing that gap is the point of this post.&lt;/p&gt;

&lt;h3&gt;
  
  
  Making the spare disk bootable
&lt;/h3&gt;

&lt;p&gt;I wanted a second system that could boot on its own, so I formatted the spare disk's root partition and copied the running system to it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Back up the important configuration files.&lt;/li&gt;
&lt;li&gt;Format the spare root partition with &lt;code&gt;mkfs.ext4&lt;/code&gt;. Formatting avoids old files mixing into the new copy.&lt;/li&gt;
&lt;li&gt;Copy the root filesystem with &lt;code&gt;rsync -aHAXx --numeric-ids&lt;/code&gt;. The &lt;code&gt;-x&lt;/code&gt; flag keeps the EFI partition out of the root copy.&lt;/li&gt;
&lt;li&gt;Rewrite the copy's &lt;code&gt;/etc/fstab&lt;/code&gt; with the spare disk's own UUIDs, and remove any entry for a mount that no longer exists.&lt;/li&gt;
&lt;li&gt;Copy the EFI contents, then reinstall GRUB inside a chroot with &lt;code&gt;--no-nvram&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two gotchas came up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Copied EFI loader stubs hard-code the root UUID.&lt;/strong&gt; The copied &lt;code&gt;grub.cfg&lt;/code&gt; still pointed at the original disk, so selecting the spare disk booted the original system. Every stub the firmware can load must point at the spare disk's root.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;grub-install&lt;/code&gt; writes to NVRAM by default.&lt;/strong&gt; When two disks share a boot label, that can repoint the original disk's firmware entry. &lt;code&gt;--no-nvram&lt;/code&gt; leaves firmware entries alone, and boot order is set in the firmware setup instead.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Booting the spare disk from the firmware boot menu brought up the cloned system. Check every loader path, not just the one you expect.&lt;/p&gt;

&lt;h3&gt;
  
  
  A weekly one-way clone with interlocks
&lt;/h3&gt;

&lt;p&gt;A one-time clone protects against nothing that happens after it. So I added a weekly job that mirrors the running primary root to the spare root:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Schedule:&lt;/strong&gt; a systemd timer every Friday at 02:00, with &lt;code&gt;Persistent=true&lt;/code&gt; so a missed run happens after the next boot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Direction:&lt;/strong&gt; one way only, primary to spare.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interlock 1:&lt;/strong&gt; the job runs only when the machine is booted from the primary disk. If it is booted from the spare, it logs a skip and writes nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interlock 2:&lt;/strong&gt; the destination must match the expected UUID, must not be the running root, and must not already be mounted. Otherwise it refuses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sync:&lt;/strong&gt; &lt;code&gt;rsync -aHAXx --numeric-ids --delete&lt;/code&gt;, excluding virtual filesystems and the EFI partition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fixups:&lt;/strong&gt; after the sync, the spare's fstab is rewritten from a template, and the primary's UUID is replaced in the spare's &lt;code&gt;grub.cfg&lt;/code&gt;. The job fails if any primary UUID remains.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Alerts:&lt;/strong&gt; failures send an email through &lt;code&gt;msmtp&lt;/code&gt; with an app password stored in a root-only config file.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The clone is a single generation. A bad change on the primary reaches the spare on the next run. This is deliberate: the clone guarantees a bootable copy of the latest state, not a history.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lessons
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;A fallback only counts once you have booted it.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cloned boot loaders are not portable.&lt;/strong&gt; Rewrite every UUID the firmware might load, and avoid touching firmware boot entries when installing a bootloader for a clone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Put interlocks in the script.&lt;/strong&gt; A job that writes to a disk should check which disk it is running from, and refuse if the answer is wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the old kernel as a rollback path&lt;/strong&gt; until the new one has run for a while.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A mirror is not a backup.&lt;/strong&gt; If you need history, plan it separately.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Part 2 (日本語)
&lt;/h1&gt;

&lt;h2&gt;
  
  
  ハードディスクからSSDへの換装、起動できる予備ディスク、そして毎週のクローン
&lt;/h2&gt;

&lt;h3&gt;
  
  
  背景
&lt;/h3&gt;

&lt;p&gt;このマシンには NVIDIA GPU を 2 枚載せている。RTX 3060 はローカル LLM の推論に使う。12B の量子化モデルを llama.cpp で動かし、systemd のサービスとして提供している。RTX 3070 は、アイドル時にマイニングへ回す。当初は 500GB の SATA ハードディスク 1 台で動かしていて、OS もモデルもサービスのデータもすべてそこに載っていた。&lt;/p&gt;

&lt;p&gt;目的は二つだった。&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;性能&lt;/strong&gt;：起動時間、サービスの立ち上がり時間、モデルの読み込み時間を短くすること。ハードディスクのランダム I/O がボトルネックになると想定していた。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;可用性&lt;/strong&gt;：ディスク 1 台に依存する状態をやめること。単独で起動できる予備ディスクを持つこと。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;この記事では、換装、その後に起きた更新中の障害とフォールバックの欠如、そして予備を最新に保つための毎週のクローンについて書く。&lt;/p&gt;

&lt;h3&gt;
  
  
  手順 1：ハードディスクから NVMe SSD への root の移行
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;ボトルネックと想定した理由。&lt;/strong&gt; このマシンのサービスは、Python の import、共有ライブラリ、systemd のユニット、モデルファイルなど、小さな読み込みを大量に行う。機械式ディスクでは、これらがシーク時間に左右される。NVMe にすれば、そのレイテンシは大きく減るはずだった。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;換装後の構成。&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ディスク&lt;/th&gt;
&lt;th&gt;換装後の役割&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;NVMe SSD（主）&lt;/td&gt;
&lt;td&gt;EFI パーティションと root。既定で起動する&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SATA ハードディスク（予備）&lt;/td&gt;
&lt;td&gt;旧システムを冷たい予備として保持。読み取り専用・&lt;code&gt;nofail&lt;/code&gt; でマウント&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;変更が必要だったこと。&lt;/strong&gt; ファイルをコピーしただけでは、起動できるかどうかは決まらない。起動を左右するのは次の四点だ。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;/etc/fstab&lt;/code&gt; のすべてのエントリを、&lt;code&gt;/dev/sda2&lt;/code&gt; のようなデバイス名ではなく UUID で指定する。デバイス名は、ディスクを追加・取り外しすると変わることがある。&lt;/li&gt;
&lt;li&gt;EFI パーティションを新しいディスクに置き、起動エントリが新しいローダーを指すようにする。&lt;/li&gt;
&lt;li&gt;GRUB の設定が新しい root の UUID を参照していること。コピーした設定は、古い UUID を指したままになる。&lt;/li&gt;
&lt;li&gt;古いディスクが起動時に書き込み可能な状態でマウントされないようにする。起動のたびに、誤って書き込まれるのを防ぐためだ。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;再起動前の確認。&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;findmnt &lt;span class="nt"&gt;-no&lt;/span&gt; SOURCE /     &lt;span class="c"&gt;# 実行中の root のデバイス&lt;/span&gt;
lsblk &lt;span class="nt"&gt;-f&lt;/span&gt;                 &lt;span class="c"&gt;# ファイルシステム、ラベル、UUID&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;blkid               &lt;span class="c"&gt;# 各 UUID の確認&lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; /etc/fstab           &lt;span class="c"&gt;# すべて UUID 指定、または意図的に無いこと&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;新しいディスクから最初に起動が成功するまで、古いディスクは接続したままにして、マウントはしない。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;結果。&lt;/strong&gt; 換装前後のベンチマークは記録していない。したがって、この記事では速度について数値による主張はしない。体感の改善は数日で薄れ、以降は当たり前の状態になった。性能が目的なら、変更の前にベースラインを記録しておくこと。後から測っても、比べられない。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;注意点。&lt;/strong&gt; 古いディスクには、動く完全なシステムが残っている。古い UUID のまま書き込み可能なマウントとして fstab に残すと、次の起動でそれをマウントしようとする。エントリを削除するか、&lt;code&gt;nofail&lt;/code&gt; と読み取り専用にすること。&lt;/p&gt;

&lt;h3&gt;
  
  
  予備ディスクが必要になった理由
&lt;/h3&gt;

&lt;p&gt;更新の途中で、主ディスクをこれまでどおりには信用できなくなった。予備ディスクは起動できない状態だったから、フォールバックがなかった。このギャップを埋めるのが、この記事の目的だ。&lt;/p&gt;

&lt;h3&gt;
  
  
  予備ディスクを起動できるようにする
&lt;/h3&gt;

&lt;p&gt;単独で起動できる第二のシステムが必要だったので、予備ディスクの root パーティションをフォーマットし、動いているシステムを丸ごとコピーした。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;重要な設定ファイルを退避する。&lt;/li&gt;
&lt;li&gt;予備の root パーティションを &lt;code&gt;mkfs.ext4&lt;/code&gt; でフォーマットする。古いファイルが混ざらないように、新規に作る。&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rsync -aHAXx --numeric-ids&lt;/code&gt; で root をコピーする。&lt;code&gt;-x&lt;/code&gt; によって、EFI パーティションはコピーから外れる。&lt;/li&gt;
&lt;li&gt;コピー先の &lt;code&gt;/etc/fstab&lt;/code&gt; を、予備ディスク自身の UUID で書き直す。存在しないマウントの行は削除する。&lt;/li&gt;
&lt;li&gt;EFI の中身をコピーし、chroot 内で &lt;code&gt;--no-nvram&lt;/code&gt; を付けて GRUB を入れ直す。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ここで二つの落とし穴があった。&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;コピーした EFI の stub は、root の UUID を埋め込んでいる。&lt;/strong&gt; コピーした &lt;code&gt;grub.cfg&lt;/code&gt; が元のディスクを指したままだったため、予備ディスクを選んでも元のシステムが起動した。ファームウェアが読み込みうる stub は、すべて予備ディスクの root を指す必要がある。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;grub-install&lt;/code&gt; は、デフォルトで NVRAM に書き込む。&lt;/strong&gt; 二つのディスクが同じラベルを持つと、元のディスクのファームウェアの起動エントリを書き換えてしまうことがある。&lt;code&gt;--no-nvram&lt;/code&gt; を付ければ、ファームウェアの起動エントリには触らずに済む。起動順はファームウェアの設定画面で決める。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ファームウェアの起動メニューから予備ディスクを選ぶと、クローンしたシステムが立ち上がった。期待している経路だけでなく、すべての読み込み経路を確認すること。ここを省くと、あとで必ず痛い目を見る。&lt;/p&gt;

&lt;h3&gt;
  
  
  インターロック付きの毎週の一方向クローン
&lt;/h3&gt;

&lt;p&gt;一度きりのクローンは、そのあとの変化を守ってくれない。そこで、動いている主ディスクの root を予備に毎週ミラーするジョブを追加した。&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;実行タイミング：&lt;/strong&gt; systemd タイマーで毎週金曜 02:00。&lt;code&gt;Persistent=true&lt;/code&gt; にしてあるので、実行を逃したときは次の起動後に実行される。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;方向：&lt;/strong&gt; 主ディスクから予備への一方通行。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;インターロック 1：&lt;/strong&gt; 主ディスクで起動しているときだけ実行する。予備ディスクで起動しているときは、skip と記録するだけで、何も書き込まない。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;インターロック 2：&lt;/strong&gt; 同期先は、期待する UUID と一致し、実行中の root ではなく、マウントされていないことを確認する。一つでも違えば拒否する。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;同期：&lt;/strong&gt; &lt;code&gt;rsync -aHAXx --numeric-ids --delete&lt;/code&gt;。仮想ファイルシステムと EFI パーティションは除外する。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;後処理：&lt;/strong&gt; 予備側の fstab をテンプレートから書き直し、予備側の &lt;code&gt;grub.cfg&lt;/code&gt; の主ディスクの UUID を置き換える。主ディスクの UUID が残っていたら、失敗として止まる。&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;通知：&lt;/strong&gt; 失敗したら &lt;code&gt;msmtp&lt;/code&gt; でメールを送る。アプリパスワードは root だけが読める設定ファイルに置く。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;クローンは世代を持たないので、主ディスクで壊れた変更は次の同期で予備にも反映される。これは意図的なトレードオフだ。クローンが保証するのは「最新の状態を起動できるコピー」であって、履歴ではない。&lt;/p&gt;

&lt;h3&gt;
  
  
  学んだこと
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;実際に起動したことがあるものだけが、フォールバックになる。&lt;/li&gt;
&lt;li&gt;クローンした起動ローダーは、そのままでは移せない。ファームウェアが読み込みうる UUID をすべて書き換えること。&lt;/li&gt;
&lt;li&gt;インターロックはスクリプトの中に入れる。ディスクに書き込むジョブは、いま自分がどのディスクから動いているかを確認し、おかしければ拒否するべきだ。&lt;/li&gt;
&lt;li&gt;性能は、変更の前に測る。&lt;/li&gt;
&lt;li&gt;古いカーネルは、新しいものが十分に動くまで残す。&lt;/li&gt;
&lt;li&gt;ミラーはバックアップではない。履歴が必要なら、別に計画する。&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Part 3: Reproduction runbook
&lt;/h1&gt;

&lt;p&gt;This runbook is written so that you can follow it with an AI assistant. Give the assistant the whole file, then tell it what your disks are.&lt;/p&gt;

&lt;h2&gt;
  
  
  0. How to work with the AI assistant
&lt;/h2&gt;

&lt;p&gt;Paste this at the start of the session, filling in the placeholders:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;We are setting up a weekly one-way clone from my primary root disk (&amp;lt;PRIMARY_ROOT_DEVICE&amp;gt;, UUID &amp;lt;PRIMARY_ROOT_UUID&amp;gt;) to a spare disk (&amp;lt;SPARE_DEVICE&amp;gt;, root partition UUID &amp;lt;SPARE_ROOT_UUID&amp;gt;, EFI UUID &amp;lt;SPARE_EFI_UUID&amp;gt;).

Rules:
- Use read-only commands first and show me the output.
- Before any command that writes to a disk (mkfs, mount with write, rsync --delete, grub-install), show me the exact command and wait for my "go".
- Back up every file before editing it.
- Never print secrets. I will type passwords myself.
- Verify the target device by UUID and size before every destructive step.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A primary disk that holds the running root (NVMe or SATA).&lt;/li&gt;
&lt;li&gt;A spare disk with at least one partition large enough for the root filesystem, and a separate EFI partition.&lt;/li&gt;
&lt;li&gt;Packages: &lt;code&gt;rsync&lt;/code&gt;, &lt;code&gt;msmtp&lt;/code&gt;, &lt;code&gt;grub-efi-amd64&lt;/code&gt; (or the distro equivalent).&lt;/li&gt;
&lt;li&gt;A working email account with an app password (never put the password in a shell history or a chat).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Identify the disks (read-only)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;findmnt &lt;span class="nt"&gt;-no&lt;/span&gt; SOURCE /
lsblk &lt;span class="nt"&gt;-f&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;blkid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the primary root UUID (&lt;code&gt;&amp;lt;PRIMARY_ROOT_UUID&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;the spare root partition and its UUID after formatting (&lt;code&gt;&amp;lt;SPARE_ROOT_UUID&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;the spare EFI UUID (&lt;code&gt;&amp;lt;SPARE_EFI_UUID&amp;gt;&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Back up before changing anything
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; ~/backup-&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;sudo cp&lt;/span&gt; /etc/fstab ~/backup-&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; +%Y%m%d&lt;span class="si"&gt;)&lt;/span&gt;/fstab.bak
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Format the spare root partition (destructive)
&lt;/h2&gt;

&lt;p&gt;Confirm the device first. The UUID must be the spare's old root, not the primary's.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;umount /path/to/spare/root 2&amp;gt;/dev/null
&lt;span class="nb"&gt;sudo &lt;/span&gt;mkfs.ext4 &lt;span class="nt"&gt;-L&lt;/span&gt; root /dev/&amp;lt;SPARE_ROOT_PARTITION&amp;gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;blkid /dev/&amp;lt;SPARE_ROOT_PARTITION&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Copy the running root
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; /mnt/clone
&lt;span class="nb"&gt;sudo &lt;/span&gt;mount /dev/&amp;lt;SPARE_ROOT_PARTITION&amp;gt; /mnt/clone
&lt;span class="nb"&gt;sudo &lt;/span&gt;rsync &lt;span class="nt"&gt;-aHAXx&lt;/span&gt; &lt;span class="nt"&gt;--numeric-ids&lt;/span&gt; &lt;span class="nt"&gt;--stats&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/proc/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/sys/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/dev/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/run/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/tmp/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/var/tmp/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/mnt/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/media/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/lost+found &lt;span class="nt"&gt;--exclude&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/boot/efi/&lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  / /mnt/clone/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rsync&lt;/code&gt; exit code 24 (files vanished during transfer) is normal on a live system. Any other non-zero code is a failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Rewrite the copy's fstab and EFI loader
&lt;/h2&gt;

&lt;p&gt;Write a fstab for the spare disk, using its own UUIDs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /mnt/clone/etc/fstab &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
UUID=&amp;lt;SPARE_ROOT_UUID&amp;gt;    /          ext4  defaults  0  1
UUID=&amp;lt;SPARE_EFI_UUID&amp;gt;     /boot/efi  vfat  defaults  0  1
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mount the spare EFI, then reinstall GRUB inside a chroot. &lt;code&gt;--no-nvram&lt;/code&gt; is important.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;mount /dev/&amp;lt;SPARE_EFI_PARTITION&amp;gt; /mnt/clone/boot/efi
&lt;span class="k"&gt;for &lt;/span&gt;d &lt;span class="k"&gt;in &lt;/span&gt;dev proc sys run&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;mount &lt;span class="nt"&gt;--bind&lt;/span&gt; /&lt;span class="nv"&gt;$d&lt;/span&gt; /mnt/clone/&lt;span class="nv"&gt;$d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done
&lt;/span&gt;&lt;span class="nb"&gt;sudo chroot&lt;/span&gt; /mnt/clone grub-install &lt;span class="nt"&gt;--target&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;x86_64-efi &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--efi-directory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/boot/efi &lt;span class="nt"&gt;--bootloader-id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;spare &lt;span class="nt"&gt;--no-nvram&lt;/span&gt; &lt;span class="nt"&gt;--recheck&lt;/span&gt;
&lt;span class="nb"&gt;sudo chroot&lt;/span&gt; /mnt/clone update-grub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify that every loader stub points at the spare root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo grep&lt;/span&gt; &lt;span class="nt"&gt;-rH&lt;/span&gt; &lt;span class="s2"&gt;"search.fs_uuid&lt;/span&gt;&lt;span class="se"&gt;\|&lt;/span&gt;&lt;span class="s2"&gt;UUID="&lt;/span&gt; /mnt/clone/boot/efi/EFI/ /mnt/clone/boot/grub/grub.cfg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If any stub still shows the primary UUID, fix it with &lt;code&gt;sed&lt;/code&gt; (after backing it up) and check again.&lt;/p&gt;

&lt;p&gt;Clean up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="k"&gt;for &lt;/span&gt;d &lt;span class="k"&gt;in &lt;/span&gt;run sys proc dev&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;umount /mnt/clone/&lt;span class="nv"&gt;$d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;done
&lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;umount /mnt/clone/boot/efi /mnt/clone
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. Boot test
&lt;/h2&gt;

&lt;p&gt;Shut down. Use the firmware boot menu to choose the spare disk's entry. Do not change the firmware boot order yet. After boot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;findmnt &lt;span class="nt"&gt;-no&lt;/span&gt; SOURCE /     &lt;span class="c"&gt;# should show the spare root partition&lt;/span&gt;
&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. Install the weekly job
&lt;/h2&gt;

&lt;p&gt;Install &lt;code&gt;msmtp&lt;/code&gt; and write &lt;code&gt;/etc/msmtprc&lt;/code&gt; with the app password (edit it yourself):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/msmtprc &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
defaults
auth on
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log

account gmail
host smtp.gmail.com
port 587
from &amp;lt;YOUR_ADDRESS&amp;gt;
user &amp;lt;YOUR_ADDRESS&amp;gt;
password "PASTE_APP_PASSWORD_HERE"

account default : gmail
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;span class="nb"&gt;sudo chown &lt;/span&gt;root:root /etc/msmtprc &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo chmod &lt;/span&gt;600 /etc/msmtprc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write the fstab template for the spare disk:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo install&lt;/span&gt; &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; 755 /etc/clone-job
&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/clone-job/fstab.spare &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
UUID=&amp;lt;SPARE_ROOT_UUID&amp;gt;    /          ext4  defaults  0  1
UUID=&amp;lt;SPARE_EFI_UUID&amp;gt;     /boot/efi  vfat  defaults  0  1
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write the sync script. It runs only from the primary disk and refuses to write anywhere else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /usr/local/sbin/clone-job.sh &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
#!/bin/bash
# One-way weekly clone: primary root -&amp;gt; spare root. Does nothing when booted from the spare.
set -uo pipefail

SRC_UUID=&amp;lt;PRIMARY_ROOT_UUID&amp;gt;
DST_UUID=&amp;lt;SPARE_ROOT_UUID&amp;gt;
FSTAB_TEMPLATE=/etc/clone-job/fstab.spare
MAIL_TO=&amp;lt;YOUR_ADDRESS&amp;gt;
MNT=/mnt/clone-job-dst
LOCK=/run/clone-job.lock
DRYRUN=&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;DRYRUN&lt;/span&gt;&lt;span class="k"&gt;:-&lt;/span&gt;&lt;span class="nv"&gt;0&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;

log() { logger -t clone-job -- "&lt;/span&gt;&lt;span class="nv"&gt;$*&lt;/span&gt;&lt;span class="sh"&gt;"; echo "&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt; &lt;span class="s1"&gt;'+%F %T'&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt; &lt;/span&gt;&lt;span class="nv"&gt;$*&lt;/span&gt;&lt;span class="sh"&gt;"; }
cleanup() { mountpoint -q "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;" &amp;amp;&amp;amp; umount "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;"; }
trap cleanup EXIT

fail() {
  log "FAILED: &lt;/span&gt;&lt;span class="nv"&gt;$*&lt;/span&gt;&lt;span class="sh"&gt;"
  printf 'To: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;Subject: [clone-job] FAILED on %s&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="sh"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="sh"&gt;' &lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="sh"&gt;
    "&lt;/span&gt;&lt;span class="nv"&gt;$MAIL_TO&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;hostname&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="nv"&gt;$*&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;date&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;" | msmtp "&lt;/span&gt;&lt;span class="nv"&gt;$MAIL_TO&lt;/span&gt;&lt;span class="sh"&gt;" &lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="sh"&gt;
    || log "could not send failure mail"
  exit 1
}

exec 9&amp;gt;"&lt;/span&gt;&lt;span class="nv"&gt;$LOCK&lt;/span&gt;&lt;span class="sh"&gt;"
flock -n 9 || { log "skip: another run is in progress"; exit 0; }

# Interlock 1: only run when booted from the primary disk
ROOT_SRC=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;findmnt &lt;span class="nt"&gt;-no&lt;/span&gt; SOURCE /&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
ROOT_UUID=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;blkid &lt;span class="nt"&gt;-s&lt;/span&gt; UUID &lt;span class="nt"&gt;-o&lt;/span&gt; value &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ROOT_SRC&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
if [ "&lt;/span&gt;&lt;span class="nv"&gt;$ROOT_UUID&lt;/span&gt;&lt;span class="sh"&gt;" != "&lt;/span&gt;&lt;span class="nv"&gt;$SRC_UUID&lt;/span&gt;&lt;span class="sh"&gt;" ]; then
  log "skip: booted from &lt;/span&gt;&lt;span class="nv"&gt;$ROOT_SRC&lt;/span&gt;&lt;span class="sh"&gt;, not the primary disk. Nothing done."
  exit 0
fi

# Interlock 2: destination must be the spare root, not the running root, and not mounted
DST_PART=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;readlink&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s2"&gt;"/dev/disk/by-uuid/&lt;/span&gt;&lt;span class="nv"&gt;$DST_UUID&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; 2&amp;gt;/dev/null &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
[ -n "&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="sh"&gt;" ] &amp;amp;&amp;amp; [ -b "&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="sh"&gt;" ] || fail "spare root (&lt;/span&gt;&lt;span class="nv"&gt;$DST_UUID&lt;/span&gt;&lt;span class="sh"&gt;) not found"
[ "&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="sh"&gt;" != "&lt;/span&gt;&lt;span class="nv"&gt;$ROOT_SRC&lt;/span&gt;&lt;span class="sh"&gt;" ] || fail "destination equals the running root; refusing"
[ "&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;blkid &lt;span class="nt"&gt;-s&lt;/span&gt; UUID &lt;span class="nt"&gt;-o&lt;/span&gt; value &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;" = "&lt;/span&gt;&lt;span class="nv"&gt;$DST_UUID&lt;/span&gt;&lt;span class="sh"&gt;" ] || fail "destination UUID mismatch"
findmnt -S "&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="sh"&gt;" &amp;gt;/dev/null &amp;amp;&amp;amp; fail "destination is already mounted; refusing"
[ -f "&lt;/span&gt;&lt;span class="nv"&gt;$FSTAB_TEMPLATE&lt;/span&gt;&lt;span class="sh"&gt;" ] || fail "fstab template missing"

mkdir -p "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;"
mount -o noatime "&lt;/span&gt;&lt;span class="nv"&gt;$DST_PART&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;" || fail "mount failed"

RSYNC_OPTS=(-aHAXx --numeric-ids --delete --stats)
[ "&lt;/span&gt;&lt;span class="nv"&gt;$DRYRUN&lt;/span&gt;&lt;span class="sh"&gt;" = 1 ] &amp;amp;&amp;amp; RSYNC_OPTS+=(-n)
EXCLUDES=(
  --exclude=/proc/* --exclude=/sys/* --exclude=/dev/* --exclude=/run/*
  --exclude=/tmp/* --exclude=/var/tmp/* --exclude=/mnt/* --exclude=/media/*
  --exclude=/lost+found --exclude=/srv/backup-hdd/* --exclude=/boot/efi/*
)
rsync "&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;RSYNC_OPTS&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;EXCLUDES&lt;/span&gt;&lt;span class="p"&gt;[@]&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;" / "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;"/
rc=&lt;/span&gt;&lt;span class="nv"&gt;$?&lt;/span&gt;&lt;span class="sh"&gt;
if [ "&lt;/span&gt;&lt;span class="nv"&gt;$rc&lt;/span&gt;&lt;span class="sh"&gt;" -ne 0 ] &amp;amp;&amp;amp; [ "&lt;/span&gt;&lt;span class="nv"&gt;$rc&lt;/span&gt;&lt;span class="sh"&gt;" -ne 24 ]; then fail "rsync exited with code &lt;/span&gt;&lt;span class="nv"&gt;$rc&lt;/span&gt;&lt;span class="sh"&gt;"; fi

if [ "&lt;/span&gt;&lt;span class="nv"&gt;$DRYRUN&lt;/span&gt;&lt;span class="sh"&gt;" = 1 ]; then log "dry run finished (nothing written)"; exit 0; fi

# Spare-specific fixups
install -m 644 "&lt;/span&gt;&lt;span class="nv"&gt;$FSTAB_TEMPLATE&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;/etc/fstab" || fail "could not write fstab"
sed -i "s/&lt;/span&gt;&lt;span class="nv"&gt;$SRC_UUID&lt;/span&gt;&lt;span class="sh"&gt;/&lt;/span&gt;&lt;span class="nv"&gt;$DST_UUID&lt;/span&gt;&lt;span class="sh"&gt;/g" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;/boot/grub/grub.cfg" || fail "could not edit grub.cfg"
grep -q "&lt;/span&gt;&lt;span class="nv"&gt;$SRC_UUID&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;/boot/grub/grub.cfg" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;/etc/fstab" &amp;amp;&amp;amp; fail "primary UUID still present on spare"
grep -q "UUID=&lt;/span&gt;&lt;span class="nv"&gt;$DST_UUID&lt;/span&gt;&lt;span class="sh"&gt;" "&lt;/span&gt;&lt;span class="nv"&gt;$MNT&lt;/span&gt;&lt;span class="sh"&gt;/etc/fstab" || fail "fstab root check failed"

sync
log "OK: primary -&amp;gt; spare clone finished (rsync rc=&lt;/span&gt;&lt;span class="nv"&gt;$rc&lt;/span&gt;&lt;span class="sh"&gt;)"
exit 0
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;span class="nb"&gt;sudo chown &lt;/span&gt;root:root /usr/local/sbin/clone-job.sh &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo chmod &lt;/span&gt;755 /usr/local/sbin/clone-job.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the systemd service and timer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/systemd/system/clone-job.service &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
[Unit]
Description=Weekly one-way clone primary root -&amp;gt; spare root
ConditionPathExists=/usr/local/sbin/clone-job.sh

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/clone-job.sh
Nice=10
TimeoutStartSec=2h
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/systemd/system/clone-job.timer &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;'
[Unit]
Description=Run clone-job every Friday 02:00

[Timer]
OnCalendar=Fri *-*-* 02:00:00
Persistent=true

[Install]
WantedBy=timers.target
&lt;/span&gt;&lt;span class="no"&gt;EOF

&lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl daemon-reload &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable&lt;/span&gt; &lt;span class="nt"&gt;--now&lt;/span&gt; clone-job.timer
systemctl list-timers clone-job.timer &lt;span class="nt"&gt;--no-pager&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  9. Test in order
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# (a) mail&lt;/span&gt;
&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'To: &amp;lt;YOUR_ADDRESS&amp;gt;\nSubject: [test] mail\n\nhello\n'&lt;/span&gt; | &lt;span class="nb"&gt;sudo &lt;/span&gt;msmtp &amp;lt;YOUR_ADDRESS&amp;gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo &lt;/span&gt;sent

&lt;span class="c"&gt;# (b) dry run, writes nothing&lt;/span&gt;
&lt;span class="nb"&gt;sudo env &lt;/span&gt;&lt;span class="nv"&gt;DRYRUN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 /usr/local/sbin/clone-job.sh 2&amp;gt;&amp;amp;1 | &lt;span class="nb"&gt;tail&lt;/span&gt; &lt;span class="nt"&gt;-15&lt;/span&gt;

&lt;span class="c"&gt;# (c) real run, from the primary disk&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start clone-job.service
journalctl &lt;span class="nt"&gt;-u&lt;/span&gt; clone-job.service &lt;span class="nt"&gt;-n&lt;/span&gt; 40 &lt;span class="nt"&gt;--no-pager&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then boot from the spare disk and run the job once more. The expected output is a &lt;code&gt;skip&lt;/code&gt; line, and nothing should be written:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl start clone-job.service
journalctl &lt;span class="nt"&gt;-u&lt;/span&gt; clone-job.service &lt;span class="nt"&gt;-n&lt;/span&gt; 10 &lt;span class="nt"&gt;--no-pager&lt;/span&gt;
findmnt | &lt;span class="nb"&gt;grep &lt;/span&gt;clone-job &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"no clone mount (good)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  10. Rollback
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;To stop the weekly job: &lt;code&gt;sudo systemctl disable --now clone-job.timer&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To remove everything: delete the files listed above, then &lt;code&gt;sudo systemctl daemon-reload&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;To return to the original disk: select it in the firmware boot menu. Do not run &lt;code&gt;grub-install&lt;/code&gt; on the primary unless you have a backup of its EFI partition.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Prepared as a reproduction guide. Test every step on a non-production machine first if you can.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>automation</category>
      <category>devops</category>
      <category>hardware</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
