Operating System - Linux
1753878 Members
7233 Online
108809 Solutions
New Discussion юеВ

how to expand disk space on linux

 
SOLVED
Go to solution
KathyK
Regular Advisor

how to expand disk space on linux

Hi all,

I'm very new to Linux. We have a drive on our linux box that is nfs mounted to the hp boxes. the drive is getting full and since there was not a volume group created on it, need to know if there is a way to expand it at this poing could we create a volume group and add that disk to it at this time?

here is the information I get on the system:


pvdisplay -v /dev/sda1
Using physical volume(s) on command line
No physical volume label read from /dev/sda1
Failed to read physical volume "/dev/sda1"



/dev/sda1 258033560 229383152 15542980 94% /vol


your help is very much appreciated since this is becoming an urgent situation.



12 REPLIES 12
Jayan Nair
Frequent Advisor

Re: how to expand disk space on linux

Hi
You have only one disk and that is full.
add one more disk and create
1. physical volume:
pvcreate /dev/sda1
pvcreate /dev/sdb1
2. create volume group
vgcreate VolGroup00 /dev/sda1 /dev/sdb1
3. create logical volume
lvcreate -L 10G /dev/VolGroup00/LogVol01
lvcreate -L 10G /dev/VolGroup00/LogVol02

T
kelvinlnx
Advisor

Re: how to expand disk space on linux

Becareful... pvcreate /dev/sda1 will destroy your data.
1. Backup your data
2. Add a new drive
3. pvcreate /dev/sda1
4. pvcreate /dev/sdb1 (assuming the new drive's been partitioned... etc)
5. Create your volume group using vgcreate
6. Create your logical volume using lvcreate
7. Create a filesystem with mkfs.ext3
8. Edit your /etc/fstab
9. Mount it and restore your backup.
Ralph Grothe
Honored Contributor
Solution

Re: how to expand disk space on linux

Hi Kathy,

it hasn't become fully clear to me if you want to change from old partition to LVM layout?

Since you applied the pvdisplay command unsuccessfully on /dev/sda1 this partition obviously isn't an LVM PV and thus can't be used to extend any volumes.
Unless you provide pvcreate with a whole disk but rather a dedicated partition (as we usually do) this partition needs to be marked as being of type 0x8e by some tool like fdisk.
Let me show you on one of my boxes.
Either the pvscan or pvs command will list all valid LVM PVs on the system
(to be more precise, only those which aren't exempt by an "r" filter in lvm.conf which sometimes is necessary if you e.g. use DRBD devices)

[root@nagsaz:~]
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vgrh lvm2 a- 9.62G 560.00M
/dev/sda4 vgdata lvm2 a- 9.62G 7.38G

So for that to work both partitions /dev/sda[24] must have been assigned the 0x8e type flag, and as fdisk proves they have.

[root@nagsaz:~]
# fdisk -l /dev/sda

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 33 265041 83 Linux
/dev/sda2 34 1289 10088820 8e Linux LVM
/dev/sda3 1290 1353 514080 82 Linux swap / Solaris
/dev/sda4 1354 2610 10096852+ 8e Linux LVM


If you run the fdisk -l command on your system you will see if all cylinders are already allocated by some partition or if you have some still left free.
If the latter is the case then you can create a new partition to make up a new volume group.
To do this run fdisk without any options but only the appropriate disk device as argument.

BEWARE, BEFORE YOU CARRY ON BACKUP ALL YOUR DATA!

But the least you should do is backup the MBR of the disk to some safe place.
This can easily done like so

# dd if=/dev/sda bs=512 count=1 | ssh remuser@remhost dd of=/some/backupdir/host-xyz_sda.mbr

And to be on the safe side also capture an sfdisk dump

# sfdisk -d /dev/sda | ssh remuser@remhost 'cat >/some/backupdir/host-xyz_sfdisk_sda.txt'


Now it depends if you previously partitioned just using primary partitions or if your 4th and last viable partition is a so called extended partition (container).
In the latter case you can at heart's content create several partitions within this extended one which you can all give the 8e type flag (well there is an upper boundary of course)
If on the other hand you have all four possible primary partitions used up then you already reached the end.
However, if you have less than four partitions and cylinders still left you may as well create another primary partition and make it 8e.
Do the following (after the backup!)
press those keys while in fdisk:
"n" will create an new partition, and will ask you the offset for the starting cylinder.
If you previously partitioned sensibly without any gaps then you can safely press return and accept the offered default.
Next it will ask you for the last cylinder and default for the very last that is still available.
If that's ok again just press return.
If you however want to specify a size rather than an offset you may do so by typing e.g. "+4g" to make the partition roughly 4 GB big (there's usually some rounding deviation), provided the still free cylinders allow for this size. If not you will get an error and asked to retry anyway.
Next you must select the partition's type by pressing "t" and will be asked to which partition No. this should apply (note, fdisk contrary to usual IT convention starts numbering with 1).
So if you created the fourth partitions press 4.
Next you will be asked for the type code, which I already mentioned several times.
So type "8e" or if you are curiuos "l" to see a long list of partition types fdisk knows of.
Finally press "p" to get a display of your changes.
If all is acceptable press "w" to have the new partition table written and exited from fdisk. It is at this point where a restore from your backup might become a bailout.

Now there is a really annoying glitch with most RH kernels, and fdisk will probably emit such a warning.
The running kernel doesn't yet know of the new partition table and thus to carry on usually a reboot is required.

I once during a Linux Tag here in Berlin was so bold to ask RH kernel legend Alan Cox about this annoying "feature" after a talk of his, and even he couldn't give me a compelling reason for that and passed the blame to the LVM layer developers ;-)

So if you are ready reboot

# init 6

After the system is back you can now create your first PV,
so assuming we chose sda4 you would

# pvcreate /dev/sda4

Ther rest is the usal LVM stuff like

# vgcreate vgdata /dev/sda4

# vgs

# lvcreate -L 1g -n lv_something vgdata

# mke2fs -j /dev/vgdata/lv_something

# mkdir -p /mnt/somewhere

# mount /dev/vgdata/lv_something /mnt/somewhere

And later when you run out of space

# lvextend -L +512m /dev/vgdata/lv_something

# resize2fs /dev/vgdata/lv_something





Madness, thy name is system administration
KathyK
Regular Advisor

Re: how to expand disk space on linux

Thank you all for your help.

Ralph:

it hasn't become fully clear to me if you want to change from old partition to LVM layout?



Since you applied the pvdisplay command unsuccessfully on /dev/sda1 this partition obviously isn't an LVM PV and thus can't be used to extend any volumes.



[root@nagsaz:~]
# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vgrh lvm2 a- 9.62G 560.00M
/dev/sda4 vgdata lvm2 a- 9.62G 7.38G

So for that to work both partitions /dev/sda[24] must have been assigned the 0x8e type flag, and as fdisk proves they have.

[root@nagsaz:~]
# fdisk -l /dev/sda





thanks again.
KathyK
Regular Advisor

Re: how to expand disk space on linux

I have another thread open regarding ioscan, but since it's kind of related I ask here too.

first thing i got to do before adding the drive to a volume group is figure out which controller/bus it's on is there a way to do this without rebooting the system?

kind of like ioscan on hpux?

thank you
Ralph Grothe
Honored Contributor

Re: how to expand disk space on linux

Hi Kathy,



No, fdisk would only modify the partition table on the disk.
So if you somehow buggered up your partition table, and haven't yet done the following with the new partition(s): created new filesystems, MD Raid or swap devices, or used them as PV for LVM then no data should be touched.
That's why I advised you to run the "sfdisk -d" dump command before issuing the "w" write command in fdisk.
In such a case you could restore your old partition table by simply reading it from stdin.
e.g.

# ssh remuser@remhost cat /some/backupdir/host-xyz_sfdisk_sda.txt | sfdisk /dev/sda

or if you have the dump already on localhost more easily

# sfdisk /dev/sda < /some/localbackupdir/fdisk_sda.txt

This is also very handy where you need exactly identical partition layouts as when you mirror your root disks by SW MD raidtools (as we do on our boxes), or every time you need to replace a failed disk.
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: how to expand disk space on linux

>first thing i got to do before adding the >drive to a volume group is figure out which >controller/bus it's on is there a way to do >this without rebooting the system?

This very much depends on your hardware.
I already had cases where the vendor claimed that when I replace a failed internal scsi disk by plugging the new disk in the emptied slot the system should autodetect the new disk, and it actually didn't, so that I was forced to reboot for the kernel to become aware of the new disk so that the sfdisk command to write a partition table on it could be applied.

Sometimes one could force a bus rescan by issuing something like this (select the right HBA)

# echo "- - -" > /sys/class/scsi_host/host0/scan

But this isn't guaranteed to work everywhere.

>kind of like ioscan on hpux?

not really, but new devices should appear in the virtual procfs mounted as /proc

Most Linux tools for HW sensing/detection mainly read from /proc it appears to me.
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: how to expand disk space on linux

If you wish to put the OS of your running system onto LVM volumes this can be done almost entirely online, ok you require a final reboot to boot from the new layout.

Although there are hacks out there that show how this can be done, there is one thing you should avoid. Don't put your /boot on an LV because this can cause a lot of headaches when you need to run a bare metal recovery.
It is best to spare a small entirely separate standard linux partition (type 0x83) and mount /boot on that.
But there is no issue putting /, /var, /usr, /home etc. on separate LVM volumes.
In fact LVM gives you greater flexibility when you later need to online extend your /var or /.
Added to this you have free snapshot facility of all your LVs which is really a great thing for fast failback backups or to clone a living / and /var.
Madness, thy name is system administration
KathyK
Regular Advisor

Re: how to expand disk space on linux

thank you all, this gives me alot to work on.