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

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