如何为运行Ubuntu的AWS Lightsail实例无缝添加块存储?根目录即将耗尽,需制定应对方案
Seamlessly Adding Block Storage (No Downtime)
If your root partition is running low, adding an additional block storage volume is a reliable way to offload data without disrupting your instance. Here's the step-by-step process:
Attach the Volume via Lightsail Console
- Navigate to your instance's dashboard, click the "Block storage" tab, then select "Add block storage".
- Pick your desired volume size, name it, and attach it to your running instance—no reboot required here.
Prepare the Volume on Ubuntu
- SSH into your server and run
lsblkto identify the new volume (it’ll appear as something like/dev/xvdf, with no mount point listed). - Format the volume with ext4:
sudo mkfs.ext4 /dev/xvdf(replace/dev/xvdfwith your actual device name). - Create a mount point:
sudo mkdir /mnt/extra_storage. - Test the mount temporarily:
sudo mount /dev/xvdf /mnt/extra_storage.
- SSH into your server and run
Make the Mount Persistent
- Grab the volume’s UUID (more stable than device names) with
sudo blkid /dev/xvdf—look for theUUID=string. - Edit the fstab file to ensure the volume mounts on boot:
sudo nano /etc/fstab - Add this line at the end (replace
your-uuid-herewith the actual UUID):UUID=your-uuid-here /mnt/extra_storage ext4 defaults 0 2 - Save and exit, then verify with
sudo mount -a—no errors mean it’s set up correctly.
- Grab the volume’s UUID (more stable than device names) with
Offload Large Directories to the New Volume
If your root is filling up from specific folders (like/var/lib/docker,/home, or/var/log), move them to the new volume using bind mounts:- For example, relocating Docker data:
- Stop Docker first:
sudo systemctl stop docker. - Copy the data over:
sudo rsync -av /var/lib/docker/ /mnt/extra_storage/docker/. - Rename the original folder as a backup:
sudo mv /var/lib/docker /var/lib/docker_old. - Add a bind mount to
/etc/fstab:/mnt/extra_storage/docker /var/lib/docker none bind 0 0 - Mount the bind:
sudo mount /var/lib/docker. - Restart Docker:
sudo systemctl start docker. - Once you confirm everything works, delete the backup:
sudo rm -rf /var/lib/docker_old.
- Stop Docker first:
- For example, relocating Docker data:
Alternative: Expand the Root Volume (Requires Reboot)
If you’d rather just increase your existing root partition size:
- In the Lightsail console, go to your instance’s "Disk" tab, click "Expand disk", and select your new desired size.
- Reboot your instance (either via the console or
sudo reboot). - After reboot, resize the filesystem:
sudo resize2fs /dev/xvda1(confirm your root device withdf -hfirst—it’s almost always/dev/xvda1). - Verify the new space with
df -h.
Emergency Fixes When Root Directory Is Completely Full
If your root is already at 100% and you can’t run basic commands:
Free Up Quick Space (If You Can Still SSH In)
- Run
sudo du -sh /* | sort -hto pinpoint the largest directories. - Delete non-critical files:
- Old log archives:
sudo rm /var/log/*.gz - Unused packages:
sudo apt autoremove && sudo apt clean - Temporary files:
sudo rm -rf /tmp/*
- Old log archives:
- Be careful—never delete system-critical files like those in
/binor/etc.
- Run
Use Lightsail Rescue Mode (If Instance Is Unresponsive)
- Stop the instance in the Lightsail console.
- Click "Actions" > "Enable rescue mode", then start the instance.
- Connect via the browser-based console—your root volume will be mounted at
/mnt/rescue. - Delete files or move data here to free up space.
- Disable rescue mode and reboot once you’re done.
Prevent Future Issues
- Set up disk usage alerts in Lightsail to get notified when space drops below a threshold.
- Schedule regular cleanup with cron jobs (e.g., weekly
apt autoremoveand log rotation). - Keep large data (like backups, media) on separate block storage volumes.
内容的提问来源于stack exchange,提问作者h38

