Running apt-get for another partition/directory?

28,148

Solution 1

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

Solution 2

Also this should work:

sudo apt-get -o Dir=/media/partitioni1 update

Solution 3

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.

Share:
28,148
Avall
Author by

Avall

Game developer /w Master of Science Degree in Software Engineering. Currently working as a programmer with Java SE and NetBeans.

Updated on July 09, 2022

Comments

  • Avall
    Avall almost 2 years

    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?

  • akostadinov
    akostadinov about 12 years
    This wouldn't work if you don't already have a usable debian system in that location.
  • akostadinov
    akostadinov about 12 years
    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
    akostadinov about 12 years
    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.
  • Paulo Scardine
    Paulo Scardine almost 11 years
    If you can't get the package system working when chrooting, it is to messed up to be ever trusted again - in my experience the effort to bring it back to life rarely pays. My advice is: be happy you can still access your HD, backup your data and perform a clean reinstall.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 6 years
    @akostadinov Your comments could be written as a good answer here.