0

LFCS filesystem & storage part 3

Welcome to post 22 of my 100 day challenge. Checkout my introduction for some background.

This is post seven of my LFCS series. This post is the third part of file system and storage. In it I will be discussing the creation of swap partitions and formatting partitions with file systems. For the first two parts see post 5 and post 6.

You can go back to the overview post for a brief introduction or take a look at post one for instructions on setting up the exam practice system which I will be using throughout this series. For the posts regarding the Linux Command Line see posts 2, 3 and 4.

This post is quite long so you might want to set a side some time to go through it properly.

LFCS filesystem & storage part 3

What is SWAP?

RAM on Linux is separated into chunks called pages. If a system is running low on memory it clears out inactive pages to a portion of the hard drive known as the SWAP space. Therefore when we talk about virtual memory in Linux systems this is the total amount of ram + total amount of swap space.

As mechanical spinning drives as slow this cannot be thought of as a replacement for buying more RAM. Although this approach can be beneficial for systems with a small amount of RAM.

SWAP should equal 2x physical RAM for up to 2 GB of physical RAM, and then an additional 1x physical RAM for any amount above 2 GB, but never less than 32 MB.

If you had a system with 2GB of RAM then you would create a 4GB SWAP partition. But for 3GB you would only need a 5GB SWAP partition.

Creating and configuring SWAP partitions

On Linux systems SWAP can either be a SWAP file or a dedicated SWAP partition. You can even mix both together. This article will walk you through the creation of a SWAP file followed by a dedicated SWAP partition.

Add a SWAP file

You should decide on a SWAP file size that reflects what we discussed above. We have 1024MB of RAM in our VM so we should therefore create a 2GB SWAP file. But for the purposes of this tutorial I will instead create a 64MB one to save on disk space. It will be deleted later.

Once we have the initial size we will multiply it by 1024 to get the block size for the file. In this case: 64 x 1024 = 65,536.

[root@centospractice ~]# dd if=/dev/zero of=/swap bs=1024 count=65536
65536+0 records in
65536+0 records out
67108864 bytes (67 MB) copied, 0.25778 s, 260 MB/s

Convert the new file we just created into a SWAP file:

[root@centospractice ~]# mkswap /swap
mkswap: /swap: warning: don't erase bootbits sectors
 on whole disk. Use -f to force.
Setting up swapspace version 1, size = 65532 KiB
no label, UUID=38e2e53f-3214-46f1-b912-4a28c104b172

Turn on the SWAP file:

[root@centospractice ~]# swapon /swap

If you want this SWAP file to be available the next time you boot you need to add it to your /etc/fstab file. Which you can do with the following command:

[root@centospractice ~]# cat << SWAP >> /etc/fstab
> # Add new swap file /swap to be automounted on boot
> /swap swap swap defaults 0 0
> SWAP

To check that the SWAP file is active you can use the following command:

[root@centospractice ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 835580 0 -1
/swap file 65532 0 -2

Now you can reboot and do the cat command above again to confirm that the SWAP file is still there and operational.

Add a SWAP partition

Firstly you need to make sure that you followed my tutorial for setting up a CentOS Practice System as I will be using this as a basis for the following tutorial.

We added 4 additional disks to our VM at creation time in order to utilise them for LVM. This means we need to add an additional disk to the system which we will use for both a SWAP partition and for configuring partitions for other file systems later in this tutorial.

So go ahead and create another disk for your VM, you can use the link to the test environment setup tutorial above. Your VM should be off when you create the additional disk. When you are ready it should appear to the lsblk command as /dev/sde. With it we will create our 2GB SWAP partition as our VM only has 1024MB RAM.

[root@centospractice ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sde 8:64 0 8G 0 disk

Use fdisk to create a 2GB partition /dev/sde1 on /dev/sde:

[root@centospractice ~]# fdisk /dev/sde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4fe8e82b.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 switch off the mode (command 'c') and change display units to
 sectors (command 'u').
Command (m for help): n
Command action
 e extended
 p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1044, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +2G
Command (m for help): p
Disk /dev/sde: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x4fe8e82b
Device Boot Start End Blocks Id System
/dev/sde1 1 262 2104483+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Once we have created the partition we can then turn the partition into a SWAP partition:

[root@centospractice ~]# mkswap /dev/sde1
Setting up swapspace version 1, size = 2104476 KiB
no label, UUID=696d7acd-cf58-4879-b525-41a8ce70f3f8

Then we can enable it:

[root@centospractice ~]# swapon /dev/sde1

This is all that is required to create and set-up the SWAP partition but if you want this partition to persist after a reboot you need to edit the /etc/fstab file to enable it to mount on boot:

[root@centospractice ~]# cat << SWAP >> /etc/fstab
> # Add new swap partition /dev/sde1 to be automounted on boot
> /dev/sde1 swap swap defaults 0 0
> SWAP

You can now look in /proc/swaps to see if the SWAP partition you have just created is working:

[root@centospractice ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 835580 0 -1
/dev/sde1 partition 2104476 0 -2

Now you can reboot and do the cat command above again to confirm that the SWAP partition is still there and operational.

One caveat when using SWAP is that you cannot modify the partition it is using whilst the operating system is utilising the SWAP space. This goes for filesystems and LVM volumes. You have to boot into a lower run level and choose not to mount the SWAP partition if you want to modify it. Therefore if you plan to increase your memory at any point in the future, it might be an idea to size the SWAP partition appropriately.

Formatting file systems

We have touched on this subject in our previous tutorial in part 2 when we discussed Logical Volume Management.

As an example we are going to nuke /dev/sde removing /dev/sde1 which is the swap file created earlier and our corresponding /etc/fstab entry so we can use the drive for other uses.

Firstly turn off the SWAP partition:

[root@centospractice ~]# swapoff /dev/sde1
[root@centospractice ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 835580 0 -1
/swap file 65532 0 -2

Now delete the following 2 lines from /etc/fstab:

# Add new swap partition /dev/sde1 to be automounted on boot
/dev/sde1 swap swap defaults 0 0

Now use fdisk to delete /dev/sde1:

[root@centospractice ~]# fdisk /dev/sde
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
 switch off the mode (command 'c') and change display units to
 sectors (command 'u').
Command (m for help): p
Disk /dev/sde: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x4fe8e82b
Device Boot Start End Blocks Id System
/dev/sde1 1 262 2104483+ 83 Linux
Command (m for help): d
Selected partition 1
Command (m for help): p
Disk /dev/sde: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 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: 0x4fe8e82b
Device Boot Start End Blocks Id System
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

Now we can create two new partitions on this disk /dev/sde1 and /dev/sde2 using the parted utility:

[root@centospractice ~]# yum install parted
Loaded plugins: fastestmirror
Setting up Install Process
Determining fastest mirrors
 * base: mirror.ukhost4u.com
 * extras: mirror.ukhost4u.com
 * updates: mirror.synergyworks.co.uk
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
updates/primary_db | 3.2 MB 00:02
Resolving Dependencies
--> Running transaction check
---> Package parted.x86_64 0:2.1-25.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
 Package Arch Version Repository Size
================================================================================
Installing:
 parted x86_64 2.1-25.el6 base 607 k
Transaction Summary
================================================================================
Install 1 Package(s)
Total download size: 607 k
Installed size: 2.2 M
Is this ok [y/N]: y
Downloading Packages:
parted-2.1-25.el6.x86_64.rpm | 607 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
 Installing : parted-2.1-25.el6.x86_64 1/1
 Verifying : parted-2.1-25.el6.x86_64 1/1
Installed:
 parted.x86_64 0:2.1-25.el6
Complete!
[root@centospractice ~]# parted /dev/sde
GNU Parted 2.1
Using /dev/sde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sde: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
(parted) mkpart primary 1 1024
(parted) mkpart primary 1025 2048
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sde: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
 1 1049kB 1024MB 1023MB primary
 2 1025MB 2048MB 1023MB primary
(parted) quit

Check the partitions you have just created:

sde 8:64 0 8G 0 disk
├─sde1 8:65 0 976M 0 part
└─sde2 8:66 0 975.6M 0 part

Format /dev/sde1 with the ext4 file system:

[root@centospractice ~]# mkfs.ext4 /dev/sde1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
62464 inodes, 249856 blocks
12492 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=255852544
8 block groups
32768 blocks per group, 32768 fragments per group
7808 inodes per group
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Format /dev/sde2 with the <> file system:

[root@centospractice ~]# mkfs.xfs /dev/sde2
meta-data=/dev/sde2 isize=256 agcount=4, agsize=62439 blks
 = sectsz=512 attr=2, projid32bit=0
data = bsize=4096 blocks=249756, imaxpct=25
 = sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0
log =internal log bsize=4096 blocks=1200, version=2
 = sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

Check that the partitions have the correct filesystem using parted:

/dev/sda1 477M 47M 405M 11% /boot
[root@centospractice ~]# parted /dev/sde
GNU Parted 2.1
Using /dev/sde
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sde: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Number Start End Size Type File system Flags
 1 1049kB 1024MB 1023MB primary ext4
 2 1025MB 2048MB 1023MB primary xfs

Tune in tomorrow for the fourth part of my revision article on LFCS Filesystem & storage where we will discuss file permissions.

Subscribe to my feed either by E-mail or by RSS to receive updates as they happen.

Can you improve on any of the tips I’ve discussed here? If you can let me know in the comments.

Jason Edwards