How to Mount and Manage LVM Volumes on Android Linux Deploy (UrBackup/Linux Deploy Tutorial Part II)
This tutorial will cover creating and mounting LVM (logical volume management) volumes on your rooted Android device. This is the second part of my "backup server-on-Android" tutorial, but this will be 80% LVM exercise and 20% UrBackup/Linux Deploy. You can find Part I here.
To summarize Part I, we have deployed Debian 10 on our rooted Android phone, installed UrBackup server, and connected a client. To complete this part of the tutorial, you will need the following additional equipment:
- External HDD/SSD with USB cable
- USB hub/dock with charging port and (optional) ethernet port
- (optional) ethernet cable)
(Check the "Optional Equipment" section of Part I for details)
Here's the plan for Part II:
First, we're going to briefly cover LVM. This is a very useful tool to understand, and it will make your life infinitely easier if you ever want to expand your backup storage. Here's a visual example of how it works:
I'm hoping this flow chart gives you an idea of how it works, but it's ok if you don't feel confident with it yet. All you essentially need to understand is the following:
1. Physical block devices are split into partitions that we are labeling physical volumes.
2. We are then reallocating them to logical "pools" called volume groups.
3. The volume groups are then repartitioned into logical volumes.
What's the point of all this? Here's a comparison:
With LVM, we can simply take unused storage and reallocate it where it needs to go. Without LVM, we need to acquire new, larger storage devices and then migrate data to these new devices to expand storage. Then we just have the old, smaller storage devices unused. Hopefully you now you at least get the point, but it's ok if not. Our process today is going to be more linear:
If you're thinking, "that's really complicated for no reason", you're not entirely wrong. If this is all we're doing with it, then it is a little silly. But we're mostly using LVM so that we can do this later:
With our block storage setup with LVM, we can easily add storage later WITHOUT having to transfer data to another bigger drive. We can simply add another drive to the volume group and enlarge our logical volume WITHOUT any data loss. It really is as easy as 1+1=2. If you get lost during our tutorial, come back to these charts to find where you are in the process.
Step 1: Create a Logical Volume From External Storage
This tutorial assumes you are logged in as root.
a. Install LVM:
apt -y install lvm2
b. Disable udev.
This is specific to the Linux Deploy environment. You should not do this if you are in a normal Linux environment. In Linux Deploy, udev will cause various errors in your LVM process.
Find the following lines throughout the file:
multipath_component_detection = 1 md_component_detection = 1 udev_sync = 1 udev_rules = 1
Change them to "0":
multipath_component_detection = 0 md_component_detection = 0 udev_sync = 0 udev_rules = 0
Save and exit.
NOTE: You may still get errors stating "Kernel not configured for semaphores (System V IPC). Not using udev synchronisation code." You can ignore these.
c. Determine the name of your storage device.
Plug in your USB hub and your external storage device. On your server, type the following:
This will show you a long list of devices and partitions. Look for the one that resembles yours (in terms of size and # of partitions).
I know my external hard drive has a capacity of 160 GB, so "sde" looks right to me. If you plugged in your device after boot, it will likely be the last in the "sdX" list. Android's block devices are located at /dev/block, so I now know my external hard drive is located at /dev/block/sde. If you are on a standard Linux distro, yours will be at /dev/sdX.
d. Partition your disk:
This command will enter you into fdisk's partitioning environment. First, view your current partition table:
As you can see, I have one partition, /dev/block/sde1, taking up all 149.1 GiB of my device.
We will be starting from scratch, so I'm going to delete my partition. This is optional if you want to keep your existing partitions.
With my partition table empty, I'm going to format it to DOS/MBT:
This is specific to the Linux Deploy Debian environment; MBT vs GPT does not matter for LVM in a normal Linux environment because the signature will be wiped anyway.
Now, you can either create a partition to use only part of your block device, or you can use the entire block device.
If you want to use the entire device, write and exit:
If you want to partition:
For the default, simply press Enter (as depicted by the empty red boxes below). For the last sector, you can input how large you want your partition to be by entering "+" with the size in kibibytes (K), mebibytes (M), gibibytes (G), tebibytes (T), or pebibytes (P).
Note the partition number. If I wanted to use this partition, I would be using /dev/block/sde1. However, for the rest of this tutorial, I will be using the whole block device and referencing /dev/block/sde.
Write and exit:
e. Create the physical volume from the partition:
You will be asked about wiping the dos signature; select y.
If you get a "not found" or "filtered" error here, go back to the previous step and ensure your disklabel type is set to dos and NOT gpt.
f. Create a volume group with the physical volume:
vgcreate vg_backup /dev/block/sde
You can choose another name for "vg_backup".
Once created, check your volume group details:
This command shows you the stats of all of your volume groups.
This volume group is what you'll be adding physical volumes to if you want to expand your backup storage in the future.
g. Create a logical volume from the volume group:
lvcreate -L 149G -n lv_backup vg_backup
Replace 149G with the desired size of your logical volume. This does not have to be the full size of the volume group; you can create other logical volumes from the volume group if you have space left over.
Replace lv_backup with the desired name of your logical volume.
Replace vg_backup with the name you set for your volume group in step e.
Once created, check your logical volume details:
You will see a path showing something like: /dev/vg_backup/lv_backup. IGNORE THIS. To reference our logical volume, we will be using the following path:
i.e. volume_group_name (DASH) logical_volume_name
h. Format logical volume to EXT4:
Step 2: Mount the Logical Volume
PSA: ONCE YOU MOUNT YOUR BLOCK DEVICE, YOU MUST UNMOUNT BEFORE PHYSICALLY DISCONNECTING YOUR BLOCK DEVICE.
If you disconnect while the device is mounted, you may lose data. Continue reading to the "Unmounting and Remounting the Logical Volume" for more info.
In real life (i. e. on a normal Linux machine), you would mount your logical volume persistently by adding it to /etc/fstab. Android doesn't have /etc/fstab, but fortunately, Linux Deploy can mount our storage for us.
a. Return to the properties menu in Linux Deploy. Ensure that mounts are enabled. Mount your logical volume to your UrBackup backup directory from Part I.
b. Restart your server (press STOP and then START from the main Linux Deploy screen).
c. Ensure your logical volume is mounted properly.
SSH back into your server. Run the following:
df -h /path/to/your/mount/point
Verify the output of this command. Your logical volume should be listed under "Filesystem". Also make sure that "Size", "Used", and "Available" make sense.
d. Ensure the "urbackup" user still has permissions to this backup folder.
In the parent directory of your backup directory:
e. Login to the UrBackup web interface. You will immediately see a warning if UrBackup is unable to access your backup directory. If you don't see a warning, you're done! Go start some backups!
(For Later) Unmounting and Remounting the Logical Volume
If you ever need to disconnect your block device, you should both unmount and deactivate your logical volume first.
b. Verify unmounting:
You should no longer see /path/to/your/mount/point under the "MOUNTPOINT" (right-most) column.
c. Deactivate logical volume:
vgchange -a n
d. Verify deactivation:
Your logical volume(s) should no longer be listed under your block device.
When you need to remount:
e. Reactivate the logical volume:
vgchange -a y vg_backup
f. Verify reactivation:
Your volume group should again be listed under your block device.
g. Remount your logical volume
mount /dev/mapper/vg_backup-lv_backup /path/to/your/mount/point
h. Verify mount:
TIP: If you accidentally unplugged your block device before unmounting and deactivating your logical volume, there's still hope! When you log back into your server and check "lsblk", you will see that there is no logical volume listed under your block device and there is nothing mounted to /path/to/your/mount/point. Simply follow these steps retroactively (ALL the steps from step a to step h), and you may find your data intact.
(For Later) Adding Storage
a. Create your physical volume per steps 1.c - 1.e
b. Extend your volume group:
vgextend /dev/vg_backup /dev/block/sdX
Ensure vgextend was successful:
"VG Size" should reflect the addition of your physical volume.
c. Extend your logical volume:
lvextend -L +200G /dev/mapper/vg_backup-lv_backup
Replace +200G with the amount you want to extend your logical volume by.
Ensure lvextend was successful:
"LV Size" should reflect your extension.
d. Resize your filesystem to match your logical volume:
e. Ensure the new size is reflected on your mount point:
df -h /path/to/mount/point
And that's it! Wasn't that so much easier than migrating all your data to a new disk?
Linux Deploy has MANY quirks we have to work with. Most of them have work arounds. Let me know what quirks you're running into!
Updated 6/28/2021: Added section "Unmounting and Remounting the Logical Volume"