Increasing the available space for the /home directory by reallocating space from another partition (such as /mnt) can be achieved by strategically moving large directories from /home to a partition with more available space and creating symbolic links or bind mounts. This approach avoids the complexities and risks associated with resizing disk partitions directly. Below are the steps you can follow using Bash commands:
1. Identify Large Directories in /home
First, determine which directories in /home are consuming the most space. This will help you decide what to move.
du -h --max-depth=1 /home | sort -hr
2. Choose a Directory to Move
Suppose you find that /home/user/Documents is a large directory you want to move to /mnt to free up space in /home.
3. Create a Destination Directory on /mnt
Create a new directory on the /mnt partition where you'll move the data.
sudo mkdir /mnt/home_data
4. Move the Directory to /mnt
Move the selected directory from /home to the new location on /mnt.
sudo mv /home/user/Documents /mnt/home_data/
5. Create a Symbolic Link
Create a symbolic link in the original location pointing to the new location on /mnt. This makes it appear as if the directory is still in its original place.
ln -s /mnt/home_data/Documents /home/user/Documents
6. Update Permissions (If Necessary)
Ensure that the permissions of the moved directory are correct so that your user can access it without issues.
sudo chown -R user:user /mnt/home_data/Documents
Replace user:user with your actual username and group.
7. Repeat as Needed
Repeat steps 2–6 for other large directories within /home that you wish to move to /mnt.
8. Automate Bind Mounts (Optional)
If you prefer using bind mounts instead of symbolic links, you can add entries to /etc/fstab to make the changes persistent across reboots.
-
Edit /etc/fstab:
sudo nano /etc/fstab
-
Add Bind Mount Entry:
Add a line like the following to bind the new location to the original path:
/mnt/home_data/Documents /home/user/Documents none bind 0 0
-
Apply the Changes:
sudo mount -a
9. Verify Available Space
After moving directories, check the available space in /home to ensure that the changes have had the desired effect.
df -h /home
Notes and Considerations
- Backup Important Data: Before moving directories, ensure you have backups to prevent data loss.
- Permissions: Make sure that directory permissions and ownerships are correctly set after moving to avoid access issues.
- Symbolic Links vs. Bind Mounts: Symbolic links are easier to set up but can sometimes cause issues with applications that expect the directory to be in place. Bind mounts are more transparent but require changes to
/etc/fstab for persistence.
- Avoid Moving System Directories: Be cautious not to move essential system directories within
/home that are required for user sessions and system operations.
Example Script
Here's a simple Bash script that automates moving a directory and creating a symbolic link:
#!/bin/bash
# Variables
USER_NAME="your_username"
DIR_TO_MOVE="Documents"
SOURCE="/home/$USER_NAME/$DIR_TO_MOVE"
DEST="/mnt/home_data/$DIR_TO_MOVE"
# Create destination directory
sudo mkdir -p /mnt/home_data
# Move the directory
sudo mv "$SOURCE" "$DEST"
# Create symbolic link
ln -s "$DEST" "$SOURCE"
# Update ownership
sudo chown -R "$USER_NAME":"$USER_NAME" "$DEST"
echo "Moved $DIR_TO_MOVE to /mnt/home_data and created a symbolic link."
Usage:
-
Replace your_username with your actual username.
-
Save the script to a file, e.g., move_home_dir.sh.
-
Make the script executable:
chmod +x move_home_dir.sh
-
Run the script:
./move_home_dir.sh
This script moves the specified directory from /home to /mnt/home_data, creates a symbolic link, and updates ownership permissions.
Conclusion
By moving large directories from /home to a partition with more available space and using symbolic links or bind mounts, you can effectively increase the usable space in /home without having to resize disk partitions. This method is safer and can be performed with standard Bash commands.
Caution: Always ensure you have backups before making significant changes to your filesystem. Improper handling can lead to data loss.