Chiedi a Claude Code un QPKG: Hello world nella cartella condivisa Public e un editor di crontab
Claude Code funziona sul tuo NAS in un ambiente di Bucket & Code Server IDE. Avvia una sessione dal pannello, dal terminale dell’editor, da claude.ai/code o dall’app per il telefono. Descrivi il pacchetto con parole semplici. La guida mostra due richieste reali. HelloPublic scrive «Hello world» in un file nella cartella condivisa Public. CronEdit aggiunge le proprie righe al crontab del NAS secondo le regole di QNAP.
- Sessionenella cartella del progetto
- Promptcosa deve fare il pacchetto
- Revisioneleggi e prova il codice
- Compilazionebvs qbuild
- Installazionee verifica
Le schermate provengono da Bucket & Code Server IDE 1.0.0-102 con l’account ola. Claude Code è installato nell’ambiente e nessuno ha effettuato l’accesso, quindi la sessione è riportata come testo. I prompt e le risposte provengono da sessioni reali di Claude Code.
01Prima di iniziare
- Un progetto creato con Nuovo progetto e il modello Pacchetto QPKG. Contiene la skill
.claude/skills/qnap-deveAGENTS.md. Claude Code legge la skill e scrive codice per il NAS:shdi BusyBox,/etc/confige registri di sistema. - Claude Code in un ambiente. Vai a Toolchain → ambiente → Strumenti IA → Claude Code → Installa. Nella stessa finestra scegli Accedi a Claude e usa il tuo account.
- Un account con ruolo developer o admin. Il pacchetto sul NAS lo installa un amministratore del NAS.
02Avvia una sessione
Tutti i metodi qui sotto avviano lo stesso Claude Code nell’ambiente. Avvialo nella cartella del progetto, così legge la skill del progetto.
- Dal pannello. Nella scheda Claude Code di Strumenti IA, scegli il progetto in Cartella e premi Avvia una nuova sessione. La sessione gira nell’ambiente isolato e resta attiva quando chiudi la pagina. La ritrovi in Terminale.

- Nel terminale dell’editor. Il link Oppure avvia dal terminale dell’editor, sotto il pulsante, apre la sessione nel terminale. La sessione termina quando chiudi la scheda dell’editor.
- Da claude.ai/code o dall’app Claude. Dopo l’accesso, la stessa scheda mostra Remote Control: «Le sessioni di questo ambiente si possono avviare e guidare da claude.ai/code e dall’app Claude.» Attiva l’opzione, poi seleziona l’ambiente da claude.ai/code o dall’app.
- Dal telefono. Associa l’app Bucket & Code per il telefono dalla pagina App per telefono del pannello. Nell’app, apri la scheda IA. Scegli l’ambiente, la cartella e lo strumento, poi seleziona Avvia una nuova conversazione.
03Chiedi di creare HelloPublic
Ecco il prompt esatto, inserito nella cartella di un nuovo progetto chiamato 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 ha scritto la procedura di avvio in shared/HelloPublic.sh. Lo script legge da QTS il percorso della cartella condivisa Public. A ogni avvio sovrascrive il file, che contiene quindi una sola riga anche dopo il secondo avvio:
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"
}
}
Ecco un estratto della risposta. Claude Code spiega che in questa sessione ha potuto modificare i file e verificarne la sintassi. Non ha potuto eseguirli:
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.
…
04Chiedi di creare CronEdit
Il prompt elenca una per una le regole di QNAP per cron, così puoi ritrovarle tutte nel codice:
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.
Un estratto della risposta:
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`.
05Le regole del crontab nel codice
CronEdit conserva le proprie righe in cronedit.tab, nella cartella del pacchetto. Il programma bin/cronedit le scrive in /etc/config/crontab, che il NAS conserva dopo il riavvio. Il programma aggiunge un tag in fondo a ogni riga che scrive. Modifica solo le righe che terminano con quel tag:
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
Un passaggio di awk crea il nuovo file. Mantiene ogni riga senza tag al suo posto. Tra le righe con il tag conserva la prima occorrenza di ogni riga richiesta dal pacchetto ed elimina le altre, comprese quelle rimosse da cronedit.tab. Aggiunge in fondo al file le righe richieste che mancano:
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")
Se non ci sono righe da aggiungere o rimuovere, il file resta com’è e cron non si riavvia:
set -- $changes
DROPPED=${1:-0}
ADDED=${2:-0}
if [ "$DROPPED" = 0 ] && [ "$ADDED" = 0 ]; then
rm -f "$new"
return 0
fi
Prima della scrittura, un secondo passaggio di awk verifica che tutte le righe senza tag siano ancora presenti, identiche e nello stesso ordine. Se la verifica fallisce, il file resta com’è. Dopo la scrittura, il programma ricarica cron secondo la procedura di QNAP:
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
}
Lo script del servizio richiama il programma. start sincronizza il file. stop e remove (disinstallazione) rimuovono tutte le righe con il tag. Lo script le rimuove anche se il pacchetto è disattivato in App Center e l’avvio non parte. Questa pulizia serve anche se un’interruzione di corrente ha impedito l’arresto:
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
Quando il pacchetto è attivo, cronedit add e cronedit remove modificano cronedit.tab e sincronizzano subito /etc/config/crontab. Quando è fermo, la sincronizzazione avviene al prossimo avvio.
06Rivedi il codice scritto da Claude Code
Leggi il diff, poi esegui ciò che Claude Code non ha potuto eseguire nella sessione. Per CronEdit, avvia il test del progetto su un file system simulato del NAS con sh build/t/run.sh. Il test è riuscito. La revisione ha portato a queste modifiche:
- CronEdit: il file
bin/CronEditdel modello è stato rimosso. Il progetto non lo eseguiva. Il nome differisce dabin/croneditsolo per l’uso di maiuscole e minuscole. Su Windows o macOS un file sovrascrive l’altro. Claude Code aveva segnalato il problema. Per rimuovere il file serviva un’autorizzazione. Le due righe del README ora descrivonobin/cronedit. - HelloPublic: nessuna modifica al codice.
- Installazione. La risposta indica di installare con
qpkg_cli -m … --keep. Su QTS 6.1 il comando non installa i pacchetti privi di firma.qpkg_cli -srestituisce «notfound». La sezione 08 mostra come installarli.
07Compila il pacchetto
Nella pagina del progetto, il pulsante Compila esegue bvs qbuild nell’ambiente di compilazione del progetto. Nel terminale, esegui lo stesso comando dalla cartella del progetto:
bvs qbuild ls build/ CronEdit_0.1.0_x86_64.qpkg CronEdit_0.1.0_x86_64.qpkg.md5
Senza un certificato di firma del codice nel progetto, il pacchetto è privo di firma. Deploy di prova resta disattivato e mostra il messaggio «Richiede un pacchetto firmato». Puoi installare un pacchetto firmato con Deploy di prova. La guida ad App Center mostra come firmarlo.
08Installa e verifica il pacchetto
Per provare un pacchetto senza firma sul tuo NAS, copia il file in una cartella condivisa. Eseguilo via SSH come admin. QTS lo installa e lo avvia:
# via SSH come admin, nella cartella condivisa che contiene il file
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
- Un pacchetto installato così non ha una riga
Statusin/etc/config/qpkg.conf. Quando installa un pacchetto, App Center scriveStatus = complete. - Firma questi pacchetti e lascia disattivata in App Center l’opzione per le app prive di una firma digitale valida.
- Disinstalla il pacchetto in App Center o con
qpkg_cli -R CronEdit.
HelloPublic dopo l’avvio:
P=$(getcfg Public path -f /etc/config/smb.conf) cat "$P/hello.txt" Hello world
CronEdit con due righe aggiunte mentre è attivo:
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
09Risultati del test
Prima dell’installazione, abbiamo salvato una copia di /etc/config/crontab (58 righe) su un NAS con QTS 6.1. Dopo ogni passaggio abbiamo confrontato il file con la copia.
| Passaggio | /etc/config/crontab dopo il passaggio | Riavvio di cron |
|---|---|---|
| installazione, ancora nessuna riga di CronEdit | identico alla copia salvata | no |
due comandi cronedit add con il pacchetto attivo | due righe con il tag in più | sì, dopo ogni comando |
| arresto | identico alla copia salvata, byte per byte | sì |
| avvio | ricompaiono le due righe | sì |
| secondo avvio | invariato, senza righe duplicate | no |
rimozione di una riga da cronedit.tab senza che lo script esegua la procedura di arresto, come dopo un’interruzione di corrente. Poi avvio | quella riga scompare, l’altra resta | sì |
cronedit add, poi cronedit remove, con il pacchetto attivo | riga aggiunta e poi rimossa | sì, dopo ogni comando |
qpkg_cli -R CronEdit | identico alla copia salvata, byte per byte, anche nell’output di crontab -l | sì |
- Dopo ogni passaggio, le altre 58 righe coincidevano con quelle salvate e avevano lo stesso ordine.
- La riga
* * * * * /bin/date -u >> /share/Public/cronedit-demo.txtha scritto il primo timestamp entro un minuto, confermando che cron esegue le righe con il tag. - Il comando
crond.sh startdi QTS sposta le proprie righevs_refreshebackup_conf.shin fondo al file dopo checrontablo ha caricato. Quando sono presenti le righe di CronEdit,crontab -lpuò quindi mostrare le stesse righe in un ordine diverso da quello del file. - Dopo l’avvio di HelloPublic,
hello.txtcontenevaHello world. Anche dopo il secondo avvio conteneva una sola riga. Il file è rimasto nella cartella condivisa Public dopo l’arresto e la disinstallazione.
10Scarica i sorgenti
ai-qpkg-examples.zip (170 KB) contiene entrambi i progetti dopo la revisione, i due prompt e un README che indica cosa ha scritto Claude Code e cosa è cambiato durante la revisione. Copia un progetto nella tua cartella dei progetti e compilalo con bvs qbuild.