1

I have a machine on which the root partition is running out of space. I have another disk on which there is plenty of free space. How do I configure apt-get to install packages on a different disk/directory?

0

4 Answers 4

3

You don't want to do that. The first thing is to try to reclaim space under root. This is probably doable. To start with, cd to /var/cache/apt/archives and type du -hs. This will give the amount of space used by deb packages cached by apt. You can remove these manually or by using apt-get clean.

There are also tools for removing large unused packages. For example, you can install wajig (apt-get install wajig) and use wajig large. That will give you a list of packages in increasing order of size.

If this doesn't solve your problem (and it should) then you can consider trying to increase your root filesystem. Are you using LVM? Regardless, what you want to do is extend the root filesystem to be larger. With LVM you could easily enlarge it across another disk, but you are going to unmount the room filesystem first. If you are not using LVM it will probably be harder - I have only used LVM for a really long time, so I'm not sure of the options there.

If you want more specific information, paste the output of df -h into your question.

1

apt-get(8) does not have a direct option to change the install directory. dpkg(1) does, but it is intended for a chroot environment.

You could install normally and then move the application files, setting up symbolic links from the original locations to the new locations. This may not work for all applications. Also upon uninstall it may be an issue if you don't reverse the process.

Your best bet is to clean up your / root partition to free up space; move /usr or other directory if under your / root partition to your free disk and mount under root; use LVM to extend your root partition utilizing free space on your other disk; or to rebuild the package to install elsewhere.

1

You could first check if there is any big log file or similar taking too much space (su du and find to find it).

If you are not able to reclaim space by any method (say apt-get clean, find if there is any big file with du -sh /* and then du -sh /bigger_directory/*, etc), then you could move /usr to a new partition on the same or in other disk. This is the directory where all the packages are typically installed so you should recover most of the disk space by moving it.

I've done this a couple of times and I've even migrated full systems with a similar approach (i.e. moved my laptop's OS to a new SSD disk). The steps I took:

  • Backup the system and the data (always before doing this kind of changes).
  • Prepare and format a partition in the disk (we'll use it as /usr).
  • Restart the system with a LIVECD or USB-stick Linux distribution.
  • While on the live CD, launch a terminal and become root.
  • Create a temporary partition in the ramdisk and mount your local PC disks there to work.
  • Execute the required commands to copy /usr data to the new disk. Recalling from my memory (I think they're OK):

sudo su - root mkdir /temp ### Assuming / is /dev/sda1 : mount -t ext4 /dev/sda1 /temp cd /temp mv /temp/usr /temp/usr.OLD mkdir /temp/usr chown root:root /temp/usr chmod 755 /temp/usr ### Assuming your new partition is /dev/sda6 mount -t ext4 /dev/sda6 /temp/usr ### Copy the entire old /usr to the new disk rsync -avz /temp/usr.OLD/* /temp/usr ### Edit your fstab to automount /usr from the new location vim /temp/etc/fstab ### (add an entry like: /dev/sda6 /usr ext4 defaults 1 2) cd / umount /temp/usr umount /temp/ rmdir /temp exit

  • Now, you've copied your /usr data to the new partition, you've created an empty /usr directory and you've modified your /etc/fstab file so that when the system starts, it mounts /usr from this new partition.
  • You can now start your system and check that it works correctly.
  • When you're sure that the system works correctly, you can delete /usr.OLD and recover all the disk used by /usr.

If there is any kind of problem, remember that you can always recover your old usr with the same method (boot with livecd, mount your / in /temp, remove the /etc/fstab entry and delete /usr and mv /usr.OLD as /usr).

DISCLAIMER: Please note that although this is not a very advanced procedure, you should be quite careful and be sure that you fully understand all the steps. Nothing is deleted in this procedure until you test that it works, but please check everything before following the procedure. You can practice this with an example partition (let's say /opt) if you want to test it first.

1

Installing different packages on different partitions is technically doable but difficult. You could use a union mount to direct newly installed packages and package versions to a different filesystem while keeping everything visible at the normal location, but it's overkill.

Instead of installing new packages in a different location, pick a large directory tree and move that to a different filesystem, then create a symbolic link. For example, if you've created a new filesystem that's mounted on /LARGE, and you decide to move /usr/share/doc to this new filesystem:

mv /usr/share/doc /LARGE/doc
ln -s /LARGE/doc /usr/share/doc

Then continue using your system normally.

Ideally, you should enlarge the root filesystem. If you use one a mainstream filesystem (e.g. ext4, btrfs) on LVM, you can do it while your system is running: enlarge the LVM volume (arrange to have some free space on the physical volume(s) then run lvextend), then enlarge the filesystem on it to fill the volume (run resize2fs or btrfs filesystem resize). If you aren't using LVM, you may need to reboot to a live system in order to enlarge the root partition.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .