How did they live in the rocks? And did they live inside?
Petra was a big religious center, and it was probably a gathering place for clergy, sculptors, merchants, and other people. In Petra, there are more than 1,000 tombs, many of which were for families and even entire tribes. Many of the people there were probably in the funeral business.
It is hard to answer the question regarding whether all of the 20,000 (or maybe even 30,000) residents of the city lived inside of Petra. It is very likely that some of the people lived in tents outside the walls.