2

Are there any method to extend a filesystem when i adding more hdd to it without using LVM? What are those ?

1
  • 1
    Why not use LVM though? There's even a tool to convert (blocks lvmify). Sep 10 '19 at 8:10
7

In this answer I'll call the resulting device a "volume", and the partitions you use to create it "devices".

LVM internally uses the dmsetup tool to set up its volumes, and uses part of the storage space for storing metadata i.e. information about how the devices are set up.

An alternative is to use dmsetup manually to create devices without need for explicitly allocated storage space for metadata. It also allows you to start using this feature with a partition that already contains data. Let's say you are currently having a drive /dev/sda5 which is an ext4 filesystem. It is 100 gigabytes big, and to get its exact size in sectors you run:

# blockdev --getsz /dev/sda5
195310000

Say you got a new hard drive sdb that is 300GB and want to use it to extend sda5. You could use the entire sdb device without a partition table for this, but for your own long-term sanity's sake, perhaps it's better to create a single partition that spans the whole disk so you remember later how the disk was used. So you then have sdb1, whose size blockdev reports as

# blockdev --getsz /dev/sdb1
583984375

So, to merge these two together, the FIRST THING you do is make sure your old device is unmounted. And of course I should say that even before that, back up your data if anything goes wrong. So, after your backup procedure, run:

# umount /dev/sda5

to be sure. NOTE: I've never tried this on a system with systemd, so please check if there is a better way of doing it to avoid it potentially undoing your manual unmount.

Next we'll create a file that contains the commands to set up the new volume. It requires some manual calculations. Basically the file we create will tell you, one line at a time, where each part of the new volume is located on the disks. So we want sectors 0-195309999 (195310000 sectors total) to map to device /dev/sda5 sectors 0-195309999. And then we want sectors 195310000-779294374 (583984375 sectors total) to map to device /dev/sdb1 sectors 0-583984374. So to do this, we create a file /etc/mybigvolume.dmsetup.txt with the following lines:

0 195310000 linear /dev/sda5 0
195310000 583984375 linear /dev/sdb1 0

Each line has the format (all units in sectors = 512 bytes):

<offset inside volume> <number of sectors> "linear" <source device> <source device offset>

So, reading out loud, the lines mean:

  • The target volume will have its sectors starting from 0 and going 195310000 sectors forward located in the device /dev/sda5, starting at sector 0 inside /dev/sda5

  • The target volume will have its sectors starting from 195310000 and going 583984375 sectors forward located in the device /dev/sdb1, starting at sector 0 inside /dev/sdb1

Side note: For the sake of understanding of numbers, should you want to add another identical 300G disk later to the end, the file contents would be:

0 195310000 linear /dev/sda5 0
195310000 583984375 linear /dev/sdb1 0
779294375 583984375 linear /dev/sdc1 0

Back to the original example; Having created the file, we can now set up the volume so we can start using it. We use dmsetup create for this.

# dmsetup create mybigvolume < /etc/mybigvolume.dmsetup.txt

If all goes well i.e. it outputs nothing, your new volume should now exist as a new device called /dev/mapper/mybigvolume which is 195310000 + 583984375 = 779294375 sectors big. Let's verify this:

# blockdev --getsz /dev/mapper/mybigvolume
779294375

You can run

# dmsetup table

at any point to see which devices have been set up with dmsetup.

Yay! Now a few important things to think about at this point:

  • You must now start using /dev/mapper/mybigvolume for accessing your disk. Always. If you use /dev/sda5, you can break your filesystem. So make sure you don't have /dev/sda5 anywhere anymore. Except of course in /etc/mybigvolume.dmsetup.txt or wherever you stored your dmsetup config.

  • Your filesystem still only uses the first 195310000 sectors of the disk until you specifically ask it to start using the newly available space. Check your file system management tools for info. Had you used LVM, this step would still be needed.

  • This setup (e.g. dmsetup configuration) only lasts until a reboot. So you will need to either configure your system to run the dmsetup create ... command automatically at boot BEFORE filesystems are mounted OR manually run it on every boot, followed by manually mounting the volume. How the former is done is highly dependent on your Linux distribution. But it would probably be similar to how cryptsetup is implemented (which also uses dmsetup to set up devices).

Sample entry in /etc/fstab:

/dev/mapper/mybigvolume  /data  ext4  defaults,noatime  0 0

Finally I'd like to point out that the risk of your volume failing is of course higher than a single device failing. But I don't know about your setup, maybe you use this /dev/mapper/mybigvolume as part of a raid-1 array! Anyway, good luck! :)

PS. Feel free to ask questions!

5
  • 1
    This is a very clear and useful answer, thanks for taking the time to write it! For the long term, it might be better to use device ids rather than /dev/sda5 etc., e.g. using the entries in /dev/disks/by-id. Sep 10 '19 at 8:15
  • Excellent point.. I never quite figured out which of the /dev/disks/by-??? are the "most permanent" though.. Sep 10 '19 at 17:18
  • I use the /dev/disks/by-id/ata-... links, they won’t change for a given disk since they use the disk’s manufacturer, model and serial number. Sep 10 '19 at 17:31
  • I would like some comments on what would be the best filesystem types to choose in fdisk / gparted for sda5 and sdb1 so I could extend my answer with the recommendations. Purpose: to avoid having tools that scan for partitions pick up sda5 on itself as a partition that can be mounted.. Sep 16 '19 at 10:13
  • What's the advantages and disadvantage of thís method, as same as LVM or anything else :/ ? Sep 17 '19 at 2:22
2

If the filesystem is Btrfs and mounted, you can add a device (like HDD or partition) to it. Example:

btrfs device add /dev/sdc2 /current/mountpoint

See man 8 btrfs-device for details. Also: Using Btrfs with Multiple Devices. This does not involve the device-mapper driver at all; the filesystem itself does what usually LVM's job is.

I believe ZFS is similar, i.e. it can handle multiple devices by itself. Useful search term: "adding disk to zpool".

2
  • Yeah exactly, if the filesystem supports it, it's much better to do it that way. Sep 10 '19 at 6:41
  • 1
    also note that an existing ext4 filesystem can easily be converted to btrfs.
    – cas
    Sep 10 '19 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.