Linux (debian 9) - root partition full: move directories from / to external drive

0

I have a Debian 9 system which is installed on a single storage device.

The size of the device is a bit less than 4 GB, and there is a single partition, onto which the debian image has been written.

I cannot remove this device. It is part of an embedded system and is permanently attached.

However, I can add an SD card to the system, as a spare slot is provided for user expansion if a larger FS is required.

My intention is to create a single partition on an SD card, probably a 16 GB one.

I would like to know what directories is it safe to move to the SD card? And how can I do this?

For example, many Linux systems move the /home folder to a separate physical device or a seperate partition.

So I know I can create a /home directory on the root of the SD filesystem. What I don't know how to do is how to tell the Debian system that /home has moved? Presumably I need to copy all the contents of /home on the original file system to the new SD card before telling Debian that it has moved?

I am guessing that I can also move /tmp and /var folders.

Is there anything else that I can safely move? I presume I should not move /bin, /sbin or /root under any circumstances?

I have worked with systems like this before, where /home is on a different disk, but I have never had to move it after an installation was finished.

I also didn't know what keywords I should be searching for to find this information - so sorry if it seems like a bit of an amature request. Thanks

user3728501

Posted 2019-06-07T16:47:14.087

Reputation: 1 090

@GabrielaGarcia Thanks, that answers one of my questions – user3728501 – 2019-06-07T17:19:38.383

@GabrielaGarcia If you want to add that link as an answer I'll upvote you since 2 others seem to have posted answers, neither of which are as good as the information contained in that article – user3728501 – 2019-06-07T19:09:58.737

Have you also done general clean up? IE, remove old kernels, run apt-get clean etc ? – ivanivan – 2019-06-07T19:18:15.067

@ivanivan yes - there isn't anything I'm aware of on the system that we can delete either – user3728501 – 2019-06-07T19:28:54.517

@ivanivan Perhaps I should have included in my question "are there any directories we can move which will likely get us back a few hundred megabytes minimum?" - our home folder is pretty much empty except for a few basic user config files – user3728501 – 2019-06-07T19:30:20.230

Answers

0

The fact that /home is not a strictly essential part of the OS makes this more feasible than moving, say, /usr to a new device. If you were moving an essential OS hierarchy, you'd want to shut down and boot from some other rescue or live CD or USB filesystem. That could be anywhere from difficult to not possible on your embedded device.

But since you're moving /home we can proceed, cautiously.

First, boot into single-user mode. Stop non-essential services, especially anything that could write to files under /home. Make sure your pwd is not under /home, and then gain root:

$ cd /tmp
$ sudo -i

Then:

1) Insert your SD card and create a partition. After you've created a partition, make a filesystem on it that will hold your new /home heirarchy. Let's assume that your SD card partition is device /dev/sdc1

# mkfs.ext4 /dev/sdc1

2) Mount the new filesystem on /mnt:

# mount /dev/sdc1 /mnt

3) COPY (don't move) or rsync your /home heirarchy onto the SD card (mind the slashes). After rsyncing, unmount the SD card:

# rsync -HAXav /home/ /mnt/
# umount /mnt

4) Backup your /etc/fstab

# cp -p /etc/fstab /etc/fstab.safety

5) Identify the SD card filesystem's UUID and edit /etc/fstab to add an entry to mount that UUID on /home. You can find the UUID using blkid:

# blkid /dev/sdc1
/dev/sdc1: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" TYPE="ext4"

Given the above output, your new fstab line would be:

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx /home ext4 defaults 0 0

That's an Ubuntu example since I don't have a Debian system at hand. You will have to replace the xxxxx... part with the correct UUID of the SD card filesystem, and replace ext4 with the type of filesystem you used when you mkfs'ed the SD card (and confirmed in the output of blkid).

6) Rename the old /home directory:

# mv /home /home.old

7) Make a new mountpoint for the SD card:

# mkdir /home

At this point, you should be able to manually mount the SD card:

# mount /home

and see content under /home that is identical to the content under /home.old. Check the contents carefully, including the ownership and permissions of home directories, and .ssh directories, or you may not be able to login remotely when the system reboots. If possible, start the sshd service and test a remote login to an unprivileged account.

If everything looks good, reboot. When the system comes back up, check the space available in the mounted filesystems:

# df

You won't have any free space in / yet, because /home.old is still hanging around. You should see lots of space available in /home:

# du -h /
# du -h /home

Once you are satisfied that everything looks good, you can get rid of /home.old:

# cd /; rm -rf home.old

You can rm /etc/fstab.safety also, but it's probably small enough that there's little harm in leaving it.

Jim L.

Posted 2019-06-07T16:47:14.087

Reputation: 669