In this video, Kevin Dankwardt describes and demonstrates formatting or making file systems for Linux file systems. He shows the Linux file system types ext4, XFS, and Brtfs using the mkfs commands.
- [Instructor] Hey, let's talk about formatting Partitions, that is putting a file system on a partition, or in Linux speak, making a file system. Alright, when we partition a disk, we just set aside some space but it doesn't have any structure to it, it doesn't have a format that Linux can understand for us to mount it into the tree of our file system, so we need to format it and there's lots of choices for kinds of file systems, but typically you do a mkfs -t and the type of file system that you want, ext4 is a really common one these days, and then you give it the device file that you want to format, this is a big deal, right, a typo there can really give you a bad day.
So let's say we're going to format the first partition on the second disk, that would be sdb1. It is possible to format an ordinary file, just have a file maybe full of binary zeros or something like that, and you can format and you can mount that as well, but that's, it's kind of unusual. Mounting a file system is essentially associating a directory with that file system. When that file system is on a device file, or a partition, then the mounting is essentially associating some directory with that partition.
Normally we would mount an empty directory, if there's something already in the directory then it's going to be hidden when we mount it. Now, with the mount you can say what type of file system you want to mount but Linux is generally smart enough to figure it out, so generally just mount device file in directory is enough. There's certain kinds of special file systems that are not on disk like a proc file system, sys file system, so forth, so then there's a little bit difference in text, and here we see the full command, mounting proc is type proc, and then instead of a device file, we put the word proc and then the directory where we want to mount it.
This is the usual case we mount the one and only proc file system on the directory /proc. Similarly the sys file system gets mounted on /sys. So those file systems don't correspond to any disk space, if we're using NFS, then the usual syntax is the server the name or the IP address colon, and the directory on the server that we want to mount somewhere. So the mount command understands that it's NFS when you do the colon, we could do a mount minus t NFS but not needed.
And then finally another kind of virtual file system besides proc and sysfs is the debug file system, and that's typically mounted on sys kernel debug, but like the others file systems, you could mount it as simply cells if you wanted. The mount command has a lot of options. One of the key configuration files related to mounting is etc fstab and the file system table. So for file systems that you want to have mounted automatically when you boot, you put entries in etc fstab, at bootup time the system typically does a mount -a, which means mount all the entries from etc fstab that are marked to be automatically mounted, you saw that I mentioned the -t before, so you can specify the type of file system with the mount command -t.
There's lots of extra options for the mount command, so the -o lets you list options that don't have special flags by themselves. For example the loop option is how you mount on a directory a file system that's in a file, and that file's already in a mounted file system. So if you format a file instead of a partition, then you can mount it with the loop option. A real handy thing to do on Linux is to mount .iso files on a directory, and you can use the loop option.
Maybe you want to mount something again with a different option. You can use the remount option and ro and rw are kind of handy, ro is read only and rw is read write. Maybe you want to remount a partition read only, you can do -o remount, ro, and the partition that you want to remount. An interesting feature of mount is this --bind. That lets you mount some part of the file system into some other part of the file system.
So you kind of have it in two places, and that's different than a link, that shows up, you know, in the kernel as a whole new place mounted. There are special options for mounting different types of file systems, if you look at the documentation for file system types, you might see that they have special options for things too. You unmount a file system with the umount command, no n at the beginning of umount. Now you can't unmount a file system if it's in use, usually, a file system would be in use if any of the files in the file system are open, so if you have some process running and it's using a file, then that'll prevent you to unmount a file system, like you can't unmount your USB stick if something has it open or if a process has, as its current directory a directory that's on that file system.
If you change directory over to a directory on a USB stick, then you can't unmount it. So what you need to do is make sure all the files are closed and all the processes have changed directory. If that's just a shell, maybe that's pretty easy to do, if that's some other process you might have to kill it. Unmounting will sync the disk, that is any pending Ios will get finished because the umount command terminates. That's kind of handy to know. That's why it's important to unmount a USB stick before you just yank it out, cause when you umount it, it will make sure that any writes that were done are actually saved out to the disk.
Linux kernel typically is configured to wait up to like 30 seconds before it writes something out the disk so if you, say, edited a file and you did a write and then you go yank out the USB stick, after a few seconds, there's a chance that the changes you've made have not yet been written to the disk. You can also do this -l to make the file system no longer appear in the tree but it's not really unmounted, it's just no one else, no other process, can start using it again.
And the -f is to force unmount for an NFS file system. If the NFS file system server dies, for example, it can be kind of a problem with NFS, sometimes things get hung. Alright, let's look at formatting, mounting, and unmounting. So let's look at formatting a partition, and mounting it, so, I have a disk with lots of partitions, sda here, and if I do a mount, and I grep sda, not that that sda is mounted, including partition 10.
So it's not mounted and in fact I know that I'm not using partition 10 so I'm going to format partition 10 and then I'm going to mount it somewhere. So to format it I'm going to do a mkfs, I'm going to do ext4, a nice good Linux file system, we say dev, sda10, and we double check, sda10, sda10, I really want to do that, that's not my system disk partition or anything, okay I convinced myself that there is no typo there.
Oh, no, just teasing you, that was fine, it's what I wanted to do, and it formatted pretty quick, this is a small partition and it's on a flash drive so that happened really fast actually. So now there's a file system in that partition and I can mount it, so we need a directory, and usually an empty directory, so we're just going to make an empty directory we'll call it tmp and we'll call it part10-dir, and then we're going to mount dev sda10, on that directory.
That seemed to work, and if we type mount, we see everything mounted and on the bottom there, we see dev sda10 on tmp part10-dir, and the default options so let's go to tmp part10-dir, in list we just got this lost and found thing that's the ext kind of file system place, putting the files that it doesn't know where it goes, when something bad happens, so we're just going to ignore that, and let's create a file. Let's go echo hello there and we'll create a file called afile and cat that, there we go.
Now let's unmount and mount. So I'm in tmp and I'm going to unmount part10-dir. There we go, now if I tried to do that again, it would tell me it's not mounted. And if we list what's in part10-dir, it looks empty, right, because the underlying directory that's in our regular file system is an empty directory.
When we mount the partition on the directory again, our file should reappear, so let's do the mount again. Then we go in there, and there's our file, right, because now this directory again is associated with that partition, if I were to mount that directory on a different partition, when I'd go into the directory I'd be looking at what was in that other partition.
- Partitioning storage
- Creating, mounting, and unmounting file systems
- Formatting file systems
- Making volumes with LVM
- Adding storage security
- Managing swap spaces
- Backing up and recovering Linux storage systems
- Working with networked file systems like NFS and SSHFS