<p>Is there a way to identify, from within a VM, that your code is running inside a VM?</p>

<p>I guess there are more or less easy ways to identify specific VM systems, especially if the VM has the provider's extensions installed (such as for VirtualBox or VMWare). But is there a general way to identify that you are not running directly on the CPU?</p>

In most cases, you shouldn't try to. You shouldn't care if someone is running your code in a VM, except in a few specific cases.
If you need to, in Linux the most common way is to look at
```
/sys/devices/virtual/dmi/id/product_name
```
, which will list the name of the laptop/mainboard on most real systems, and the hypervisor on most virtual systems.
```
dmidecode | grep Product
```
is another common method, but I think that requires root access.