Monday, December 15, 2014

DIY Xen: Shrinking a Linux Disk

If you've been a reader of my blog for a while you've probably picked up two important details about me. First, I'm a huge fan of open source. Second, I'm a big DIY'er when it comes to software and services. Not that they have to, but I think the two tend to go hand in hand. My guess as to why that is may be because most people who gravitate to open source seem to do so out of a desire to learn.

I've been running my own mail and web server(s) for well over a decade now. Not because I think I can do it better than what's out there. But because I was truly interested in understanding the nitty gritty of what makes the internet run. Historically I had always done this on a single Slackware Linux box. This served it's purpose but did come with a few side affects. I was using my email service as my primary email, my RSS aggregator as my primary source for news, and my CalDav server as my primary calendar.

One big problem I started to run into with my single server setup was that every so often while tinkering with some new software I wanted to learn about I'd inadvertently take down my server for a bit. Which basically meant I was dead out of the water in terms of my email, calendar, and RSS feeds. So I decided to let my curiosity about "The Cloud" turn into working knowledge by setting up my own Xen server.

My initial impression (which still holds true today) is that Xen is awesome. Within just a few hours I was able to get Xen running on my hand built server (16GB RAM, 700GB hard drive, Intel Quad Core i5). Slackware has never let me down so I decided to stick with it for my guest OSs and I setup separate servers for my production services and my tinkering. It's been great.

One problem I ran into while I was trying to find the optimal setup was shrinking a Linux disk that I made too big to start. So I thought I would document the process in case anyone out there was running into the same issue.

  • Shutdown your existing Linux Virtual Machine (VM).
  • Create and attach a new storage device in XEN.
  • Start the Linux VM.
  • Create a partition on new drive.
  • $ sudo /sbin/fdisk /dev/sd[b,c,etc] (i.e. sdc or sdb or etc)
  • Create a filesystem on the new partition.
  • $ sudo /sbin/mkfs.ext4 /dev/sd[b,c,etc]1
  • Create temporary mount point so that you can copy the existing partition over.
  • $ sudo mkdir /temp_mount
  • Mount smaller partition you just created.
  • $ sudo mount /dev/sd[b,c,etc]1 /temp_mount
  • Copy the contents of the existing partition that you want to shrink onto new smaller partition.
  • $ sudo cp -rax /old/drive/* /temp_mount/
  • After the copy has completed unmount the new smaller partition.
  • $ sudo umount /temp_mount
  • Shutdown your Linux VM.
  • Detach the original larger drive from the VM in XEN
  • Restart the Linux VM and verify everything was copied and is working as expected.
  • Delete no-longer-needed storage device in XEN.

No comments:

Post a Comment