Linux: add a new harddrive and create logical volumes

Linux: add a new harddrive and create logical volumes

Linux: add a new harddrive and create logical volumes 150 150 Roderick Derks

Is it your dream to install linux on your system? And you fullfilled that dream? And you used Linux and the space on the disk so much that you need to add e new hard drive?

Well.. this article will help you! 

Prepare the disk

[root@server root]# fdisk /dev/hdb

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1860, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1860, default 1860):
Using default value 1860

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/hdb: 15.3 GB, 15303075840 bytes
255 heads, 63 sectors/track, 1860 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1             1      1860  14940418+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

Create the Logical Volume

[root@server root]# pvcreate /dev/hdb1
pvcreate — physical volume "/dev/hdb1" successfully created

[root@server root]# vgcreate Volume01 /dev/hdb1
vgcreate — INFO: using default physical extent size 4 MB
vgcreate — INFO: maximum logical volume size is 255.99 Gigabyte
vgcreate — doing automatic backup of volume group "Volume01"
vgcreate — volume group "Volume01" successfully created and activated

[root@server root]# vgdisplay
— Volume group —
VG Name               Volume01
VG Access             read/write
VG Status             available/resizable
VG #                  0
MAX LV                256
Cur LV                0
Open LV               0
MAX LV Size           255.99 GB
Max PV                256
Cur PV                1
Act PV                1
VG Size               14.24 GB
PE Size               4 MB
Total PE              3646
Alloc PE / Size       0 / 0
Free  PE / Size       3646 / 14.24 GB
VG UUID               fFqAUY-0cFJ-GwCk-0Muf-0goc-Hwzc-ouBR4B

[root@server root]# lvcreate -L 14.24G Volume01 -n LogVol01
lvcreate — rounding size up to physical extent boundary
lvcreate — doing automatic backup of "Volume01"
lvcreate — logical volume "/dev/Volume01/LogVol01" successfully created

[root@server root]# umount /WERK/
umount: /WERK: device is busy
[root@server root]# init 2
[root@server root]# umount /WERK/

[root@server root]# vgextend Volume01 /dev/hdc1
vgextend — INFO: maximum logical volume size is 255.99 Gigabyte
vgextend — doing automatic backup of volume group "Volume01"
vgextend — volume group "Volume01" successfully extended

[root@server root]# vgextend Volume01 /dev/hdd1
vgextend — INFO: maximum logical volume size is 255.99 Gigabyte
vgextend — doing automatic backup of volume group "Volume01"
vgextend — volume group "Volume01" successfully extended

[root@server root]# vgdisplay
— Volume group —
VG Name               Volume01
VG Access             read/write
VG Status             available/resizable
VG #                  0
MAX LV                256
Cur LV                1
Open LV               0
MAX LV Size           255.99 GB
Max PV                256
Cur PV                3
Act PV                3
VG Size               52.01 GB
PE Size               4 MB
Total PE              13315
Alloc PE / Size       3646 / 14.24 GB
Free  PE / Size       9669 / 37.77 GB
VG UUID               fFqAUY-0cFJ-GwCk-0Muf-0goc-Hwzc-ouBR4B

[root@server root]# lvextend -l +9669 /dev/Volume01/LogVol01 /dev/hdc1
lvextend — extending logical volume "/dev/Volume01/LogVol01" to 52.01 GB
lvextend — not enough free/allocatable physical extents to extend logical volume  
"/dev/Volume01/LogVol01"

[root@server root]# lvextend -l +9669 /dev/Volume01/LogVol01 /dev/hdc1 /dev/hdd1
lvextend — extending logical volume "/dev/Volume01/LogVol01" to 52.01 GB
lvextend — doing automatic backup of volume group "Volume01"
lvextend — logical volume "/dev/Volume01/LogVol01" successfully extended

[root@server root]# vgdisplay
— Volume group —
VG Name               Volume01
VG Access             read/write
VG Status             available/resizable
VG #                  0
MAX LV                256
Cur LV                1
Open LV               0
MAX LV Size           255.99 GB
Max PV                256
Cur PV                3
Act PV                3
VG Size               52.01 GB
PE Size               4 MB
Total PE              13315
Alloc PE / Size       13315 / 52.01 GB
Free  PE / Size       0 / 0
VG UUID               fFqAUY-0cFJ-GwCk-0Muf-0goc-Hwzc-ouBR4B

[root@server root]# vi /etc/fstab
LABEL=/                 /                       ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
none                    /dev/pts                devpts  gid=5,mode=620  0 0
none                    /proc                   proc    defaults        0 0
none                    /dev/shm                tmpfs   defaults        0 0
/dev/hda2               swap                    swap    defaults        0 0

LABEL=/WERK             /WERK                   ext3    defaults        1 2

[root@server root]# mkfs -t ext3 /dev/Volume01/LogVol01
mke2fs 1.32 (09-Nov-2002)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
6818784 inodes, 13634560 blocks
681728 blocks (5.00%) reserved for the super user
First data block=0
417 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424

Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@server root]# e2label /dev/Volume01/LogVol01 /WERK
 

Create single partition (instead of creating a logical volume)

[root@server root]# mkfs -t ext3 /dev/hdb1

[root@server root]# mkdir /mnt/hdb1

[root@server root]# mount /dev/hdb1 /mnt/hdb1

[root@server root]# vi /etc/fstab

# # File: /etc/fstab # /dev/hdb1  /mnt/hdb1  ext3  defaults 1 2

 

Copy data to create a new mounting point

If you created a new volume (on a second disk) and you want this to be the new /opt directory:

Mount the newly created volume temporarily to /mnt:
mount /dev/VolGroup01/LogVol01 /mnt

Copy the data from /opt to the new disk:
rsync -avu /opt/ /mnt/

mv /opt /opt_old
mkdir /opt
umount /mnt

vi /etc/fstab
/dev/mapper/VolGroup01-LogVol01 /opt                    ext4    defaults        1 1

reboot or mount -a

if you need to copy data from a directory and exclude one directory:

find /source_dir/* -not -type d -and -not -regex '\./dir/youneed/to_exclude/.*' -exec echo mv {} /destination/ \;

find /mnt/opt/* -not -type d -and -not -regex '\./mnt/opt/splunk/.*' -exec echo mv {} /mnt/opt/splunk/var/ \;

Roderick Derks

Liefhebber van fietsen, van het oplossen van IT puzzels, en van het delen van informatie om anderen te helpen.

All stories by:Roderick Derks

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Your Name (required)

Your Email (required)

Subject

Your Message

Your Name (required)

Your Email (required)

Subject

Your Message