Useful Unix commands for managing disk space on VMware appliances
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.
1labvcenter:/var/log/vmware # df -h 2Filesystem Size Used Avail Use% Mounted on 3/dev/sda3 11G 4.5G 5.8G 44% / 4udev 7.9G 164K 7.9G 1% /dev 5tmpfs 7.9G 68K 7.9G 1% /dev/shm 6/dev/sda1 128M 38M 84M 31% /boot 7/dev/mapper/core_vg-core 50G 50G 0 100% /storage/core 8/dev/mapper/log_vg-log 9.9G 9.9G 0 100% /storage/log 9/dev/mapper/db_vg-db 9.9G 217M 9.2G 3% /storage/db 10/dev/mapper/dblog_vg-dblog 5.0G 267M 4.5G 6% /storage/dblog 11/dev/mapper/seat_vg-seat 25G 1.3G 23G 6% /storage/seat 12/dev/mapper/netdump_vg-netdump 1001M 18M 932M 2% /storage/netdump 13/dev/mapper/autodeploy_vg-autodeploy 9.9G 164M 9.2G 2% /storage/autodeploy 14/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:
1labvcenter:/ # cd /storage/core 2labvcenter:/storage/core # du * | sort -nr 323710992 vc-labvcenter-2016-02-18--05.36.tgz 411898208 vc-labvcenter-2016-02-14--22.56.tgz 55940060 vc-labvcenter-2016-02-14--21.21.tgz 62948816 vc-labvcenter-2016-02-14--20.06.tgz 72285384 vc-labvcenter-2016-02-18--07.31.tgz 81454044 vc-labvcenter-2016-02-14--19.01.tgz 9705960 vc-labvcenter-2016-02-14--17.56.tgz 10463072 core.vpxd.30775 11390300 core.vpxd.25636 12340800 core.vpxd.24308 13323256 core.vpxd.31694 14321492 core.vpxd.21753 15321100 core.vpxd.26963 16309444 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.
List symbolic links and discover link targets
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.
1labvcenter:/var/log # ls -la 2lrwxrwxrwx 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:
1labvcenter:/storage/core # rm vc-labvcenter-2016-02-18--05.36.tgz
To remove a folder:
1labvcenter:/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