# How do I extract the list of installed packages on a broken FreeBSD upgrade without using any pkg related commands?



## freebsdnewbie (May 4, 2021)

I had a laptop with no HDD and was running FreeBSD to a USB stick for a while.
Upon upgrading from 12.2 to 13.0, something happened and I think the install must have failed and reboot while I wasn't around.
Now basic commands like pkg and freebsd-update don't work anymore.

Is there a way for me to manually extract the list of installed packages via text files? 
All of the pkg related commands don't work.
I think the USB stick is corrupted and possibly failing.

I was able to copy all files from /var/db/pkg from the broken upgrade.
And I bought a new SSD and did a fresh install of FreeBSD 13 on the same laptop. 

So I am running the freshly installed FreeBSD 13 and mounting the USB with the broken FreeBSD upgrade.

I want the list of packages to be in such a manner that I can type the pkg command on the fresh install, copy/paste the list of packages and run it on the fresh install.
Thank you.


----------



## SirDice (May 4, 2021)

freebsdnewbie said:


> Is there a way for me to manually extract the list of installed packages via text files?


No, this information is stored in a database; /var/db/pkg/local.sqlite to be precise. You might have a working backup of it in /var/backup/ though.


----------



## datasmurf (May 4, 2021)

freebsdnewbie said:


> I was able to copy all files from /var/db/pkg from the broken upgrade.
> And I bought a new SSD and did a fresh install of FreeBSD 13 on the same laptop.



You could try:

`sqlite3 local.sqlite 'select name from packages;'`

this would give you whatever is in the _name_ column in the table _packages _of the saved local.sqlite database from the broken installation.

Maybe this helps.
Good luck.


----------



## datasmurf (May 4, 2021)

With `/bin/sh` you could install them all like that.

`for i in $( sqlite3 local.sqlite 'select name from packages;' ); do pkg install -y $i; done`

To backup your current database would be advisable. Good luck


----------



## T-Daemon (May 4, 2021)

freebsdnewbie said:


> I think the USB stick is corrupted and possibly failing.


If the file system is UFS you can try fsck(8) on the unmounted root partition.



freebsdnewbie said:


> I was able to copy all files from /var/db/pkg from the broken upgrade.





freebsdnewbie said:


> I want the list of packages to be in such a manner that I can type the pkg command on the fresh install, copy/paste the list of packages and run it on the fresh install.



If you can mount the root (/) partition of the USB stick system on the newly installed system you can try (assuming USB system root is mounted on /mnt):

`pkg -c /mnt prime-list > prime-list` ..    # .pkg(8)... -c ..pkg will chroot in the ⟨chroot path⟩ environment.

`pkg install `cat prime-list``


----------



## T-Daemon (May 4, 2021)

datasmurf said:


> With `/bin/sh` you could install them all like that.
> 
> `for i in $( sqlite3 local.sqlite 'select name from packages;' ); do pkg install -y $i; done`



Alternatively: 

`pkg install -y `sqlite3 local.sqlite 'select name from packages;'``


----------

