Linux Disk Partition
3:47
The partition type is only meaningful for mbr partition tables. Remember, the mbr uses primary, extended, and logical partitions. Since we are formatting this using gpt, we’re just going to use primary as the partition type. The start point here is one mebibyte and the endpoint is five gibibytes. So our partition is essentially five gibibytes. Remember from the earlier course, that data sizes have long been referred to in two different ways, using the exact data measurement and the estimated data measurement. Remember that one kibibyte is actually 1,024 bytes while one kilobyte is 1,000 bytes. We haven’t really had to care about this distinction before. Some operating systems sometimes measure one kilobyte as 1,024 bytes which is confusing, but when dealing with data storage we want to make sure we’re using the precise measurements so we don’t waste precious storage space. Let’s opt to use mebibyte and gibibyte in our partition. Next, we’re going to format the partition with the file system using mkfs. So I’m just going to quick, sudo mkfs type is ext4. And I want to format the partition, so sdb1. We also left the rest of the disk unpartitioned because we’re going to use it for something else later. With that, we’ve created a partition and formatted a file system on a USB drive. Remember to always be careful when using the parted tool. It’s very powerful and if you modify the wrong disk on here it could cause a pretty big mess. Even though we’ve partitioned our disk and formatted a file system on here, we’re not actually able to start reading and writing files to it just yet. There’s one last step to get a usable disk in Linux. We have to mount the file system to a directory so that we can access it from the shell. Spoiler alert, you’ll learn how to do that in the next video.
In Linux, the dedicated area of the hard drive used for virtual memory is known as swap space. We can create swap space by using the new disk partitioning tools that we learned. A good guideline to use to determine how much swap space you need is to follow the recommended partitioning scheme in the next supplementary reading. In our case, since we just have a USB drive which doesn’t need swap, we’re just going to partition the rest of it as swap to show you how this works. In practice, you would create swap partitions for your main storage devices like hard drives and SSDs. Okay. Let’s make swap space. First, go back into the parted tool and select /dev/sdb, where our USB is. We’re going to partition it again this time to make a swap partition. And then we’ll format the Linux dash swap file system on it. So, mkpart primary Linux swap 5 gibibytes 100 percent. You’ll notice that the end point of the drive says 100 percent which indicates that we should use the rest of the free space on our drive. We’re not done yet. Swap isn’t actually a file system, so this command won’t be enough. I know I’m sorry, I just lied to you like five seconds ago. If you think about it, it makes a lot of sense since pages go into swap and not file data. Anyways, to complete this process, we need to specify that we want to make it swap space with the mkswap command. Let’s quit out of parted and run this command on a new swap partition. So, sudo mkswap dev, and our new swap partition is on dev sdb2. Finally, there’s one more command to run to enable swap on the device, swapon. So, sudo swapon dev sdb2. If we want to automatically mount swap space every time the computer boots up, just add a swap entry to the /etc fstab file like we did earlier.