Useful Unix commands for managing disk space on VMware appliances

Categories: Quick Posts

Coming from a Windows background without much knowledge of Unix commands, I often find myself at a loss when trying to figure out how to do things on VMware’s vSphere appliances.  Managing disk space from the command line on an appliance is something I’ve had to do more than a few times, so I thought I’d create a quick list of the Unix commands I use most often to identify which partitions are filling up, and then which folders and files on that partition are consuming the most space.

When I’m working on a disk space problem, there are few things I need to do. First, list disk space by partition. Second, identify the biggest consumers on a partition by listing disk usage of child files and folders. Third, figure out if any of the directories identified in the previous step are symbolic links, and find the link target. Lastly, depending on what files are consuming all that space, I may want to delete them.

List disk space per partition

The df command (which is an abbreviation for disk free) is the trick.  The -h switch will display file sizes in KB, MB and GB.

labvcenter:/var/log/vmware # df -h
Filesystem                            Size  Used Avail Use% Mounted on
/dev/sda3                              11G  4.5G  5.8G  44% /
udev                                  7.9G  164K  7.9G   1% /dev
tmpfs                                 7.9G   68K  7.9G   1% /dev/shm
/dev/sda1                             128M   38M   84M  31% /boot
/dev/mapper/core_vg-core               50G   50G     0 100% /storage/core
/dev/mapper/log_vg-log                9.9G  9.9G     0 100% /storage/log
/dev/mapper/db_vg-db                  9.9G  217M  9.2G   3% /storage/db
/dev/mapper/dblog_vg-dblog            5.0G  267M  4.5G   6% /storage/dblog
/dev/mapper/seat_vg-seat               25G  1.3G   23G   6% /storage/seat
/dev/mapper/netdump_vg-netdump       1001M   18M  932M   2% /storage/netdump
/dev/mapper/autodeploy_vg-autodeploy  9.9G  164M  9.2G   2% /storage/autodeploy
/dev/mapper/invsvc_vg-invsvc          9.9G  188M  9.2G   2% /storage/invsvc

Now we know that /storage/core and /storage/log are both 100% full, we need to work out what is consuming the space on those partitions.

List disk usage of child files and folders on a partition

The du command (which is an abbreviation for disk usage) estimates the size of directories and files under a specific path. The best way to use this command is to sort the results by file size, as follows:

labvcenter:/ # cd /storage/core
labvcenter:/storage/core # du * | sort -nr
23710992        vc-labvcenter-2016-02-18--05.36.tgz
11898208        vc-labvcenter-2016-02-14--22.56.tgz
5940060 vc-labvcenter-2016-02-14--21.21.tgz
2948816 vc-labvcenter-2016-02-14--20.06.tgz
2285384 vc-labvcenter-2016-02-18--07.31.tgz
1454044 vc-labvcenter-2016-02-14--19.01.tgz
705960  vc-labvcenter-2016-02-14--17.56.tgz
463072  core.vpxd.30775
390300  core.vpxd.25636
340800  core.vpxd.24308
323256  core.vpxd.31694
321492  core.vpxd.21753
321100  core.vpxd.26963
309444  core.vpxd.26672

You can also use the -h switch to present the file size in a more friendly format, but the downside of that approach is that you can’t pipe the results to sort, as the list will be incorrectly sorted because it only considers the numbers and not the units, so it doesn’t understand that 10GB is larger than 100MB).

One thing to note is that if a child folder is actually a symbolic link, the file size will be listed as zero. These need to be identified and handled separately.

To identify symbolic links, use the ls command with the -la switches. The results will be colour coded, and symbolic links will be listed in a light blue colour. The real path of the symbolic link will be listed to the right. In the snipped example below, you can see that /var/log/vmware is actually a symbolic link to /storage/log/vmware.

labvcenter:/var/log # ls -la
lrwxrwxrwx  1 root  root        12 Feb 18 11:12 vmware > /storage/log/vmware

Delete files or folders

It’s possible to delete files via the command line using the rm command and specifying the file or folder name. To remove a file:

labvcenter:/storage/core # rm vc-labvcenter-2016-02-18--05.36.tgz

To remove a folder:

labvcenter:/storage/core # rm -r netdumps

But use that with caution, because it could end badly.

A third option is actually my preferred choice, but it doesn’t involve using the command line at all. This option is to use a program like WinSCP to connect to the appliance and delete the files via the GUI.

This is a good thing in my opinion, as there’s less of a risk of accidentally deleting a folder by mistake, and because it’s much easier to delete multiple files at once.

comments powered by Disqus

See also