Wednesday, July 20, 2011

How to create and remove a linux logical volume and filesystem


Gather information

In this example we would like to add a 10G filesystem to the system volume group, so we need to determine if the volume group (system) has enough free space.
Check the status of the mounted filesystems
df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/system-root  10G  3.2G  6.9G  32% /                        
udev                    474M  184K  474M   1% /dev
/dev/sda1               102M   40M   63M  39% /boot
/dev/mapper/system-home_lv 2.0G  872M  1.2G  43% /home
Use the vgdisplay command to list the volume groups (VGs) and check the available space in the chosen VG.
vgdisplay
--- Volume group ---
  VG Name               system
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  16
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               233.66 GB
  PE Size               4.00 MB
  Total PE              59816
  Alloc PE / Size       5428 / 21.20 GB
  Free  PE / Size       54388 / 212.45 GB
  VG UUID               efvr7g-grgf5g-V0SP-45gdf-Xber-VKzj-fg5hf
The "Free PE / Size 54388 / 212.45 GB" line shows that we have 212.45 GB free in this volume group.

Create the logical volume

To create the logical volume use the lvcreate command as follows.
lvcreate -L 10G -n optlv system
Logical volume "optlv" created
This creates a 10GB logical volume named optlv in the volume group called system.

Create a reiserfs filesystem

If you require a reiserfs filesystem then follow this step else skip to the next step to create a ext3 filesystem.
(Reiser filesystems are often used on Suse linux).
mkfs.reiserfs /dev/system/optlv
mkfs.reiserfs 3.6.19 (2003 www.namesys.com)

A pair of credits:
BigStorage  (www.bigstorage.com)  contributes to our general fund  every month,
and has done so for quite a long time.

The  Defense  Advanced  Research  Projects Agency (DARPA, www.darpa.mil) is the
primary sponsor of Reiser4.  DARPA  does  not  endorse  this project; it merely
sponsors it.

Guessing about desired format.. Kernel 2.6.16.21-0.25-smp is running.
Format 3.6 with standard journal
Count of blocks on the device: 2621440
Number of blocks consumed by mkreiserfs formatting process: 8291
Blocksize: 4096
Hash function used to sort names: "r5"
Journal Size 8193 blocks (first block 18)
Journal Max transaction length 1024
inode generation number: 0
UUID: 571e5090-af78-4732-a058-c96596d829bc
ATTENTION: YOU SHOULD REBOOT AFTER FDISK!
        ALL DATA WILL BE LOST ON '/dev/system/optlv'!
Continue (y/n):y
Initializing journal - 0%....20%....40%....60%....80%....100%
Syncing..ok
ReiserFS is successfully created on /dev/system/optlv.

Create an ext3 filesystem

Use the mkfs.ext3 command to make an ext3 filesystem.
mkfs.ext3 /dev/system/optlv
mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1310720 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
80 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

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

Mounting the filesystem

Create the directory to mount the new filesystem.
mkdir /opt
Then mount the directory onto the new mount point.
mount /dev/system/optlv /opt
Check the status of the new filesystem.
df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/system-root  10G  3.2G  6.9G  32% /                         
udev                    474M  188K  474M   1% /dev
/dev/sda1               102M   40M   63M  39% /boot
/dev/mapper/system-home_lv 2.0G  872M  1.2G  43% /home                        
/dev/mapper/system-optlv 10G   33M   10G   1% /opt
Edit /etc/fstab to ensure that the new filesystem mounted on /opt is mounted at system boot.
This example is for a reiserfs. Change reiserfs in the table below to ext3 for an ext3 filesystem.
/dev/system/root     /                    reiserfs   acl,user_xattr        1 1
/dev/sda1            /boot                reiserfs   acl,user_xattr        1 2
/dev/system/swap     swap                 swap       defaults              0 0
/dev/system/home_lv /home                 reiserfs   acl,user_xattr        1 2
proc                 /proc                proc       defaults              0 0
sysfs                /sys                 sysfs      noauto                0 0
usbfs                /proc/bus/usb        usbfs      noauto                0 0
devpts               /dev/pts             devpts     mode=0620,gid=5       0 0
/dev/system/optlv    /opt           reiserfs   acl,user_xattr        1 2

Removing a filesystem and logical volume

Unmount the filesystem.
umount /opt
Then remove the logical volume. Removing the logical volume will also remove the filesystem
lvremove /dev/system/optlv
Do you really want to remove active logical volume "optlv"? [y/n]: y
  Logical volume "optlv" successfully removed
Then edit and remove the entry from /etc/fstab.
Then remove the mount directory.
rmdir /opt

No comments:

Post a Comment