2

I am using an SSD where I have 3 partitions:

$ lsblk  /dev/sda
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 111.8G  0 disk 
├─sda1   8:1    0     2M  0 part 
├─sda2   8:2    0   108G  0 part /
└─sda3   8:3    0   3.8G  0 part /mnt

The /dev/sda2 and /dev/sda3 was formatted as XFS.

I don't need the /dev/sda3 any more, I don't have any data there, and I want to merge it with /dev/sda2. It's not a big problem if I have to unallocate /dev/sda2. But I have important data and the overall OS installed on /dev/sda2.

Is there a way to merge /dev/sda3 with /dev/sda2 in a way that the data on /dev/sda2 remains intact?

Edit:

Before asking I have tried xfs_growfs this way:

# xfs_growfs /dev/sda2 
meta-data=/dev/sda2              isize=512    agcount=4, agsize=7077888 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=1, rmapbt=0
         =                       reflink=0
data     =                       bsize=4096   blocks=28311552, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
log      =internal log           bsize=4096   blocks=13824, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

It doesn't do anything. I have also tried deleting partition 3, and then running xfs_growfs with both / and /dev/sda2. I rebooted, and saw that the free space is free, and /dev/sda2 is 108 GB as always.

I am not using LVM.

Details:

xfs_info -V
xfs_info version 5.7.0

OS:

$ cat /etc/os-release | head -n1
NAME="Arch Linux"

Kernel:

$ cat /proc/version 
Linux version 5.8.5-xanmod1-1-xanmod (makepkg@archlinux) (gcc (GCC) 10.2.0, GNU ld (GNU Binutils) 2.35) #1 SMP PREEMPT Wed, 02 Sep 2020 20:22:18 +0000
15 Volts
  • 2,069

1 Answers1

3

No merging is possible of course; what you described on the last paragraph is the proper way to go, plus one step which I missed initially:

  • umount /mnt, done
  • delete sda3 using fdisk or parted, done
  • extend partition sda2
  • extend filesystem using xfs_growfs /.

The 3rd step is a bit dangerous. If you have a backup of the system proceed, else use gparted iso. On your live system, start fdisk /dev/sda. p to list the partitions. Write down the start sector of sda2 and the id type. Then d to delete it! Next, make a new partition starting from exactly the same sector as before and ending to the last sector of the disk. It'll probably be set to the same id & type, 83 Linux I suppose, but check this also. After you finish p, check again that the start sector of new sda2 and id are the same as the old one, w q. The dangerous part is that if you miss the start or the id, the partition data will be destroyed (well not exactly but you're getting in trouble). Just deleting the partition alters the table, the data are not touched.
After that xfs_growfs / will extend the file system; if not a partprobe or partx command may be needed for the kernel to get informed of the changes. Or you can just reboot.

It may seem odd to delete a partition to extend it, but it's a standard procedure, you can search for it. If you want to avoid it, you can use LVM on future systems.


Another way would be to download gparted make a boot usb/cd, boot from there and do the job in an easy graphical environment.

Krackout
  • 2,642
  • 12
  • 26
  • Yes, I have deleted partition 3 using fdisk (the tools I prefer), w, q. Confirmed that partition 3 is deleted using lsblk.

    The output from lsblk -o name,fstype,size,fssize,mountpoint,label,model,vendor is here: https://pastebin.com/EPQBwfZm ... Now if I do xfs_growfs / and reboot, nothing happens.

    – 15 Volts Sep 06 '20 at 17:39
  • 1
    You are right, a step was missing. I changed my answer. – Krackout Sep 06 '20 at 18:33
  • Oh wow! That's a real tricky one! Wait, first let me do it in my virtual machine... I will let you know... – 15 Volts Sep 06 '20 at 19:41
  • In my VM, I deleted the BIOS boot partition, truncated it from 2 mebibytes to ~970B, installed GRUB, and rebooted. That's fine. Then I tried merging that free space before my /dev/sda2 with the small free space, doing so, I have to forcefully use different starting sector... So isn't it possible to merge partition whose sectors lies before the partition that I am trying to merge? And the system (cloned VM) won't boot now... – 15 Volts Sep 06 '20 at 19:58
  • 1
    First of all, there's no merging! No, you cannot use sectors before. That messes the layout. You must start at exactly the same sector and end on the same as before or greater, if you want to find the file system(s) inside the partition again! Since you are an enquiring mind regarding partitioning, you can also have NO partitions at all :) On VMs that's what I do, in order to be able to extend them live (without LVM). – Krackout Sep 06 '20 at 20:13
  • 1
    And you know what, your suggestion worked out of the box. I then ran dd if=/dev/urandom of=/test_file bs=10M count=1000 status=progress dd failed. I learnt the xfs partition, even though has more space now, can't be utilized unless xfs_growfs is run... Then I ran that on / and the whole system has more space now!! Thanks a lot! – 15 Volts Sep 06 '20 at 20:38
  • 1
    On my laptop, I deleted partition 2, created 2 with the extra space, said N to the suggestion of removal of XFS signature by fdisk, w, q. Then I ran xfs_grow, rebooted, working just fine... Didn't even take a backup... I think there's 0 chance of data corruption... – 15 Volts Sep 06 '20 at 20:48