Home » Articles » How to add new storage to linux server live without rebooting.

How to add new storage to linux server live without rebooting.

How to add new storage to linux server live without rebooting.

Short version:

  • Resize the virtual disk in vmware or add a new hard drive
  • Detect the new change:
    • partprobe
    • echo “- – -” > /sys/class/scsi_host/host0/scan
    • fdisk –l
  • pvcreate /dev/sda3
  • vgextend rootvg /dev/sda3
  • lvextend
    • lvextend –L +10G /dev/rootvg/var
    • lvextend –l +100%FREE /dev/rootvg/var
  • Resize filesystem
    • lvresize -r -l +100%FREE /dev/mapper/rootvg-var
    • resize4fs /dev/mapper/rootvg-var 

How to detect new drive in Linux without rebooting.

  • partprobe
    • partprobe – inform the OS of partition table changes
    • echo “- – -” > /sys/class/scsi_host/host0/scan
    • That "- - -" is wildcards for channel, id & LUN respectively, so you can restrict the scan to some subset if you want by specifying numbers instead.

How to delete a drive from Linux without rebooting.

You removed a hotswap drive and replaced it with a new drive. Ex.: /dev/sdc  When you added the drive, it was detected /dev/sdd instead of /dev/sdc.  You must delete the old entry from the table to allow the new drive to be assigned the right entry.

  • echo 1 > /sys/block/<devnode>/device/delete  *where devnode = sda, sdX

 

How to extend a file system

  1. Login to the machine as root or ‘sudo’ each of the following commands, enter ‘fdisk –l’, you should see something like this;Disk /dev/sda: 21.1 GB, 21xxxxxxxxx bytes
    255 heads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 2610 20860402+ 8e Linux LVMIn this case I’ve altered the values but as you can see this machine has a single ~20GB root virtual disk with two partitions, sda1 and sda2, sda2 is our first LVM ‘physical volume’, see how LVM uses a partition type of ‘8e’.
  2. Now type ‘pvdisplay’, you’ll see a section for this first PV (sda2) like this;— Physical volume —
    PV Name /dev/sda2
    VG Name rootvg
    PV Size 19.89 GB / not usable 19.30 MB
    Allocatable yes (but full)
    PE Size (KByte) 32768
    Total PE 636
    Free PE 0
    Allocated PE 636
    PV UUID PgwRdY-EvCC-b5lO-Qrnx-tkrd-m16k-eQ9beCThis shows that this second partition (sda2) is mapped to a ‘volume group’ called ‘rootvg’.
  3. Now we can increase the size of the virtual disk using the usual vSphere VSClient by selecting the VM, choosing to ‘edit settings’, then selecting ‘Hard Disk 1’. You can then increase the ‘Provisioned Size’ number – so long as there are no snapshots in place anyway – and select OK. This will take a few seconds to complete.
  4. If you then switch back to the Linux VM and enter ‘echo “- – -“ > /sys/class/scsi_host/hostX/scan’, where the X character is likely to be zero, it will perform a SCSI bus rescan, then reenter ‘fdisk –l’, you should see something like;Disk /dev/sda: 42.2 GB, 42xxxxxxxxx bytes
    255 heads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Device Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 2610 20860402+ 8e Linux LVMYou’ll see that the disk size has increased, in this case to ~40GB from ~20GB but that the partition table remains the same.
  5. We now need to create a new LVM partition, type ‘parted’, you should see something like this;GNU Parted 1.8.1
    Using /dev/sda
    Welcome to GNU Parted! Type ‘help’ to view a list of commands.
    (parted)You’ll now need to create a new partition for the extra new space, type ‘p’ to see the current partition table such as this;

    Model: VMware Virtual disk (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number Start End Size Type File system Flags
    1 32.3kB 107MB 107MB primary ext3 boot
    2 107MB 21.5GB 21.4GB primary lvm

    Then type ‘mkpart’, then select ‘p’ for ‘Primary’, for file system type enter ‘ext3’, for start enter a number a little higher than the combination of both ‘sizes’ listed above (i.e. 107MB + 21.4GB, so say 21.6GB), for end type the size of the disk (i.e. in this case 42.9GB).
    Once you press enter it will create this new primary partition, type ‘p’ to show the new partition table, you should see something like;

    Model: VMware Virtual disk (scsi)
    Disk /dev/sda: 42.9GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number Start End Size Type File system Flags
    1 32.3kB 107MB 107MB primary ext3 boot
    2 107MB 21.5GB 21.4GB primary lvm
    3 21.5GB 42.9GB 21.5GB primary ext3

    You’ll see that the new partition started after the first two and fills the available space, unfortunately we had to set it to a type of ‘ext3’, so let’s change that.

  6. Type ‘t’, then the partition number (in our case 3 as it’s the third partition), then for the ‘hex code’ enter ‘8e’ – once you’d done this type ‘p’ again and you should see it change to ‘Linux LVM’;Disk /dev/sda: 42.9 GB, 42949672960 bytes
    ads, 63 sectors/track, 5221 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytesDevice Boot Start End Blocks Id System
    /dev/sda1 * 1 13 104391 83 Linux
    /dev/sda2 14 2610 20860402+ 8e Linux LVM
    /dev/sda3 2611 5221 20972857+ 8e Linux LVM
  7. Now we need to create a new LVM ‘physical volume’ in this new partition, type ‘pvcreate /dev/sda3’, this should then create a new LVM PV called /dev/sda3, type ‘pvdisplay to check;— Physical volume —
    PV Name /dev/sda3
    VG Name
    PV Size 20.00 GB / not usable 1.31 MB
    Allocatable no
    PE Size (KByte) 0
    Total PE 0
    Free PE 0
    Allocated PE 0
    PV UUID gpYPUv-XdeL-TxKJ-GYCa-iWcy-9bG6-tfZtShYou should see something similar to above.
  8. Now we need to extend the ‘rootvg’ ‘Volume Group’, or create a new one for non-root ‘volume group, type ‘vgdisplay’ to list all ‘volume groups’, here’s an example;— Volume group —
    VG Name rootvg
    System ID
    Format lvm2
    Metadata Areas 2
    Metadata Sequence No 19
    VG Access read/write
    VG Status resizable
    MAX LV 0
    Cur LV 8
    Open LV 8
    Max PV 0
    Cur PV 2
    Act PV 2
    VG Size 21.3 GB
    PE Size 32.00 MB
    Total PE 1276
    Alloc PE / Size 846 / 26.44 GB
    Free PE / Size 430 / 13.44 GB
    VG UUID tGM4ja-k6es-la0H-LcX6-1FMY-6p2g-SRYtfYIf you want to extend the ‘rootvg’ ‘volumer group type ‘vgextend rootvg /dev/sda3’, once you press enter you should see a message saying the ‘volume group’ has been extended. Once extended enter ‘vgdisplay’ again to see that the ‘rootvg’ ‘volume group’ has indeed been extended such as here;

    — Volume group —
    VG Name rootvg
    System ID
    Format lvm2
    Metadata Areas 2
    Metadata Sequence No 19
    VG Access read/write
    VG Status resizable
    MAX LV 0
    Cur LV 8
    Open LV 8
    Max PV 0
    Cur PV 2
    Act PV 2
    VG Size 39.88 GB
    PE Size 32.00 MB
    Total PE 1276
    Alloc PE / Size 846 / 26.44 GB
    Free PE / Size 430 / 13.44 GB
    VG UUID tGM4ja-k6es-la0H-LcX6-1FMY-6p2g-SRYtfY

    You can see the ‘VG Size’ is as expected.

  9. Now we need to extend the ‘logical volume’, type ‘lvdisplay to show our ‘logical volumes’, you’ll see something like;— Logical volume —
    LV Name /dev/rootvg/var
    VG Name rootvg
    LV UUID NOP1jF-09Xt-LkX5-ai4w-Srqb-xGka-nYbI2J
    LV Write Access read/write
    LV Status available
    # open 1
    LV Size 3.00 GB
    Current LE 320
    Segments 3
    Allocation inherit
    Read ahead sectors auto
    currently set to 256
    Block device 253:2If we want to expand the /var file system from 3GB to 10GB then type ‘lvextend –L 10G /dev/rootvg/var’, now type ‘lvdisplay’ again, you’ll see the ‘logical volume’ has grown to 10GB;

    — Logical volume —
    LV Name /dev/rootvg/var
    VG Name rootvg
    LV UUID NOP1jF-09Xt-LkX5-ai4w-Srqb-xGka-nYbI2J
    LV Write Access read/write
    LV Status available
    # open 1
    LV Size 10.00 GB
    Current LE 320
    Segments 3
    Allocation inherit
    Read ahead sectors auto
    currently set to 256
    Block device 253:2

  10. Now the last thing we need to do is to grow the actual file system, this doesn’t have to use all of the newly added space by the way. Enter ‘df –h’ to show the current filesystems, here’s an example;Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/rootvg-root
    2.0G 1.4G 495M 74% /
    /dev/mapper/rootvg-home
    248M 124M 113M 53% /home
    /dev/mapper/rootvg-var
    3.0G 1.1G 1.8G 30% /var
    /dev/mapper/rootvg-usr
    3.0G 936M 1.9G 34% /usr
    /dev/mapper/rootvg-opt
    3.0G 811M 2.0G 29% /opt

If we want to expand the /var file system from 3GB to 10GB then type ‘resize4fs /dev/mapper/rootvg-var’ or lvresize -r -l +100%FREE /dev/mapper/rootvg-var , when you press enter the actual filesystem will grow, this may time time, enter ‘df –h’ once completed to check;

Filesystem            Size  Used Avail Use% Mounted on<br>
/dev/mapper/rootvg-root<br>
                      2.0G  1.4G  495M  74% /<br>
/dev/mapper/rootvg-home<br>
                      248M  124M  113M  53% /home<br>
/dev/mapper/rootvg-var<br>
                      9.88G  1.1G  8.2G  12% /var<br>
/dev/mapper/rootvg-usr<br>
                      3.0G  936M  1.9G  34% /usr<br>
/dev/mapper/rootvg-opt<br>
                      3.0G  811M  2.0G  29% /opt<br>

You’re now finished!

Leave a Reply