You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为运行Ubuntu的AWS Lightsail实例无缝添加块存储?根目录即将耗尽,需制定应对方案

How to Add Block Storage to Ubuntu Lightsail & Fix Full Root Directory

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:

  1. 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.
  2. Prepare the Volume on Ubuntu

    • SSH into your server and run lsblk to 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/xvdf with your actual device name).
    • Create a mount point: sudo mkdir /mnt/extra_storage.
    • Test the mount temporarily: sudo mount /dev/xvdf /mnt/extra_storage.
  3. Make the Mount Persistent

    • Grab the volume’s UUID (more stable than device names) with sudo blkid /dev/xvdf—look for the UUID= 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-here with 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.
  4. 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.

Alternative: Expand the Root Volume (Requires Reboot)

If you’d rather just increase your existing root partition size:

  1. In the Lightsail console, go to your instance’s "Disk" tab, click "Expand disk", and select your new desired size.
  2. Reboot your instance (either via the console or sudo reboot).
  3. After reboot, resize the filesystem: sudo resize2fs /dev/xvda1 (confirm your root device with df -h first—it’s almost always /dev/xvda1).
  4. 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:

  1. Free Up Quick Space (If You Can Still SSH In)

    • Run sudo du -sh /* | sort -h to 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/*
    • Be careful—never delete system-critical files like those in /bin or /etc.
  2. 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.
  3. 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 autoremove and log rotation).
    • Keep large data (like backups, media) on separate block storage volumes.

内容的提问来源于stack exchange,提问作者h38

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 16:02:51