ext4

The ext4 filesystem is the 4th version of ext file systems and adds some improvements over ext3: use of extents (as opposed to 512 byte OS blocks), pre-allocation, delayed allocation, stripe-aware allocation, capable of handling more and bigger files and directories, etc.

Creating an ext4 file system is simple provided the disk has already been labeled and partitioned:

# mkfs -­t ext4 /dev/sda1
mke2fs 1.42.9 (28­Dec­2013)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
124928 inodes, 498688 blocks
24934 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
61 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

# mkdir /ext4

# mount /dev/sda1 /ext4/

# df ­-h /ext4/
Filesystem      Size      Used    Avail   Use%   Mounted on
/dev/sda1       464M      2.3M     433M     1%   /ext4
We can look at the configuration file /etc/mke2fs.conf to see what the default values are for newly created ext4 file systems (and ext3). Most of the options will rarely need changing. If we explicitly set any of the them, it will probably be stride and stripe-width.
# mkfs -t ext4 -E stride=16,stripe-width=64 /dev/sda2

The command above would create an ext4 filesystem meant to be included in a RAID or LVM setup. We set the stride size to 16KB and stripe-width to 64KB so we’re dealing with a group of 4 disk drives. There are 2 additional options we might use:

# mkfs -t ext4 -b 4096 -D /dev/sda3

The -b option sets the OS block size to a value (1024, 2048 or 4096) other than its default of 512 bytes. The -D flag indicates that I/O on this filesystem should not use the OS buffer and use direct I/O instead. If our needs are a bit more esoteric than that we should check man mkfs.ext4 for an exhaustive list of options.

We can use the commands tune2fs or dumpe2fs to check the characteristics of an ext4 file system:

root:~> tune2fs ­-l /dev/sdb
tune2fs 1.42.9 (28­Dec­2013)
Filesystem volume name:           EXT4
Last mounted on:                  <not available>
Filesystem UUID:                  92b72a59­9813­4406­aa66­ab140ead30a7
Filesystem magic number:          0xEF53
Filesystem revision #:            1 (dynamic)
Filesystem features:              has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:                 signed_directory_hash
Default mount options:            user_xattr acl
Filesystem state:                 clean
Errors behavior:                  Continue
Filesystem OS type:               Linux
Inode count:                      65536
Block count:                      262144
Reserved block count:             13107
Free blocks:                      249189
Free inodes:                      65525
First block:                      0
Block size:                       4096
Fragment size:                    4096
Group descriptor size:            64
Reserved GDT blocks:              127
Blocks per group:                 32768
Fragments per group:              32768
Inodes per group:                 8192
Inode blocks per group:           512
Flex block group size:            16
Filesystem created:               Sun Apr 5 11:56:14 2015
Last mount time:                  Sun Apr 5 11:56:41 2015
Last write time:                  Sun Apr 5 12:16:44 2015
Mount count:                      1
Maximum mount count: ­             1
Last checked:                     Sun Apr 5 11:56:14 2015
Check interval:                   0 (<none>)
Lifetime writes:                  33 MB
Reserved blocks uid:              0 (user root)
Reserved blocks gid:              0 (group root)
First inode:                      11
Inode size:                       256
Required extra isize:             28
Desired extra isize:              28
Journal inode:                    8
Default directory hash:           half_md4
Directory Hash Seed:              368f137a­36be­4932­850c­74c024b671f5
Journal backup:                   inode blocks
root:~> dumpe2fs -h /dev/sdb1
dumpe2fs 1.42.12 (29­Aug­2014)
Filesystem volume name:           TMP
Last mounted on:                  /tmp
Filesystem UUID:                  92b72a59­9813­4406­aa66­ab140ead30a7
Filesystem magic number:          0xEF53
Filesystem revision #:            1 (dynamic)
Filesystem features:              has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:                 signed_directory_hash
Default mount options:            user_xattr acl
Filesystem state:                 clean
Errors behavior:                  Continue
Filesystem OS type:               Linux
Inode count:                      65536
Block count:                      262144
Reserved block count:             13107
Free blocks:                      249189
Free inodes:                      65525
First block:                      0
Block size:                       4096
Fragment size:                    4096
Reserved GDT blocks:              273
Blocks per group:                 32768
Fragments per group:              32768
Inodes per group:                 8016
Inode blocks per group:           501
Flex block group size:            16
Filesystem created:               Wed Apr 29 21:43:41 2015
Last mount time:                  Sat Sep 26 17:29:30 2015
Last write time:                  Sat Sep 26 17:29:30 2015
Mount count:                      20
Maximum mount count: ­             1
Last checked:                     Wed Apr 29 21:43:41 2015
Check interval:                   0 (<none>)
Lifetime writes:                  6253 MB
Reserved blocks uid:              0 (user root)
Reserved blocks gid:              0 (group root)
First inode:                      11
Inode size:                       256
Required extra isize:             28
Desired extra isize:              28
Journal inode:                    8
First orphan inode:               40
Default directory hash:           half_md4
Directory Hash Seed:              092a9ce8­4a7c­4fc6­b6cd­46d17c718a41
Journal backup:                   inode blocks
Journal features:                 journal_incompat_revoke
Journal size:                     128M
Journal length:                   32768
Journal sequence:                 0x000110dd
Journal start:                    8227

The tune2fs command can also be used to change some of the parameters above (check man tune2fs):

# tune2fs -­E stride=16,stripe­width=64 /dev/sdb4              → change stride & stripe­size
# tune2fs ­-L OSVAR /dev/sda4                                 → relabel the filesystem
# tune2fs ­-U “d4efcfa2­439f­4809­abb5­c5cab4d2ab3e” /dev/sdc2    → reset the UUID
# tune2fs ­-U random /dev/sdd3                                → regenerate the UUID

We can shrink an ext4 filesystem but first we have to umount it and run e2fsck on it:

# df /ext4
Filesystem    Size     Used    Avail   Use%   Mounted on
/dev/sdb      976M     2.6M     907M     1%   /ext4

# umount /ext4

# e2fsck ­-f /dev/sdb
e2fsck 1.42.9 (28­Dec­2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
EXT4: 11/65536 files (0.0% non­contiguous), 12955/262144 blocks

# resize2fs /dev/sdb 900m
resize2fs 1.42.9 (28­Dec­2013)
Resizing the filesystem on /dev/sdb to 230400 (4k) blocks.
The filesystem on /dev/sdb is now 230400 blocks long.

# mount /dev/sdb /ext4/

# df -­h /ext4
Filesystem    Size     Used    Avail   Use%   Mounted on
/dev/sdb      852M     2.6M     789M     1%   /ext4

We can also grow it with or without the size parameter. If it is absent, the file system is grown as much as the underlying device allows:

# umount /ext4

# e2fsck -­f /dev/sdb
e2fsck 1.42.9 (28­Dec­2013)
Pass 1: Checking inodes, blocks, and sizes
[…]
EXT4: 11/65536 files (0.0% non­contiguous), 12955/230400 blocks

# resize2fs /dev/sdb
resize2fs 1.42.9 (28­Dec­2013)
Resizing the filesystem on /dev/sdb to 262144 (4k) blocks.
The filesystem on /dev/sdb is now 262144 blocks long.

# mount /dev/sdb /ext4

# df /ext4
Filesystem    Size     Used    Avail   Use%   Mounted on
/dev/sdb      976M     2.6M     907M     1%   /ext4

When it comes to backups, we have 2 tools we can use: dump and snapper. The dump utility can be used to backup ext2/3/4 filesystems but does not work with XFS. The snapper tool takes snapshots of LVM volumes (on top of ext4/xfs) or BTRFS filesystems. We will cover the snapper tool later on but let’s now see how dump works.

The dump command can perform backups on mounted filesystems as long as they are not the OS root filesystem (which needs to be unmounted to be backed up). This first example dumps all the contents of /dev/sdb2 (full backup with -0) onto the given file (-f), using bzlib compression (-j) and updating afterwards the contents of the file /etc/dumpdates (-u):

dump -0uj -f /backup/21-09-2015-server1-home.dmp /dev/sdb2

The next example performs an incremental dump (-1) of the /var onto tape (-f /dev/st0) using the zlib compression library (-z) and updates /etc/dumpdates :

dump -1uz -f /dev/st0 /var

This last example takes a full backup of the given filesystem and dumps it onto a file in a remote server through SSH:

dump -0uj -f – /home | ssh backup@backupserver “cat >> /backup/21-09-2015-server1-home.dmp”

If we need to restore the backed up volume above we just need to execute:

cd /home

# restore -rf /backup/21-09-2015-server1-home.dmp      -> if dump is local

# ssh  backup@backupserver “cat /backup/21-09-2015-server1-home.dmp” | restore -rf –     -> if dump is remote

To perform recovery and maintenance of ext2/3/4 filesystems we should unmount them and use e2fsck (check man e2fsck).

If you need a more in-depth dive into the specifics of ext4 I would suggest two starting points:

https://en.wikipedia.org/wiki/Ext4

https://www.kernel.org/doc/Documentation/filesystems/ext4.txt


<<Disk partitioning          Filesystem xfs >>