Monday, September 23, 2013

Creating a Swap Partition In Linux

Creating a Swap Partition In Linux


Step 1 ) Create the required partition (say 9 partition)

[root@client1 ~]# fdisk -l

[root@client1 ~]# fdisk /dev/hda
:p
:n
:t  { to change the partition ID}
:82 { for swap partition }
:w  { save & exit }
[root@client1 ~]#
[root@client1 ~]# partprobe /dev/hda

[root@client1 ~]# fdisk –l

Step 2 ) Make the partition as swap

[root@client1 ~]# mkswap /dev/hda9

To see the status

[root@client1 ~]# swapon –s

To on the swap partition

[root@client1 ~]# swapon /dev/hda9

Again  see the status

[root@client1 ~]# swapon -s

To put Off the swap partition

[root@client1 ~]# swapoff /dev/hda9

[root@client1 ~]# swapon –s






Logical Volume Manager


To view the Hard disk name and partition information

[root@client24 ~]# fdisk -l

Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         523     4096575   83  Linux
/dev/hda3             524         778     2048287+  83  Linux
/dev/hda4             779        4865    32828827+   5  Extended
/dev/hda5             779         905     1020096   83  Linux
/dev/hda6             906        1032     1020096   83  Linux
/dev/hda7            1033        1097      522081   82  Linux swap / Solaris
/dev/hda8            1098        2314     9775521   83  Linux


Create 3 partitions

[root@client24 ~]# fdisk /dev/hda

The number of cylinders for this disk is set to 4865.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (2315-4865, default 2315):
Using default value 2315
Last cylinder or +size or +sizeM or +sizeK (2315-4865, default 4865): +200M

Command (m for help): n
First cylinder (2340-4865, default 2340):
Using default value 2340
Last cylinder or +size or +sizeM or +sizeK (2340-4865, default 4865): +200M

Command (m for help): n
First cylinder (2365-4865, default 2365):
Using default value 2365
Last cylinder or +size or +sizeM or +sizeK (2365-4865, default 4865): +200M




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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

[root@client24 ~]# partprobe /dev/hda

[root@client24 ~]# fdisk -l

Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         523     4096575   83  Linux
/dev/hda3             524         778     2048287+  83  Linux
/dev/hda4             779        4865    32828827+   5  Extended
/dev/hda5             779         905     1020096   83  Linux
/dev/hda6             906        1032     1020096   83  Linux
/dev/hda7            1033        1097      522081   82  Linux swap / Solaris
/dev/hda8            1098        2314     9775521   83  Linux
/dev/hda9            2315        2339      200781   83  Linux
/dev/hda10           2340        2364      200781   83  Linux
/dev/hda11           2365        2389      200781   83  Linux

Create Physical Volumes

[root@client24 ~]# pvcreate /dev/hda9 /dev/hda10 /dev/hda11
  Physical volume "/dev/hda9" successfully created
  Physical volume "/dev/hda10" successfully created
  Physical volume "/dev/hda11" successfully created

To view Physical Volumes

[root@client24 ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/hda9
  VG Name               vg1
  PV Size               196.08 MB / not usable 4.08 MB
  Allocatable           yes (but full)
  PE Size (KByte)       4096
  Total PE              48
  Free PE               0
  Allocated PE          48
  PV UUID               5r8qvn-GF0k-NAfo-Rhqc-I3Qn-ZWws-zLCvks
 
  --- Physical volume ---
  PV Name               /dev/hda10
  VG Name               vg1
  PV Size               196.08 MB / not usable 4.08 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              48
  Free PE               21
  Allocated PE          27
  PV UUID               ys5Wd9-YiQ5-mM7c-sjrt-Mcwb-35oF-8mFyDW
 
  --- Physical volume ---
  PV Name               /dev/hda11
  VG Name               vg1
  PV Size               196.08 MB / not usable 4.08 MB
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              48
  Free PE               48
  Allocated PE          0
  PV UUID               5U81jh-Uddd-0giT-GYUT-pkvu-3MK3-KNkZJi
 
To create Volume Group

[root@client24 ~]# vgcreate vg1 /dev/hda9 /dev/hda10 /dev/hda11
  Volume group "vg1" successfully created

To display Volume Group Information.

[root@client24 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg1
  System ID          
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               576.00 MB
  PE Size               4.00 MB
  Total PE              144
  Alloc PE / Size       75 / 300.00 MB
  Free  PE / Size       69 / 276.00 MB
  VG UUID               P1zXt6-yBWW-SoUq-ZeF1-K7pf-Z69D-GVz8Up
 
To create logical Volume

[root@client24 ~]# lvcreate vg1 -L +300M -n lv1
  Logical volume "lv1" created





To view Logical Volume Information.

[root@client24 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg1/lv1
  VG Name                vg1
  LV UUID                ZvsfPh-Ve0c-y4Qa-VUYy-HbdR-lG3G-66703a
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                300.00 MB
  Current LE             75
  Segments               2
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0
 
[root@client24 ~]#

Format the Logical Volume

[root@client24 ~]# mkfs.ext3 /dev/vg1/lv1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
102400 inodes, 409600 blocks
20480 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
50 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

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

[root@client24 ~]# mkdir /mylvm

[root@client24 ~]# mount /dev/vg1/lv1 /mylvm

[root@client24 ~]# mount
/dev/hda3 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
/dev/hda5 on /home type ext3 (rw)
/dev/hda2 on /usr type ext3 (rw)
/dev/hda6 on /var type ext3 (rw)
/dev/hda8 on /dada type ext3 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/vg1-lv1 on /mylvm type ext3 (rw)


To resize the Logical Volume

[root@client24 ~]# lvresize -L +100M /dev/vg1/lv1

Extending logical volume lv1 to 400.00 MB
Logical volume lv1 successfully resized


[root@client24 ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/vg1/lv1
  VG Name                vg1
  LV UUID                ZvsfPh-Ve0c-y4Qa-VUYy-HbdR-lG3G-66703a
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                400.00 MB
  Current LE             100
  Segments               3
  Allocation             inherit
  Read ahead sectors     0
  Block device           253:0
 
[root@client24 ~]# cd /mylvm

[root@client24 mylvm]# ls
lost+found

[root@client24 mylvm]# touch file1 file2 file3

[root@client24 mylvm]# mkdir hyd sec

[root@client24 mylvm]# ls
file1  file2  file3  hyd  lost+found  sec

To remove logical Volume

[root@client24 ~]# cd

[root@client24 ~]# umount /mylvm

[root@client24 ~]# lvremove /dev/vg1/lv1

Do you really want to remove active logical volume "lv1"? [y/n]: y
Logical volume "lv1" successfully removed





[root@client24 ~]# fdisk /dev/hda

The number of cylinders for this disk is set to 4865.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (2390-4865, default 2390): +300M
Value out of range.
First cylinder (2390-4865, default 2390): w
First cylinder (2390-4865, default 2390):
Using default value 2390
Last cylinder or +size or +sizeM or +sizeK (2390-4865, default 4865): +300M

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@client24 ~]# partprobe /dev/hda


[root@client24 ~]# fdisk -l

Disk /dev/hda: 40.0 GB, 40020664320 bytes
255 heads, 63 sectors/track, 4865 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1          13      104391   83  Linux
/dev/hda2              14         523     4096575   83  Linux
/dev/hda3             524         778     2048287+  83  Linux
/dev/hda4             779        4865    32828827+   5  Extended
/dev/hda5             779         905     1020096   83  Linux
/dev/hda6             906        1032     1020096   83  Linux
/dev/hda7            1033        1097      522081   82  Linux swap / Solaris
/dev/hda8            1098        2314     9775521   83  Linux
/dev/hda9            2315        2339      200781   83  Linux
/dev/hda10           2340        2364      200781   83  Linux
/dev/hda11           2365        2389      200781   83  Linux
/dev/hda12           2390        2426      297171   83  Linux






[root@client24 ~]# pvcreate /dev/hda12

  Physical volume "/dev/hda12" successfully created

To extend the volume group.

[root@client24 ~]# vgextend vg1 /dev/hda12
  Volume group "vg1" successfully extended

[root@client24 ~]# vgdisplay

  --- Volume group ---
  VG Name               vg1
  System ID          
  Format                lvm2
  Metadata Areas        4
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                4
  Act PV                4
  VG Size               864.00 MB
  PE Size               4.00 MB
  Total PE              216
  Alloc PE / Size       0 / 0
  Free  PE / Size       216 / 864.00 MB
  VG UUID               P1zXt6-yBWW-SoUq-ZeF1-K7pf-Z69D-GVz8Up
 

Looking forward for your suggestions.. please feel free to write to me.. Your help would definitely help to improve our blog. Chetan Yadav

No comments:

Post a Comment