We consider a model for quasar formation in which massive black holes are formed and fuelled largely by the accretion of hot gas during the process of galaxy formation. In standard hierarchical collapse models, objects about the size of normal galaxies and larger form a dense hot atmosphere when they collapse. We show that if such an atmosphere forms a nearly ‘maximal’ cooling flow, then a central black hole can accrete at close to its Eddington limit. This leads to exponential growth of a seed black hole, resulting in a quasar in some cases. In this model, the first quasars form soon after the first collapses to produce hot gas. The hot gas is depleted as time progresses, mostly by cooling, so that the accretion rate eventually falls below the threshold for advection-dominated accretion, at which stage radiative efficiency plummets and any quasar turns off. A simple implementation of this model, incorporated into a semi-analytical model for galaxy formation, overproduces quasars when compared with observed luminosity functions, but is consistent with models of the X-ray background, which indicate that most accretion is obscured. It produces few quasars at high redshift owing to the lack of time needed to grow massive black holes. Quasar fuelling by hot gas provides a minimum level, sufficient to power most quasars at redshifts between one and two, to which other sources of fuel can be added. The results are sensitive to feedback effects, such as might result from radio jets and other outflows.