SYS: Add new partitions and logical volumes, and swap to a system non-destructively.

Share this link

Note: This is an RHCSA 7 exam objective.

Presentation

You’ve got three options when adding some swap space to a system:

  • use a logical volume inside a volume group,
  • use a new partition,
  • use a file system (this is not a good solution performance-wise).

Logical Volume Configuration

If you decide to create a logical volume, follow these steps:
Create a logical volume (here called lv_swap with a size of 1G in the vg volume group):

# lvcreate --size 1G --name lv_swap vg

Prepare the swap logical volume:

# mkswap /dev/vg/lv_swap

Add the swap logical volume to the system:

# swapon /dev/vg/lv_swap

Choose one of these commands to check the result:

# swapon -s
# cat /proc/swaps

Edit the /etc/fstab file and add the following line (you can replace the beginning of the line with the UUID of the swap logical volume):

/dev/mapper/vg-lv_swap swap swap defaults 0 0

Note: to remove the swap logical volume, remove the line previously created in the /etc/fstab file and type:

# swapoff /dev/vg/lv_swap
# lvremove /dev/vg/lv_swap

Partition Configuration

If you decide to create a new partition, follow these steps:
Create a new partition with fdisk (here on the /dev/vda disk):

# fdisk /dev/vda

At the fdisk prompt, type ‘c‘, then ‘u‘ and finally ‘p‘ to print the partition table.
Still at the fdisk prompt, type ‘n‘ to create a new partition, type the partition number, the first sector and the partition size.
Don’t forget to give the swap type to this partition: press ‘t‘, then the partition number, then ‘82‘.
Exit the fdisk prompt with the ‘w‘ to write the partition table on disk.

Ask the kernel to read again the partition table (where X is the number of the swap partition):

# partprobe /dev/vdaX

Prepare the swap partition:

# mkswap /dev/vdaX

Add the swap partition to the system:

# swapon /dev/vdaX

Choose one of these commands to check the result:

# swapon -s
# cat /proc/swaps

Edit the /etc/fstab file and add the following line (you can replace the beginning of the line with the UUID of the swap partition):

/dev/vdaX swap swap defaults 0 0

Note: to remove the swap partition, remove the line previously created in the /etc/fstab file and type:

# swapoff /dev/vdaX
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Leave a Reply

33 Comments on "SYS: Add new partitions and logical volumes, and swap to a system non-destructively."

Notify of
Sort by:   newest | oldest
pawel
Member
pawel

Alternatively, if you do not have a spare partition for swap, you can always use a file on existing one:

1. Create an empty file on e.g. /mnt/ partition (size 50MB):

dd if=/dev/zero of=/mnt/swap.file bs=1M count=50
chmod 0600 /mnt/swap.file

2. Set up a Linux swap area on that file:

mkswap /mnt/swap.file

3. Add an entry to your /etc/fstab file:

/mnt/swap.file swap swap 0 0

4. Enable newly added swap area:

swapon -a

5. Check if the system is using it:

swapon -s

hunter86_bg
Member

I can only add that “mkswap” command has a “-L” option with which you can LABEL your partition (or file), which can be later used in “/etc/fstab”. Although the swap file is not the best practice, it might come in handy, when you have some free space on a partition, but you cannot shrink (not in lvm), nor have the time to backup and use the whole space.

Piotr
Member
Piotr

I am trying to create a swap file:
mkdir /swap
fallocate -l 200M /swap/swap.file
mkswap /swap/swap.file

but when I try to enable swapping I’ve got:

swapon: /swap/swap.file: swapon failed: Invalid argument

What am I doing wrong?

lixinchina1
Member
lixinchina1

Hi, thanks for all the great info. I saw from other websites few people discussed that they do not see extra disk or partition when it comes down to extend lv or swap, which makes me a bit worried. When I practice I have a storage server that can always quickly assign disk to my Linux host. May I know in the real exam where the disk space comes from?

lixinchina1
Member
lixinchina1

That makes sense; thx CertDepot

samuel.sappa
Member
samuel.sappa

I had an experience when reducing the file system using lvreduce and then when the system rebooted, my system was in error, with error message missing superblock, is there any save way to reduce the filesystem without causing this error ?
(note I reduce the home dir not the root dir)
Thank You for Your Info

twostep
Member
twostep

If you obtained the error:
“wrong fs type, bad option.bad superblock…”,
the cause is that you did not reduce filesystem via command: “resize2fs /path size” before you did lvreduce.

Linuxpro
Member
Linuxpro

Hello, I took the exam. However, I was unable to do the swap partition and to create a new LVM despite the fact I know all the steps. This was due to the fact that I was unable to create partition from fdisk cmd. There was not space, that is what the system told me. I know all the step but I just did not figure that out… I will be taken the exam on Friday. I need to fix this issue before I go back. Thank you.

reaz_mahmood
Member
reaz_mahmood

Does it make any sense to make two swap partitions? or we are supposed to expand existing one rather than creating a new one?

wpDiscuz

RHCSA7: Task of the day

Allowed time: 5 minutes.
Configure a cron task to write the uptime at 2PM every day.

RHCE7: Task of the day

Allowed time: 15 minutes.
Configure a httpd server with a password protected directory under the /var/www/html/private directory.

Poll for favorite RHEL 7 book

What is your favorite RHEL 7 book to prepare RHCSA & RHCE exams?

View Results

Loading ... Loading ...

Poll for most difficult RHCSA 7 topic

What do you think is the most difficult RHCSA 7 topic?

View Results

Loading ... Loading ...

Poll for most difficult RHCE 7 topic

What do you think is the most difficult RHCE 7 topic?

View Results

Loading ... Loading ...

Recent Comments