This was something I needed in th %pre of my kickstart, but its likely to be generally handy so noting the recipe for future reference.

# Detect virtualisation
if [[ $( hostnamectl status | grep Chassis | awk '{print $2}' ) == 'vm' ]]
then
    # Virtual machine
    echo "This is a virtual machine."
else
    # Physical machine
    echo "This is not a virtual machine."
fi