Can I withdraw only the contributions from a 401k rollover into a Roth IRA or do I have to withdraw proportionately from contributions and earnings?

My Roth 401k consists of $30,000 in contributions, and $20,000 in earnings. When I rollover into a Roth IRA, can I withdraw only the $30,000 of contributions, or am I required to withdraw the balances proportionately resulting in about $20,000 of contributions, and $10,000 of earnings? The withdrawal will be used to payoff my student loans, which is about $30,000. Also, will I be subject to the 5-year window limitation and be penalized 10% for early withdrawal?