Switching ext4 to btrfs on a VPS with no CD-ROM

The default filesystem on my VPS was ext4 and I wanted to use btrfs. However, the CD-ROM options were disabled so there was no simple way to boot a rescue CD and do the conversion. Happily, it was possible without opening a ticket to use GRUB to boot an ISO image. This will require console access (e.g., VNC) and was done on a Debian 8.0 system virtualized under KVM.

I worked from this btrfs wiki article, which contains additional information about the process including how to rollback to ext4 if unsuccessful.

First, install the necessary software.

# apt-get install btrfs-tools

Next, hopefully there is a swap partition of sufficient size available. If not, it ought to be possible to use a netboot image to do the same thing. I had 512MB swap configured at /dev/vda2, which is perfect. Time to get the iso ready.

# swapoff -a
###
### NOTE: The system may not use /dev/vda2! Check fstab!
###
# mkfs.ext4 -m 0 /dev/vda2
# mount /dev/vda2 /mnt
# cd /mnt
# wget http://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/debian-live-8.1.0-amd64-standard.iso

Grab a VNC console or whatever is available, reboot the system, and interrupt the GRUB prompt from the console. If you never see a prompt and the system just boots right up, try holding down the SHIFT key while the system reboots. Press ‘c’ to enter the command line. (hd0,2) is for /dev/vda2, use what is appropriate for the partition with the iso on it. If done correctly, the stuff after (loop) should tab complete!

> loopback loop (hd0,2)/debian-live-8.1.0-amd64-standard.iso
> linux (loop)/live/vmlinuz boot=live findiso=/debian-live-8.1.0-amd64-standard.iso config quiet splash
> initrd (loop)/live/initrd.img
> boot

On a graphical CD it should be dropped right to the desktop. Simply sudo -i to switch to the root user. On the standard/text live CD I used above, login with username user and password live and sudo -i to root. Now for the conversion work.

### First, install btrfs-tools *on the live-booted system*. This is possible thanks to aufs.

# apt-get install btrfs-tools

### Run the following command on the root system partition, in my case /dev/vda1

# btrfs-convert /dev/vda1
creating btrfs metadata.
creating ext2fs image file.
cleaning up system chunk.
conversion complete.
# mount /dev/vda1 /mnt

### Need the following so that GRUB can update later from chroot

# mount -o rw,bind /proc /mnt/proc
# mount -o rw,bind /dev /mnt/dev
# mount -o rw,bind /sys /mnt/sys

### Now chroot into the btrfs filesystem

# chroot /mnt

### Change the line with the old filesystem from ext4 to btrfs
### The following sed command will work assuming everything was dumped into one
### partition, which is usually the case on a VPS. The line should look like this:
###
### /dev/vda1 / btrfs defaults 0 1

# sed -i s/ext4/btrfs/ /etc/fstab

### Now that fstab is updated, the initramfs must be updated. On Debian we do:

# update-initramfs -k all -u

### Next, make sure GRUB is happy.

# update-grub

### Replace the swap partition and reboot if no errors were encountered.

# exit
# umount /mnt
# mkswap /dev/vda2

### Check fstab. If this is a recent install and not something that has been
### upgraded a few times, chances are that the swap partition is mounted by UUID.
### This UUID has now changed. In this case, get the new UUID using the following
### and replace the old one in the fstab BEFORE rebooting.

# blkid /dev/vda2
### NOTE: The following output is an *EXAMPLE* do not use this in your fstab!
/dev/vda2: UUID="746f0669-5232-4481-8ff4-e68e52fbe8d0" TYPE="swap" PARTUUID="000dcb5c-02"
### UUID is what you want, not PARTUUID

### Finally, reboot the system.

# reboot

Hopefully the system came back up. Log in and verify that everything is as expected. Now
cleanup of the conversion can be done.

# btrfs subvol delete ext2_saved

### Wait until the following command shows the subvol as DELETED

# btrfs subvol list -d /

### Optional but recommended: defrag and rebalance the filesystem

# btrfs fi defrag -r /
# btrfs balance start /

All done! Enjoy the benefits of the btrfs filesystem, including snapshots and compression!

December 10, 2015 В· agw В· No Comments
Tags: , , ,  В· Posted in: Debian, Linux, Technical

Leave a Reply