Ah, an amateur coffee scientist -- a man after my own heart.
If you don't already have it, you need to get a copy of
"Espresso Coffee, the Science of Quality", Second Edition, by Illy and Viani (with Liverani). This is
the definitive scientific text on all things espresso, from the coffee plant, to processing of the raw bean, to roasting, to grinding, to storage and packing, to percolation, to taste in the cup, and more.
Your first explanation is closer to what Illy and Viani describe:
Dissolved gases, mainly CO2, quickly effervesce in the cup, and bubble up to build a layer of froth. This makes espresso a composite beverage where two distinct constituents are present: supernatant foam and underlying liquid.
Foam
Espresso foam -- also known by the Italian term of crema -- is by itself a biphasic system composed of gas globules framed within liquid films (called lamellae) constituited by a water solution of surfactant. These films tend to set in a configuration of two layers of surface-active molecules facing the gas, with water molecules between them. The high molecular force in the film allows its peculiar geometry: a bubble if isolated, or a honeycomb-like structure of many bubbles growing close together...
The error in your theory is that the CO2 formed during roasting isn't stored just in the pores of the roasted beans, from whence it escapes relatively quickly as you suggest, but is also stored in the intact coffee cells, from whence it takes much longer to escape. Furthermore, grinding does not release all of the CO2:
When broken down by grinding, coffee cells release their pyrolytic gas formed at roasting. This gas is mostly composed of CO2 and CO...
...However, the unfractured cells still present in the larger-size particles (over 50 um) keep their content of high pressure roasting gas, which will contribute to the formation of 'crema'... Inevitably, the gas will slowly escape during storage through microcracks, or through the natural porosity of the cell walls.
So, the answer to the subject question is this: CO2 that remains in the intact coffee cells is released by the pressurized hot water and combines with lipids and other insolubles extracted from the grinds to form the bubbles comprising crema foam.
The retained CO2 is why you have to let the beans rest for several days after roasting. A good bit of the CO2 needs to escape from the cells (i.e., the coffee needs to "degas"), or too much will be released when the hot water hits the puck and will interfere with extraction (i.e., the coffee will be under extracted.) You can see this effect when you drip-brew freshly ground coffee that hasn't been rested: you get a huge "bloom" of CO2 bubbles above the coffee. The bubbles get between the hot water and the coffee grinds, preventing contact and extraction. The result is under extracted coffee.
This also explains why stale coffee usually doesn't produce much, if any, crema. If the beans are allowed to sit for too long after roast, most of the CO2 escapes and you get no crema.