Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running apt-get for another partition/directory?

I have booted my system from a live Ubuntu CD, and I need to fix some package problems. I have mounted my hard drive, and now I want to run apt-get as if I booted normally. ie change the working directory for apt-get so it will work on my hard drive. I have done this before, but I can't remember the syntax. I think it was only some flag, like this:

apt-get --root-directory=/mnt/partition1 install....

But I only get "Command line option...is not understood". Any ideas?

like image 878
Avall Avatar asked Oct 17 '10 18:10

Avall


3 Answers

Also this should work:

sudo apt-get -o Dir=/media/partitioni1 update
like image 64
enzotib Avatar answered Nov 04 '22 21:11

enzotib


chroot /mnt/partition1

If your system uses several disk partitions you may have to mount some of them in order to get the package system working (I stopped setting up multiple partitions 10 years ago when hard disks started to get too large for raw physical backup).

This wouldn't work if you don't already have a usable debian system in that location. – akostadinov

If you can't get the package system working when chrooting, perhaps it is too messed up to ever be trusted again - in my experience the effort to bring it back to life rarely pays. If that happens, be happy you can still access your HD, backup your data and perform a clean reinstall.

Some relevant comments from other answer:

apt-get -o RootDir=/tmp/test_apt sets (almost) all paths to be in the different root. btw on a running system, if you copy /etc/apt, /usr/lib/apt, and mkdir -p usr/lib etc var/cache var/lib/dpkg var/lib/apt/lists/partial var/cache/apt/archives/partial and finally touch var/lib/dpkg/status, then apt is going to work in that root. It can even work as a non-root user if you add the option -o Debug::NoLocking=1. The nolock option is necessary because I couldn't find a way to set the lock file inside the different root directory. – akostadinov

Work means using search and downloading packages and such operations. Actually installing is not possible if some essential packages are not already there. debootstrap can help if the goal is actually installing packages in a new root for whatever reason. – akostadinov

like image 34
Paulo Scardine Avatar answered Nov 04 '22 21:11

Paulo Scardine


Running chroot /mnt/partition1 will start a new shell in which the root of the filesystem is /mnt/partition1. Assuming the apt-get on your hard drive still works correctly, you can proceed from there.

dpkg --root=/mnt/partition1 -i mypackage.deb is an option that doesn't require chroot, but does require you to download the package yourself.

like image 26
Jander Avatar answered Nov 04 '22 20:11

Jander