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 but could be required if all partitions are already used).

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

Additional Resources

Beyond the exam objectives, you can learn how to prioritize the devices used for swap partition.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4.00 out of 5)
Loading...
50 comments on “SYS: Add new partitions and logical volumes, and swap to a system non-destructively.
  1. pawel says:

    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

    • CertDepot says:

      I agree with your comment. However, if this can be useful, it’s definitively not a best practice.
      Furthermore, instead of using the dd command, you can even improve the file creation step:
      # fallocate -l 50M /tmp/swap.file
      It is much quicker and cleaner: blocks are pre-allocated but not initialized (no IO to the data blocks).

  2. hunter86_bg says:

    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.

  3. Piotr says:

    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?

    • CertDepot says:

      I would suggest the following steps:
      # mkdir /swap
      # fallocate -l 200M /swap/swap.file
      # mkswap /swap/swap.file
      # chmod 600 /swap/swap.file
      # swapon /swap/swap.file
      # cat /proc/swaps
      + /swap/swap.file swap swap defaults 0 0
      This was tested and worked on a CentOS 7.1 virtual machine.

      • Piotr says:

        I tried that on RedHat 7 and 7.1, CentOS 7 and still have the same issue “swapon: /swap/swap.file: swapon failed: Invalid argument” It works fine when I use dd method. Just wonder if this have anything to do with XFS.

        • Sam says:

          I took a quick look at this. It seem that the fallocate would work with most other file systems. The reasoning is in the way each is processed.

          from the swapon man file –snip–
          You should not use swapon on a file with holes. Swap over NFS may not work.

          Swapon automatically detects and rewrites swap space signature with old software suspend data (e.g S1SUSPEND, S2SUSPEND, …). The problem is that if we don’t do it, then we get data corruption the next time an attempt at unsuspending is made.
          –end snip–

          which unfortunately, if you read the man file, is the way that fallocate works!!! for more information see the man files.

  4. lixinchina1 says:

    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?

    • CertDepot says:

      Without being completely sure, I would say that during the exam you play with a VM in a KVM host. Additional space comes from some unused space already allocated to the VM, totally free or part of an existing partition.

  5. lixinchina1 says:

    That makes sense; thx CertDepot

  6. samuel.sappa says:

    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

    • CertDepot says:

      If you want to reduce the risks, you should run a fsck command on the file system. I don’t know any additional precaution on this subject.

    • twostep says:

      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.

  7. Linuxpro says:

    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.

    • CertDepot says:

      There are several problems that you can get:
      – no more available primary partitions => replace the last primary partition with an extended partition and add logical partitions
      – no more available space => try to shrink the swap partition (if it exists) or one of the other partitions (avoid /)
      This kind of task is one of the most complicated tasks (depending how it is presented) of the RHCSA exam.

      • Linuxpro says:

        Thank you….I am going for it, I got my ldap down. I did not do it last time because I was unable to start my graphical. Now I know how to, just that partition problem is what I don’t have the solution to. But I will expect to see if the actual partition they have on the system have been used also.
        I learned a lot from here, thanks.

        • CertDepot says:

          You’re welcome.

          • Linuxpro says:

            Job done…Boss, I passed my exam last Friday with satisfaction … Thank you and I killed that partition and this time my ldap client was running good and autofs was just a piece of cake.
            CertDepot, you are the best and thanks.

          • CertDepot says:

            Congratulations!

          • Linuxpro says:

            Thank you so much…but where do I go from here…

          • CertDepot says:

            What do you mean?

          • Linuxpro says:

            Hello, sorry for the later reply I have been working on my resume as RHCSA. But I don’t know how to answer the question “tell me about your experience as a LINUX ADMIN” (I put 3 years of experience on my resume). No company wants a non experience admin. Please what are the basic THINGS I SHOULD NAIL DOWN AND IT WILL HELP ME A LOT WHEN I’LL GET A JOB.

          • CertDepot says:

            If you’ve got no admin experience, you should get involved in an Open Source project and start using tools like Ansible (very useful for your future career). This way, you will be able to show on your CV that got already some interesting experience.
            I perfectly understand that it’s not easy. With your RHCSA you showed that you could learn and stay focus on an target. Now, you have to show that you can bring added value to a company. At this time, automation is hot (Ansible, Chef, Puppet, Salt, etc). Providing some proof of your skills (like a personal git repository) should help you get your foot in the door.

          • Linuxpro says:

            Thank you so much I am working on the web server, after I will do puppet.

          • CertDepot says:

            You’re welcome.

          • Lisenet says:

            Check Red Hat Satellite 6 (combination of Foreman, Puppet, Katello, Pulp and Candlepin), it’s a beast, and looks good on your CV.

          • hacwordix says:

            You said you killed the partition, how were you able to resolve the issue with no space?

          • madunix says:

            hi CertDepot, about the above comment, is it the right way to start a gui in the exam to hit: # startx or # init 5 of course after installing the gnome packages if it were not found!! So there is any other restrictions in the exam in starting the gui interface regarding to ldap configuration. Thanks in advance

          • CertDepot says:

            startx or init 5 are not used in RHEL 7 anymore. You execute: # systemctl isolate graphical.target to start the gui.
            However, the gui is not at the heart of the exam but mainly the command line interface.
            You should learn the cli and for specific tasks fall back to the gui only if you can’t find any other solution.

  8. reaz_mahmood says:

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

    • CertDepot says:

      I think creating a second swap partition can be useful.
      Also, I don’t remember that you can expand an existing swap partition. I think you can only drop it and recreate it bigger.

      • Lisenet says:

        Agree with CertDepot, it can be useful if you need to temporarily expand your swap space, it’s easier to add a separate disk/volume to swap space rather than messing around the existing one.

        However, if your swap partition is an LVM volume, you can extend it (no need to drop it).

  9. hacwordix says:

    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. I was unable to create partition from fdisk cmd. fdisk anv pvs show there was no space. I know all the steps 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.

    • CertDepot says:

      Did you try to create an extended partition?

      • hacwordix says:

        Fdisk didn’t allow me to create any partition. I am trying to recreate the problem and see if I can get an alternative, I vaguely remember one of the lvm partition’s having a couple of hundred mb space, but is it possible to create a new partition with that space, like doing a lv resize, vgresize or pvresize or the existing data? I really need help in this …

        • Lisenet says:

          You can shrink an ext4 partition, but not an xfs.

          • hacwordix says:

            What I will be able to shrink is only lvm, but not the vg nor pv I guess.

          • Lisenet says:

            Why would you want to shrink a volume group? Free disk space for a NEW logical volume is all that you need.

          • Sam says:

            In addition to Lisenet, xfs is the default on Centos7.x.
            Use and know tools pvdisplay, vgdisplay and lvdisplay, to work out if there is any space available. I would suggest creating a LVM with all steps including the Physical Volume as a manual step. Have at least 2 versions of each (ie the Physical Volume)

            Other tools to lookup, can display the partition table in an easy to use manner: cfdisk, gdisk, sfdisk.
            gdisk is used for the gpt tables.

          • Lisenet says:

            You missed the most important tool I guess, parted.

          • Sam says:

            yeap there’s always one. good call

  10. rb51 says:

    hi all,

    just wondering whether you guys could share a thought about the following, which has been kind of discussed before:

    In the RHCSA exam when asked to deal with LVM or swap partition and there is no space available on existing VGs or existing partitions or new storage, how would you guys normally tackle the issue?

    I’ve tried to shrink an ext3/ext4 formatted partition, using e2fsck/resize2fs but can only achieve “creating/freeing up” space by deleting the partition and re-creating another partition(s) again. Is this the correct way of solving the issue?

    I have read the following RedHat KBs:

    How to Shrink an ext2/3/4 File system with resize2fs: https://access.redhat.com/articles/1196333

    How to Resize a Partition using fdisk: https://access.redhat.com/articles/1190213

    I have tried to use parted utility but could not find the “resize” command. Likewise the cfdisk utility was not of help.

    I am taking the exam on Friday, so would like to know what you guys can suggest.

    I would appreciate your comments.

  11. Ph.Bryab says:

    Hi CD,

    I was concern about “no space available” just in case this error encountered in exam, is it ok to use swap partition to solve the question?

    Thanks

Leave a Reply

Upcoming Events (Local Time)

There are no events.

RHCSA7: Task of the day

Allowed time: 5 minutes.
Add 100MB of swap space to the machine using a new logical volume.

RHCE7: Task of the day

Allowed time: 3 minutes.
Configure your machine to be a router.

Follow me on Twitter

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 ...