Changing a folder to a new drive with symbolic links

Written by Robert -

Changing a folder location.

I'm having the following issue. With the upgrade of a new software package, a virtual server is now generating a lot of session and image caches that it stores in the /tmp directory. This is generating problems because the /tmp is on the / drive and it's too small to handle this data properly. Because this is a virtual machine running in ESX, i'm able to attach a new disk without touching hardware. Now I could go and make the drive the / partition is on bigger and extend the filesystem. However, because this is a serious risk and I wish to avoid downtime, I'm going to add a new drive and use symbolic links to place the actual files on a new drive.

This is the current situation. As you can see, there are two "physical" drives attached to this server. sda has the swap, / and the /boot directory, sdb has the /var/log/apache2 directory.

sda      8:0    0    50G  0
├─sda1   8:1    0   9.8G  0 [SWAP]
├─sda2   8:2    0    40G  0 /
└─sda3   8:3    0   205M  0 /boot
sdb      8:16   0    50G  0
└─sdb1   8:17   0    50G  0 /var/log/apache2
fd0      2:0    1     4K  0
sr0     11:0    1  1024M  0

With the df -h command you can easily see that the problem is on the / drive. It's using 82% of it's disk usage at the time of writing this document.

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        40G   26G   12G  82% /
udev            4.0G  104K  4.0G   1% /dev
tmpfs           4.0G     0  4.0G   0% /dev/shm
/dev/sda3       199M   35M  154M  19% /boot
/dev/sdb1        50G  2.6G   45G   6% /var/log/apache2

The first step is to add a new drive within ESX. After contacting an external party regarding the software that is causing this, I've decided to give this server a drive with a space of 160GB.

alt text

To make this drive accessible, I've rebooted the server. It now shows the correct output.

Disk /dev/sdc: 171.8 GB, 171798691840 bytes
255 heads, 63 sectors/track, 20886 cylinders, total 335544320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0006a3b5

 Device Boot      Start         End      Blocks   Id  System

Using the command parted /dev/sdc I first created a new partition table: mklabel msdos
After this was done I was able to create a new partition filling the entire drive:

mkpart primary ext3 1MiB 100%

After reviewing this you can see that the new disk has a partition on it:

Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 172GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  172GB  172GB  primary  ext3         type=83

After creating a partition using parted, you need to format it:

mkfs.ext3 /dev/sdc1

Now that we have a nice partition on the new drive, we need to mount it. I created a new folder on the system called /mnt/sdc:

mkdir /mnt/sdc

Now I can add this drive to the /etc/fstab file so it will be automatically mounted when the server is booted. I did this by opening the /etc/fstab file with my favorite text edit, VIM. I inserted the following line to the file:

/dev/sdc1 /mnt/sdc ext3 defaults 0 0

Because this drive is only holding caching files, I don't need this file to be backed up by any software so I left the default settings like the ones above.

After saving the file, you can remount the drives according to the content of the fstab file using the following command:

mount -o

After typing the df -h command, you see a nice new line:

/dev/sdc1 158G 188M 150G 1% /mnt/sdc

The following three directory's had to be moved from the /tmp folder to the new location called /mnt/sdc. I need to do this by creating a symbolic link from the location in the /tmp folder to the location in the /mnt/sdc folder.

$:/mnt/sdc # service apache2 stop
Shutting down httpd2 (waiting for all children to terminate)

First I move the folders

mv SessionCache/ SessionCache.old/
mv getmomimg/ getmomimg.old
mv imgredir/ imgredir.old

Now let's create some symbolic links:

$:/tmp # ln -s /mnt/sdc/imgredir/ /tmp/imgredir
$:/tmp # ln -s /mnt/sdc/SessionCache/ /tmp/SessionCache
$:/tmp # ln -s /mnt/sdc/getmoming/ /tmp/getmomimg

After using the ls -lah command, you can see that the newly created symbolic links are having one problem. The links are owned by the root user and the root group, instead of the wwwrun user in the www group.

lrwxrwxrwx  1 root root    22 Apr 28 21:28 SessionCache -> /mnt/sdc/SessionCache/
drwxrwxrwx  3 wwwrun www  4.0K Apr 28 17:54 SessionCache.old
lrwxrwxrwx  1 root root    19 Apr 28 21:28 getmomimg -> /mnt/sdc/getmoming/
drwxrwxrwx 18 wwwrun www  4.0K Mar 10  2014 getmomimg.old
lrwxrwxrwx  1 root root    18 Apr 28 21:28 imgredir -> /mnt/sdc/imgredir/
drwxrwxrwx 18 wwwrun www  4.0K Apr 28 18:29 imgredir.old

We fix this by using the chown command.

chown -R wwwrun:www imgredir/
chown -R wwwrun:www getmoming/
chown -R wwwrun:www SessionCache/

We also need to make sure this is done at the /mnt/sdc directory. After doing this we can start apache2 again:

service apache2 start

Once apache2 is started, I can now check that the drive is being populated

hostname:~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        40G  5.7G   32G  16% /
udev            4.0G  108K  4.0G   1% /dev
tmpfs           4.0G     0  4.0G   0% /dev/shm
/dev/sda3       199M   35M  154M  19% /boot
/dev/sdb1        50G  2.6G   45G   6% /var/log/apache2
/dev/sdc1       158G  4.6G  145G   4% /mnt/sdc

Now make sure you test the functionality of the software, in this case the webserver and your done.