How to Safely Resize /var Partition on Remote Debian Server Without Live CD


6 views

When managing remote Linux servers without physical access, resizing critical partitions like /var presents unique challenges. Traditional methods involving live CDs or USB drives aren't feasible in SSH-only environments. Here's a comprehensive approach to safely shrink your /var partition while maintaining system integrity.

Before proceeding, ensure:

  • At least 20% free space in your root (/) partition
  • Backup of critical data (consider rsync -a /var/ /backup/var_backup/)
  • Maintenance window during low-traffic periods
  • Root access via SSH

Here's the optimized procedure with additional technical details:

Preparation Phase

# Stop services using /var
sudo systemctl stop apache2 mysql postfix
# Verify no processes are using /var
sudo lsof +D /var | grep -v "COMMAND"

Remount /var as Read-Only

Attempt to remount, though some systems may resist:

sudo mount -f -oremount,ro /var
# Verify with:
mount | grep " /var "

Create Temporary Copy

sudo mkdir /vartmp
sudo cp -ax /var/* /vartmp/
# Verify copy integrity
sudo diff -r /var /vartmp | grep -v "Only in /var"

Modify System Configuration

Edit /etc/fstab using nano/vim:

# Original line might look like:
# UUID=123... /var ext4 defaults 0 2
# Change to:
#UUID=123... /var ext4 defaults 0 2

Reboot Sequence

After first reboot, execute:

sudo mv /var /varold
sudo mv /vartmp /var
# Verify directory structure
ls -la /var

Now you can resize using standard tools:

# Identify partition
sudo fdisk -l
# For LVM:
sudo lvreduce -L -5G /dev/vg/var
# For standard partitions:
sudo resize2fs /dev/sdaX 15G

After resizing, restore normal operation:

# Uncomment /var in /etc/fstab
sudo nano /etc/fstab
# Final reboot
sudo reboot

If using LVM, consider this cleaner method:

# Create new LV for temporary storage
sudo lvcreate -n vartmp -L 15G vg
sudo mkfs.ext4 /dev/vg/vartmp
sudo mount /dev/vg/vartmp /vartmp
# Copy data
sudo rsync -aAX /var/ /vartmp/
# Then proceed with /var unmount and resize

After successful operation:

# Check filesystem
sudo fsck /dev/sdaX
# Remove old data
sudo rm -rf /varold
# Restart services
sudo systemctl start apache2 mysql postfix

Resizing a critical system partition like /var on a remote Linux server presents unique challenges when you lack physical access or boot media. Here's a comprehensive technical guide for sysadmins working with Debian Lenny systems:

Before proceeding, verify available space with these commands:

df -h
lsblk
fdisk -l

You'll need:

  • Root SSH access
  • At least 1.5x /var's used space free in root (/)
  • Critical services stopped (Apache, MySQL, etc.)

Phase 1: Data Migration

# Stop services using /var
sudo service apache2 stop
sudo service mysql stop

# Remount as read-only (if possible)
sudo mount -f -o remount,ro /var

# Create temporary copy
sudo mkdir /vartmp
sudo cp -av /var/* /vartmp/

Critical Configuration Changes:

# Edit fstab
sudo nano /etc/fstab
# Comment out /var line: #/dev/sdaX /var ext4 defaults 0 2

After initial preparation:

sudo reboot

Post-reboot operations:

# Move directories
sudo mv /var /varmount
sudo mv /vartmp /var

# Verify structure
ls -la /var

Using fdisk for MBR partitions:

sudo fdisk /dev/sda
# Delete partition (d)
# Create smaller partition (n)
# Set proper type (t) and flags (a)
# Write changes (w)

For LVM systems:

sudo lvreduce -L -10G /dev/vg0/var
sudo resize2fs /dev/vg0/var

Restore fstab configuration:

sudo nano /etc/fstab
# Uncomment /var line

Complete the process with:

sudo reboot

Post-reboot checks:

mount | grep var
df -h /var
sudo fsck -f /dev/sdaX

Common issues:

  • Permission problems: sudo chmod -R 755 /var
  • Service failures: Check logs in /var/log
  • Filesystem corruption: Run fsck before final reboot