0

LFCS filesystem & storage part 5

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

This is post nine of my LFCS series. This post is the fifth part of file system and storage. In it I will be discussing creating partitions and mounting file systems at boot time with fstab. For the first four parts see post 5post 6post 7 and post 8.

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 5

In this tutorial we will be setting up /dev/sde1 and /dev/sde2 so that they mount at boot time. Previously we used this drive to practice our SWAP skills in the previous article. We will now be splitting it up as a standard ext4 partition 5GB in size and another ext2 partition 2GB in size. We will then mount the partitions to points on the file system and putting some data into one of them. Then we will set them up to mount on boot before rebooting and confirm they still there after we log back in.

Use fdisk to prepare /dev/sde1 and /dev/sde2 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 0x244873bb.
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): 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +5G
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: 0x244873bb
Device Boot Start End Blocks Id System
/dev/sde1 1 654 5253223+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

[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 0x244873bb.
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): 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +5G

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: 0x244873bb

 Device Boot Start End Blocks Id System
/dev/sde1 1 654 5253223+ 83 Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[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): n
Command action
 e extended
 p primary partition (1-4)
1
Invalid partition number for type `1'
Command action
 e extended
 p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (655-1044, default 655):
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-1044, default 1044): +2G

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

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

Give /dev/sde1 and /dev/sde2 a file system ext4 and ext2 respectively:

[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
328656 inodes, 1313305 blocks
65665 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@centospractice ~]# mkfs.ext2 /dev/sde3
mke2fs 1.41.12 (17-May-2010)
Could not stat /dev/sde3 --- No such file or directory
[root@centospractice ~]# mkfs.ext2 /dev/sde2
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
131648 inodes, 526128 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
 32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

Now we need to create directories on the file system that we will mount the partitions to:

[root@centospractice ~]# mkdir /mnt/ext4disk1
[root@centospractice ~]# mkdir /mnt/ext2disk2

Now we need to add these partitions to the /etc/fstab file so that they are mounted automatically at boot time:

[root@centospractice ~]# cat << MOUNTER >> /etc/fstab
> # Mount /dev/sde1 and /dev/sde2 on boot.
> /dev/sde1 /mnt/ext4disk1 ext4 defaults,users 0 0
> /dev/sde2 /mnt/ext2disk2 ext2 defaults,users 0 0
> MOUNTER

I have added the users option as it means that any users who are part of the disk group are able to mount and unmount each of the drives.

We can add our testuser user to the disk group so that they can mount the drives.

[root@centospractice ~]# usermod -a -G disk testuser

Log in as the test user and mount both of the partitions:

[testuser@centospractice ~]$ mount /dev/sde1
[testuser@centospractice ~]$ mount /dev/sde2
[testuser@centospractice ~]$ mount |grep sde
/dev/sde1 on /mnt/ext4disk1 type ext4 (rw,noexec,nosuid,nodev)
/dev/sde2 on /mnt/ext2disk2 type ext2 (rw,noexec,nosuid,nodev)

Create a test file on one of the partitions, check that the file exists and then unmount the drive and confirm that the drive is back and the file you created is still there:

[root@centospractice ~]# echo "Test file on ext4disk1." > /mnt/ext4disk1/testfile.txt
[root@centospractice ~]# ll /mnt/ext4disk1/
total 20
drwx------. 2 root root 16384 Apr 28 22:28 lost+found
-rw-r--r--. 1 root root 24 Apr 28 22:46 testfile.txt
[root@centospractice ~]# cat /mnt/ext4disk1/testfile.txt
Test file on ext4disk1.
[root@centospractice ~]# umount /dev/sde1
[root@centospractice ~]# ls /mnt/ext4disk1/
[root@centospractice ~]#
[root@centospractice ~]# mount /dev/sde1
[root@centospractice ~]# ls /mnt/ext4disk1/
lost+found testfile.txt

Reboot and confirm that the drive and file is available:

[root@centospractice ~]# shutdown -r now
[root@centospractice ~]#
Broadcast message from root@centospractice.local
 (/dev/pts/0) at 22:49 ...

The system is going down for reboot NOW!

[root@centospractice ~]# mount |grep sde
/dev/sde1 on /mnt/ext4disk1 type ext4 (rw,noexec,nosuid,nodev)
/dev/sde2 on /mnt/ext2disk2 type ext2 (rw,noexec,nosuid,nodev)

[root@centospractice ~]# cat /mnt/ext4disk1/testfile.txt
Test file on ext4disk1.

Tune in tomorrow for the sixth and final part of my revision article on LFCS Filesystem & storage where we will discuss mounting Networked File Systems (NFS) and troubleshooting file system issues. Then we will move on to the articles covering Local System Administration.

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