Modifying partitions

Run this lsblk command to get a clear overview of all your disks and partitions

$ lsblk -o KNAME,TYPE,FSTYPE,SIZE,LABEL,VENDOR,MODEL

Find the disk you want to modify, and take note of its kname. Note: names like sda, sdb and sdc represent disks. Names ending in a number like sda1, sda2 or sdb1 represent partitions.

Replace sda with the diskname/kname you want to modify. (so not a partition on that disk)

$ cfdisk /dev/sda

This will open a cli UI where you can select, modify, delete, and create partitions. Also make sure to select the right partition type. Finally write the new config to disk, and quit

Alternativeley, fdisk can be used, this is a less interactive tool.

$ fsdisk /dev/sda
p (optional: print current table)
g (Create GPT partion table on new disks)
n (Create a new partition, select first and last sector)
t (Change partition type, follow instructions)
p (optional: print)
w (write configuration to disk)

LVM

Source: http://www.devops-engineer.com/how-to-create-linux-lvm-step-by-step/

To setup LVM:

  1. Create at least a single partition with partition type Linux LVM

  2. Initialize physical volume: pvcreate /dev/sda1 (Optional, vgcreate will do this automatically)

  3. Create a volume group vgcreate -s 16M vg0 /dev/sda1

  4. Create a logical volume with maximum size lvcreate -n lvol0 -l 100%FREE vg0

  5. Intialize the filesystem mkfs.ext4 /dev/mapper/vg0-lvol1

  6. (Mount the new partition mount /dev/mapper/vg0-lvol0 PATH, or add it to fstab)

Adding a new disk to a virtual machine

This command rescans all iscsi busses and prints all devices it found.

for host in /sys/class/scsi_host/*; do echo "- - -" | sudo tee $host/scan; ls /dev/sd* ; done

Initializing a filestystem

To create a filesystem on a partition you can use mkfs utils, for instance mkfs.ext4 or mkfs.exfat. Obviously, replace the Volume Label name and partition name

$ mkfs.exfat -n 'Volume Label' /dev/sda1

Increase live LVM partition size

Source: https://carleton.ca/scs/2019/extend-lvm-disk-space/

In case of a VM; if after increasing the virtual disk size, the guest system does not recognize the new space, you can rescan the disk. (change 'sda' to the drive name). Afterwards run parted -l, and pass f/fix when it warns about available space.

$ echo 1 > /sys/class/block/sda/device/rescan
$ parted -l
Warning: Not all of the space available to /dev/sda appears to be used, you can
fix the GPT to use all of the space (an extra xxxx blocks) or continue with
the current setting? 
Fix/Ignore? f

$ fdisk /dev/sda
p #(print, make sure that there is available/unpartitioned diskspace)
d #(delete, then enter the partion number you want to grow)
n #(new, make sure the first sector is the exact same as before, choose a last sector equal to or higher than before.)
# If prompted, don't delete the filesystem signature
t #(partition type, if prompted, choose the partition number you just recreated)
# Enter the GUID for lvm. for GPT disks this is 'E6D6D379-F507-44C2-A23C-238F2A3DF928', for MBR disks, this is '8e'.
p #(print, check your changes)
w #(write the partiton table)

$ sudo pvresize /dev/sda1

$ sudo lvdisplay # To find the logical volume path, should look like '/dev/vg0/lvol0'

$ sudo lvextend -l +100%FREE /dev/vg0/lvol0

$ partprobe /dev/sda # Make the kernel aware of the changed partition table, this fill fail for mounted filesystems, you can skip this step

$ e2fsck /dev/vg0/lvol0 # Check the filesystem, this might not work on a mounted filesystem, if so, skip.

$ resize2fs /dev/vg0/lvol0 # Grow the filesystem

Increase live root partition size (Non-LVM)

Source: https://medium.com/100-days-of-linux/how-to-resize-a-linux-root-file-system-af3e5096b4e4

In case of a VM; if after increasing the virtual disk size, the guest system does not recognize the new space, you can rescan the disk. (change 'sda' to the drive name). Afterwards run parted -l, and pass f/fix when it warns about available space.

$ echo 1 > /sys/class/block/sda/device/rescan
$ parted -l
Warning: Not all of the space available to /dev/sda appears to be used, you can
fix the GPT to use all of the space (an extra xxxx blocks) or continue with
the current setting? 
Fix/Ignore? f

$ fdisk /dev/sda
p #(print, make sure that there is available/unpartitioned diskspace)
d #(delete, then enter the partion number you want to grow)
n #(new, make sure the first sector is the exact same as before, choose a last sector equal to or higher than before.)
# If prompted, don't delete the filesystem signature
p #(print, check your changes)
w #(write the partiton table)

$ partprobe /dev/sda # Make the kernel aware of the changed partition table

$ e2fsck /dev/sda1 # Check the filesystem, this might not work on a mounted filesystem, if so, skip.

$ resize2fs /dev/sda2 # Grow the filesystem

Howto/DiskManagement (last edited 2023-01-20 11:29:35 by Burathar)