AWS EC2 새 디스크에 /var 폴더 마운트

2021. 3. 19. 17:52서버 프로그래밍

처음 EC2 인스턴스를 생성할때, 적은 용량의 EBS를 사용하도록 했을 경우에는 나중에 용량이 부족해서 추가 EBS를 생성하여 연결해야 한다. 새로운 폴더를 새로 연결된 EBS에 마운트할때는 상관없지만, /var 폴더와 같이 기존에 사용하던 폴더를 새 EBS에 마운트하려면 다음과 같은 순서로 처리를 해야한다.

 

Configuring a new /var partition on a virtual server

When I took over a new virtual server that had been provisioned by my employer’s hosting company, there wasn’t enough free space in the root filesystem. Luckily, they had used Logical Volume Manager (LVM) to sub-divide the virtual disk and there was sufficient free space available to create new volumes. I created extra logical volumes for var and home which had been regular directories in the root filesystem. Since the virtual server provider didn’t provide a KVM-like interface by which I could access the server in single-user mode, I used a very similar method to the one outlined by Aleksander (this answer includes extra details for recovering disk space in addition to LVM-specific commands).

Create a new /var filesystem with LVM

Create a logical volume for the new var filesystem, mount it (using a temporary directory) and copy files from the current /var to the new filesystem:

# Create a new 60GB logical volume in the `VolGroup00` group called `var`.
sudo lvcreate -L 60GB -n var VolGroup00
# Create an ext4 filesystem on this new `var` volume.
sudo mkfs.ext4 /dev/VolGroup00/var
# Mount this filesystem at a temporary mount-point.
sudo mkdir /var.new
sudo mount /dev/VolGroup00/var /var.new

Since running processes will have files in /var kept open and in use, the directory tree can’t simply be moved to the new filesystem. Recursively (-r) copy files from the current /var partition to the new filesystem while preserving file attributes and extended attributes (-a, --archive option). A cautious user might first create an LVM snapshot of the current volume before copying but that’s too much off-topic detail for this question.

sudo cp -ra /var/ /var.new/

Alternatively, the files can be copied with rsync, with its -a, --archive option to preserve time-stamps, ownership, modes, etc. and its -X, --xattrs option to preserve the extended attributes such as the security labels used by AppArmor and SELinux:

sudo rsync -raX /var/ /var.new/

Update the filesystem table

Configure the new filesystem to be used as a new mount-point for /var by adding the following line to /etc/fstab. Note that 0 is used as the pass number (last field) so that the filesystem won’t be automatically checked (fsck) after a certain number of reboots.

/dev/mapper/VolGroup00-var    /var    ext4  defaults  0 0

Since changing into single-user mode is not possible, reboot the computer to use this new volume as /var.

Remove temporary mount-point

After the machine has restarted, the new filesystem will be mounted on /var so the temporary mount-point can be safely removed:

sudo rmdir /var.new

Recover disk space from the root filesystem

The old /var files will still be taking up space on the root partition but they are not easily accessible while another filesystem is mounted at /var (they are “masked” by the new filesystem using the /var directory as its mount-point). Use a temporary mount-point to mount the root filesystem so that contents of the original /var directory are available by an alternative path.

    sudo mkdir /old-root
    sudo mount /dev/mapper/VolGroup00-root /old-root/
    sudo rm -rf /old-root/var/*
    sudo umount /old-root/
    sudo rmdir /old-root/

 

 

askubuntu.com/questions/39536/how-can-i-store-var-on-a-separate-partition

 

How can I store /var on a separate partition?

I'd like to store /var on a separate partition from /. What is the correct way to set this up?

askubuntu.com