Claude Code に QPKG の作成を依頼する:Public 共有フォルダーに Hello world を書き込むパッケージと crontab エディター
Claude Code は、NAS 上の Bucket & Code Server IDE の環境で動作します。パネル、エディターのターミナル、claude.ai/code、スマートフォンアプリのいずれかからセッションを開始します。パッケージに何をさせたいかを、普段の言葉で伝えます。このガイドでは、実際の依頼を 2 つ紹介します。HelloPublic は Public 共有フォルダー内のファイルに「Hello world」と書き込みます。CronEdit は QNAP のルールに従って、NAS の crontab に自分のスケジュール行を追加します。
- セッションプロジェクトのフォルダーで開始
- プロンプトパッケージの動作を指定
- レビューコードを読んでテスト
- ビルドbvs qbuild
- インストールインストール後に動作を確認
画像は、Bucket & Code Server IDE 1.0.0-102 をアカウント ola で使用したときのものです。環境には Claude Code をインストール済みです。Claude Code には誰もサインインしていないため、セッション自体はテキストで示します。プロンプトと回答は、実際に Claude Code を実行したときのものです。
01始める前に
- 新しいプロジェクトでテンプレートQPKG パッケージを選び、プロジェクトを作成します。プロジェクトにはスキル
.claude/skills/qnap-devとファイルAGENTS.mdが含まれます。Claude Code はスキルを読みます。その内容に従って、BusyBox のsh、/etc/config、システムログを使うコードを NAS 向けに書きます。 - 環境に Claude Code をインストールします。ツールチェーン → 対象の環境 → AI ツール → Claude Code → インストールの順に選びます。続けて、同じダイアログの Claude にサインインから自分のアカウントでサインインします。
- ロールが developer または admin のアカウントを用意します。NAS へのパッケージのインストールは管理者が行います。
02セッションを開始する
どの方法でも、環境内の同じ Claude Code が起動します。プロジェクトのフォルダーで起動します。Claude Code はそこでプロジェクトのスキルを読み込みます。
- パネルから AI ツールの Claude Code タブを開きます。フォルダーでプロジェクトを選び、新しいセッションを開始を押します。セッションは環境の隔離領域で動作します。ページを閉じても、セッションは続きます。ターミナルから再び開けます。

- エディターのターミナルから ボタンの下にあるエディターのターミナルからも起動できますリンクを押します。エディターのターミナルでセッションが開きます。エディターのタブを閉じると、そのセッションは終了します。
- claude.ai/code または Claude アプリから サインインすると、同じタブに Remote Control が表示されます。「この環境のセッションは claude.ai/code と Claude アプリから開始・操作できます。」これを有効にし、claude.ai/code または Claude アプリでこの環境を選びます。
- スマートフォンから パネルの電話アプリページでペアリングした Bucket & Code のスマートフォンアプリを開きます。AI タブで環境、フォルダー、ツールを選びます。次に新しい会話を開始を押します。
03HelloPublic の作成を依頼する
新しいプロジェクト HelloPublic のフォルダーで入力したプロンプトの原文です。
This folder is a QPKG project that Bucket & Code's New project made. Make the package HelloPublic: when it starts, it writes the line "Hello world" into the text file hello.txt in the Public shared folder. - Take the Public folder's path from QTS: getcfg Public path -f /etc/config/smb.conf. Never a fixed volume such as /share/CACHEDEV1_DATA. - If the NAS has no Public share, the start says so in the NAS's System Logs and fails. - Stop and uninstall leave hello.txt where it is: it is the user's file then. - Follow the qnap-dev skill in .claude/skills. Do not build or install anything: I build it with bvs qbuild and install it myself. At the end, tell me which files you changed, how to build it and how to check it on the NAS.
Claude Code が書いた起動処理は shared/HelloPublic.sh にあります。スクリプトは Public 共有フォルダーのパスを QTS から取得します。起動のたびにファイルを上書きするため、2 回起動してもファイルの内容は 1 行のままです。
service_start() {
local dir file
dir=$("$QPKG_SYS/sbin/getcfg" Public path -f "$QPKG_SYS/etc/config/smb.conf")
if [ -z "$dir" ]; then
qpkg_log error "this NAS has no Public shared folder (no path for [Public] in /etc/config/smb.conf); hello.txt is not written"
return 1
fi
qpkg_sys "$dir"
if [ ! -d "$QPKG_P" ]; then
qpkg_log error "the Public shared folder $dir does not exist or its volume is not mounted; hello.txt is not written"
return 1
fi
file=$QPKG_P/hello.txt
…
if ! echo "Hello world" >"$file"; then
qpkg_log error "cannot write $dir/hello.txt"
return 1
fi
# the user's file: not only root may change it (root writes it, with the umask 022 qpkg_init set)
chmod 666 "$file"
qpkg_log info "wrote \"Hello world\" into $dir/hello.txt"
}
}
回答の抜粋です。このセッションでは、Claude Code はファイルを編集し、構文を確認できました。スクリプトは実行できなかったと、Claude Code 自身が回答で述べています。
HelloPublic now writes "Hello world" into `hello.txt` in the Public shared folder when it starts. I haven't run it: the syntax check (`sh -n`) passes, but the test against a fake NAS folder needed your approval and wasn't run. Nothing is built, installed or committed.
## Files changed
- **`shared/HelloPublic.sh`** (the start script):
- **Start** gets the Public folder's path from `/sbin/getcfg Public path -f /etc/config/smb.conf`, with no fixed volume anywhere. It then overwrites `hello.txt` there with the one line, so starting twice still leaves one line. It sets the file to mode 666 so other users can change it, not only root.
- **Start fails** (exit 1, with an error in System Logs) when:
- the NAS has no Public share;
- Public's folder doesn't exist, for example because its volume isn't mounted. The script never creates that folder, because a folder made then would land in RAM;
- `hello.txt` is a link or not a regular file. Public is open to every user, so this stops anyone tricking the root-run start into overwriting some other file.
- **Stop and uninstall** don't touch `hello.txt`.
…
## Check it on the NAS
1. Install it: `qpkg_cli -m /share/<folder>/HelloPublic_0.1.0_<arch>.qpkg --keep`, or use App Center's manual install. The start runs as part of the install.
…
04CronEdit の作成を依頼する
プロンプトには、QNAP が定める cron のルールを 1 つずつ記載します。後から、各ルールに対応する処理をコードで確認できます。
This folder is a QPKG project that Bucket & Code's New project made. Make the package CronEdit, a small crontab editor for a QNAP NAS that follows QNAP's rules for cron: 1. The app keeps its own schedule lines in a file of its own in the package's folder, one cron line per line. A command, cronedit, manages them: cronedit add "<five time fields> <command>", cronedit list, cronedit remove <number>. 2. The lines go into /etc/config/crontab, the file that survives a restart of the NAS. Never crontab -e, never the files under /tmp/cron or /var/spool/cron. 3. Every line the app writes into /etc/config/crontab ends with the app's own tag, so its lines can always be told from the others. 4. At every start, the service script checks each of the app's lines and adds it only when /etc/config/crontab does not have it yet. A second start adds nothing: never a duplicate. 5. On stop and on uninstall it removes its own tagged lines, and only those (cleanup), including lines that are no longer in its own file. 6. After every change of /etc/config/crontab it reloads cron the QNAP way: crontab /etc/config/crontab && /etc/init.d/crond.sh restart. When nothing changed, it does not reload. 7. It never changes, moves or removes a line it did not write. 8. cronedit add and cronedit remove change the app's file and, while the app is started, bring /etc/config/crontab in line at once, by the same rules. Follow the qnap-dev skill in .claude/skills (the NAS has BusyBox and no bash; cron runs commands with a short PATH). Do not build or install anything: I build it with bvs qbuild and install it myself. At the end, tell me which files you changed, how to build it and how to check it on the NAS.
回答の抜粋です。
I've written CronEdit, but **I haven't run it.** Running scripts needed an approval this session didn't give, so the only check was a syntax check with `sh -n`, which passed for both scripts. Everything else comes from reading the code. A test script for a fake NAS file tree is in `build/t/run.sh` (git ignores `build/`), so you can run it with `sh build/t/run.sh`. Nothing is committed. ## Files changed - **`shared/bin/cronedit`** (new, POSIX `sh` for BusyBox): the `add`, `list` and `remove <number>` commands, plus `sync`, `cleanup` and `check`, which the service script calls. - **Its own file:** the lines live in `<install folder>/cronedit.tab`. The package doesn't ship that file, so an upgrade can't overwrite it. - **Tag:** every line it writes into `/etc/config/crontab` ends in `# managed-by:CronEdit`. It rejects a line that is badly formed, starts with `@reboot` or `#`, contains the tag, or ends in `\`. It also rejects a line that is already in its file. - **Other lines are safe:** before it replaces `/etc/config/crontab`, it checks that every untagged line is still there, unchanged and in the same order. If not, it leaves the file alone. It keeps a backup while writing and puts it back if the write fails. - **Reload:** it runs `crontab /etc/config/crontab && /etc/init.d/crond.sh restart` only when lines were actually added or removed. - **Started or stopped:** `add` and `remove` update `/etc/config/crontab` straight away only while the package is started. While it's stopped, the change waits for the next start. - A command that doesn't start with `/` gets a note that cron's PATH is short. - **`shared/CronEdit.sh`**: the service script, written by hand on top of `lib/bvs-qpkg.sh`. - **start:** runs `sync`, which adds only the missing lines, so a second start adds nothing. It also removes tagged lines that are no longer in `cronedit.tab`, and links `/usr/bin/cronedit`. - **stop and remove (uninstall):** run `cleanup`, which removes every tagged line and only those. - **status:** runs `check`. - **Switched-off package:** if a start is refused because the package is off, it still removes the tagged lines first, in case a power cut skipped the stop. … **One thing for you:** deleting the template placeholder `shared/bin/CronEdit` needed an approval I didn't get, so it's still there. It does no harm, but you can remove it with `git rm shared/bin/CronEdit`.
05crontab のルールを実装するコード
CronEdit は、パッケージのフォルダーにある cronedit.tab に自分のスケジュール行を保存します。bin/cronedit は、その行を NAS の再起動後も残る /etc/config/crontab に書き込みます。書き込む行の末尾にはタグを付けます。変更するのは、末尾にそのタグがある行だけです。
TAG_WORD='managed-by:CronEdit'
TAG="# $TAG_WORD" # the end of every line CronEdit writes; a user's line may not hold TAG_WORD
SYS=${BVS_QPKG_SYSROOT-}
CONF=$SYS/etc/config/qpkg.conf
CRONTAB=$SYS/etc/config/crontab
awk を 1 回実行して新しいファイルを作ります。タグのない行は元の位置に残します。タグ付きの行のうち、パッケージに必要な行は、それぞれ最初に現れたものだけを残します。重複した行や cronedit.tab にもうない行は取り除きます。必要な行がファイルになければ、末尾に追加します。
changes=$(awk -v tag="$TAG" -v out="$new" "$AWK_OURS"'
FILENAME == ARGV[1] { if (!($0 in want)) { want[$0] = 1; order[++n] = $0 }; next }
ours($0) {
if (($0 in want) && !($0 in seen)) { seen[$0] = 1; print > out } else { dropped++ }
next
}
{ print > out }
END {
for (i = 1; i <= n; i++) if (!(order[i] in seen)) { print order[i] > out; added++ }
print dropped + 0, added + 0
}' "$want" "$old")
行の追加も削除もない場合は、ファイルを変更せず、cron も再起動しません。
set -- $changes
DROPPED=${1:-0}
ADDED=${2:-0}
if [ "$DROPPED" = 0 ] && [ "$ADDED" = 0 ]; then
rm -f "$new"
return 0
fi
書き込む前に awk をもう一度実行し、タグのない行がすべて同じ内容と順序で残っていることを確認します。確認に失敗した場合はファイルを変更しません。書き込んだ後は、QNAP の手順で cron を再読み込みします。
reload() {
# on the NAS: /usr/bin/crontab /etc/config/crontab && /etc/init.d/crond.sh restart
if "$SYS/usr/bin/crontab" "$CRONTAB" && "$SYS/etc/init.d/crond.sh" restart >/dev/null 2>&1; then
return 0
fi
say "the reload of cron failed (crontab /etc/config/crontab && /etc/init.d/crond.sh restart)"
event 2 "the reload of cron failed (crontab /etc/config/crontab && /etc/init.d/crond.sh restart)"
return 1
}
サービススクリプトがこの処理を呼び出します。start はファイルを同期します。stop と remove(アンインストール)はタグ付きの行をすべて削除します。App Center のスイッチがオフで起動できないときも、タグ付きの行を削除します。停電で停止処理が実行されなかった場合に備えるためです。
service_start() {
# the command on PATH; a file of another at /usr/bin/cronedit is left alone, and bin/cronedit still works
qpkg_link "$CRONEDIT" /usr/bin/cronedit || :
"$CRONEDIT" sync
}
service_stop() {
"$CRONEDIT" cleanup
qpkg_unlink /usr/bin/cronedit "$CRONEDIT"
return 0
}
…
service_remove() {
"$CRONEDIT" cleanup
qpkg_unlink /usr/bin/cronedit "$CRONEDIT"
return 0
}
# A switched-off package's start is refused by qpkg_start; its tagged lines must not run either (a power cut skips
# the stop), so they go first.
if [ "${1-}" = start ] && ! qpkg_enabled; then
"$CRONEDIT" cleanup
fi
パッケージの稼働中は、cronedit add と cronedit remove が cronedit.tab を変更し、その内容をすぐに /etc/config/crontab に反映します。停止中の変更は、次回の起動時に反映します。
06Claude Code が書いたコードをレビューする
差分を読みます。次に、セッションで実行できなかった処理を実行します。CronEdit では、NAS のディレクトリ構成を模した環境で、付属のテスト sh build/t/run.sh を実行します。このテストは合格しました。レビューで加えた変更は次のとおりです。
- CronEdit では、テンプレートの
bin/CronEditを削除しました。 このファイルを実行する処理はなく、bin/croneditとの違いは大文字と小文字だけです。Windows や macOS では片方がもう片方を上書きします。Claude Code はこの点に気づいていましたが、削除には承認が必要でした。README の該当する 2 行はbin/croneditの説明に更新しました。 - HelloPublic のコードに変更はありません。
- インストール方法 回答には
qpkg_cli -m … --keepでインストールするように書かれています。QTS 6.1 で署名のないパッケージにこのコマンドを使ってもインストールされず、qpkg_cli -sは「notfound」を返します。インストール方法はセクション 08 で説明します。
07パッケージをビルドする
プロジェクトのページでビルドを押すと、プロジェクトのビルド環境で bvs qbuild が実行されます。ターミナルを使う場合は、プロジェクトのフォルダーで同じコマンドを実行します。
bvs qbuild ls build/ CronEdit_0.1.0_x86_64.qpkg CronEdit_0.1.0_x86_64.qpkg.md5
プロジェクトにコード署名証明書がない場合、パッケージには署名が付かず、テストデプロイは無効のままです。「コード署名済みのパッケージが必要です」と表示されます。署名済みのビルドは テストデプロイでインストールできます。署名方法は App Center ガイドを参照してください。
08インストールして動作を確認する
自分の NAS で署名のないビルドを試すには、ファイルを共有フォルダーにコピーします。SSH で NAS に admin としてサインインし、ファイルを実行します。QTS がパッケージをインストールして起動します。
# admin として SSH 接続し、ファイルがある共有フォルダーで実行
cd /share/Public/packages
sh ./CronEdit_0.1.0_x86_64.qpkg
…
Link service start/stop script: CronEdit.sh
Set QPKG information in /etc/config/qpkg.conf
CronEdit: started
- この方法でインストールすると、
/etc/config/qpkg.confにそのパッケージのStatus行は入りません。App Center は、パッケージをインストールするときにStatus = completeを書き込みます。 - これらのパッケージを使うために、有効なデジタル署名のないアプリを許可する App Center の設定は有効にしないでください。パッケージに署名してください。
- パッケージを削除するには、App Center または
qpkg_cli -R CronEditを使います。
HelloPublic の起動後の確認結果です。
P=$(getcfg Public path -f /etc/config/smb.conf) cat "$P/hello.txt" Hello world
稼働中の CronEdit に 2 行を追加したときの結果です。
cronedit add "*/15 * * * * /bin/true" cronedit: added: */15 * * * * /bin/true cronedit: /etc/config/crontab: 1 line(s) added, 0 removed cronedit add "* * * * * /bin/date -u >> /share/Public/cronedit-demo.txt" cronedit: added: * * * * * /bin/date -u >> /share/Public/cronedit-demo.txt cronedit: /etc/config/crontab: 1 line(s) added, 0 removed cronedit list CronEdit is started; its lines (/share/CACHEDEV1_DATA/.qpkg/CronEdit/cronedit.tab): 1 */15 * * * * /bin/true 2 * * * * * /bin/date -u >> /share/Public/cronedit-demo.txt grep -n 'managed-by:CronEdit' /etc/config/crontab 57:*/15 * * * * /bin/true # managed-by:CronEdit 58:* * * * * /bin/date -u >> /share/Public/cronedit-demo.txt # managed-by:CronEdit /etc/init.d/CronEdit.sh status CronEdit: Enable = TRUE CronEdit: started CronEdit: every line of /share/CACHEDEV1_DATA/.qpkg/CronEdit/cronedit.tab is in /etc/config/crontab, once: yes
09テスト結果
QTS 6.1 を搭載した NAS で、インストール前に /etc/config/crontab(58 行)のコピーを保存しました。各手順の後で、ファイルをそのコピーと比較しました。
| 手順 | 実行後の /etc/config/crontab | cron の再起動 |
|---|---|---|
| インストール(登録行なし) | 保存したコピーと同じ | なし |
稼働中に cronedit add を 2 回実行 | タグ付きの行が 2 行増加 | 操作のたびに再起動 |
| 停止 | 保存したコピーとバイト単位で一致 | あり |
| 起動 | 2 行が復元 | あり |
| 再度起動 | 変更なし、重複なし | なし |
停電後など、停止処理が実行されていない状態で cronedit.tab から 1 行を削除してから起動 | 該当する行が削除され、もう 1 行は残る | あり |
稼働中に cronedit add、続けて cronedit remove を実行 | 行を追加し、その後に削除 | 操作のたびに再起動 |
qpkg_cli -R CronEdit | 保存したコピーとバイト単位で一致し、crontab -l の出力も同じ | あり |
- 各手順の後で、既存の 58 行は、内容も順序も保存したコピーと一致しました。
* * * * * /bin/date -u >> /share/Public/cronedit-demo.txtのコマンドが、1 分以内に最初のタイムスタンプを書き込みました。この結果から、cron がタグ付きの行を実行することが分かります。- QTS の
crond.sh startは、crontabがファイルを読み込んだ後に、QTS 自身のvs_refreshとbackup_conf.shの行をファイルの末尾へ移動します。そのため、CronEdit の行が登録されている間は、crontab -lに同じ行がファイルとは異なる順序で表示されることがあります。 - HelloPublic を起動すると、
hello.txtにHello worldが書き込まれました。2 回目の起動後も 1 行のままでした。停止やアンインストールを行っても、ファイルは Public 共有フォルダーに残りました。
10ソースをダウンロードする
ai-qpkg-examples.zip(170 KB)には、レビュー後の 2 つのプロジェクト、2 つのプロンプト、README が含まれます。README には、Claude Code が書いた内容とレビューで変更した内容を記載しています。いずれかのプロジェクトを、プロジェクトの保存先フォルダーにコピーします。bvs qbuild でビルドします。