Hello! I have not posted since the summer and will try to update more often. I try to only put tips and tricks on the Internet when I cannot find an article or blog already posted on the topic.
Anyone who knows me knows I run many operating systems and programs on a Virtual Machine (VM) for testing. I have two main bash scripts I run in Linux on a regular basis that I find useful every day. To understand these bash scripts, please note I keep my virtual machines in the directory /home/VMs. The first script clears out the cache kept by a running VM while the second script scans for and repairs problems on virtual disks.
#!/bin/bash
pwd=$(pwd)
cd /home/VMs
chmod -R 755 *
for i in `ls -alh /home/vms | grep ^d | awk '{print $8}' | grep -v '\.'`
do rm -rfv /home/vms/$i/caches
rm -rfv /home/vms/$i/*.lck
rm -rfv /home/vms/$i/*.log
done
cd $pwd
This script goes to that directory (/home/VMs) and lists any directories in that directory looking for any not containing a period/dot in their name. The script recursively deletes any cache directories, lock (.lck) files, and any log files (.log). I do not run this script if I am troubleshooting a Virtual Machine, but do run this script when I wish to tidy up the files – especially before backing them up.
#!/bin/bash echo ## "sudo echo" if you want to gain sudo privileges for commands below cd /home/VMs for DIR in `ls` do cd /home/VMs/$DIR CHARS=$(echo $DIR | wc -m) let SPACES=$((35-$CHARS)) echo -en "$DIR" for (( i=1; i<=$SPACES; i++)) do echo -en " " done TEST=$(find ./ -name "*.vmdk" | wc -l) if [[ "$TEST" != "0" ]] then for FILES in `ls` do vmware-vdiskmanager -R $FILES done else echo -en "There are no VMDK files in this directory.\n" fi done echo -e "\n\n\n"
If you wanted to substitute a disc/disk wiping utility there, you could. I chose normal removal because I do not need a secure wipe for VMWare’s temporary files.
This last script, also written in bash, will recurse through the same directories in /home/VMs and attempt to repair the virtual disks. Whether you create your virtual disks via a single file or with multiple, this script scans for the VMDKs until it finds one it can scan and/or repair.
I prefer to collect the sudo permission immediately instead of pausing while /home/VMS is scanned because asking for a password immediately after invocation associates the permissions with the program invoked. In order words, it does not leave someone to wonder why they are being asked for credentials seconds or minutes into the program. However, please note sudo access is not necessary to run VMWare’s vdiskmanager. I leave it for sudo to do because some of my VMs are not writable by the username I use on the given machine that hosts the VM.