Solved pkg db corrupt on one jail

Code:
]# pkg -j jailname upgrade
Updating custom repository catalogue...
custom repository is up to date.
All repositories are up to date.
pkg: sqlite error while executing PRAGMA journal_mode = WAL; in file pkgdb.c:2755: database disk image is malformed
pkg: sqlite error while executing PRAGMA synchronous = NORMAL; in file pkgdb.c:2755: database disk image is malformed
pkg: sqlite error while executing PRAGMA user_version; in file pkgdb.c:2792: database disk image is malformed

Three other jails and the host are fine, so how do I recover this one? There are no stored backups on this machine as it is a test machine only booted when needed. The configuration is quite complicated so it would be difficult to rebuild the jail.
 
As already written, this machine is never up long enough for daily cron jobs to run so there are no backups.

I need to know how to repair/replace the db without backups. Because it's a jail I don't even know which db files need rebuilding - are they in the host or the jail? /var/db/pkg/ or /usr/local/bastille/jails/jailname/root/var/db/pkg/?
 
should be in your jail
try sqlite3 /var/db/pkg/local.sqlite .dump from your jail
or from the host /usr/local/bastille/jails/jailname/root/var/db/pkg/local.sqlite .dump
 
They're in the jail. The one you want is local.sqlite - see e.g. man pkg, and the example that says how you _could_ restore if only you had a backup.

Are you using ZFS? If you are, there may be a snapshot of the db on the host, something like:

Code:
root@puck:~ # ls -l /usr/local/bastille/jails/smtp/root/.zfs/snapshot/weekly-2026-11-07_04.16.02--1m/var/db/pkg/local.sqlite
-rw-r--r--  1 root wheel 5525504  4 Nov 19:40 /usr/local/bastille/jails/smtp/root/.zfs/snapshot/weekly-2026-11-07_04.16.02--1m/var/db/pkg/local.sqlite
 
Thanks. In the end I renamed /var/db/pkg in the jail and then I could install the packages again. Configuration seems untouched and it's all working.

It's just a question of knowing where the corrupt file was.
 
the problem is that the system wont know what was installed before so you have to reinstall all what you had and pkg upgrades may leave left overs and whatever
 
Well, yes, but if there's no way to recover that data I have to do the best I can. There's a small risk a file which should have been removed in an upgrade will still be there and there's a small risk such a leftover file could cause a problem in future, but it probably won't.
 
you may try to dump the database. if you recover the package names + version it is enough
sqlite3 /path/to/broken/local.sqlite .dump >file.sql
sqlite3 /path/to/broken/local.sqlite "select concat(name,'-',version) nv from packages;" >pakages.txt
 
Back
Top