Matrix free petsc solver with multigrid (MG or GAMG) preconditioner

I want to use petsc MG or GAMG preconditioner in conjunction with matrix free approach given here.
So in line 346, I want
instead of
Is it possible to use petsc preconditioners directly or do I need to define my own preconditioner? I am presuming something like PCSHELL