Thursday 2 April 2015

Adding new swap partition to centos VM without rebooting

This is the process that I used to add a new swap file to a centos vm today without rebooting them:

The new hard drive was added to the VM using the vmware client.

Ran the following command to see if the new drive has been found by Linux

# ls /dev/sd*

It showed the following output:

/dev/sda  /dev/sda1  /dev/sda2

This showed that the new drive had not been added as it would be shown as /dev/sdb.

Ran the following command to force Linux to add the new drive:

# ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done

I then ran:

# ls /dev/sd*

In now showed:

/dev/sda  /dev/sda1  /dev/sda2 /dev/sdb

I then created a new partition with fdisk by running:

fdisk /dev/sdb

I choose the following options:

Command (m for help): c
DOS Compatibility flag is not set
Command (m for help): u
Changing display/entry units to sectors

Disk /dev/sdb: 34.4 GB, 34359738368 bytes
255 heads, 63 sectors/track, 4177 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd1082b01

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4):

Partition number (1-4): 1
First sector (2048-67108863, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-67108863, default 67108863):
Using default value 67108863

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

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

I then checked to see if the new partition had been added:

# ls /dev/sd*

It showed the following output that showed the new partition had been added (/dev/sdb1):

/dev/sda  /dev/sda1  /dev/sda2 /dev/sdb  /dev/sdb1

I then ran the following command:

# mkswap /dev/sdb1

Which showed the following output:

Enable the swap partition for usage using swapon command as shown below.

I then ran the following command to enable the new swap partition:

# swapon /dev/sdb1

Which showed the following:

To make this swap space partition available even after the reboot, add the following line to the /etc/fstab file.

I then added the following line to /etc/fstab to enable the new swap partition at boot:

/dev/sdb1               swap                    swap    defaults        0 0

I then ran the following command to check that the new swap was present:

# swapon -s

It showed this output:

Filename                        Type            Size    Used    Priority
/dev/sda2                       partition       4192956 0       -1
/dev/sdc1                       partition       1048568 0       -2

The following comand was run to see the total amounts of Swap on the system:

# free -k

It showed:

             total       used       free     shared    buffers     cached
Mem:       3082356    3022364      59992          0      52056    2646472
-/+ buffers/cache:     323836    2758520
Swap:      5241524          0    5241524

That was the whole process.







No comments:

Post a Comment