Adding a disk
- [Narrator] My little server here has an internal hard disk that so far we haven't done anything with. Let's take a moment and set that up so it can be used as storage by anyone or any service on the system. First, I need a way of referring to the disk that I'm planning to work with. All the disks that the system knows about will be assigned a name as they're discovered based on the type of connection, or bus, that they use to connect to the system. To see the disks that the system knows about, I can look inside the dev file system, or I can use the ls block command to show me block devices. That is devices that provide block storage all in one place. These loop file systems are the file systems in a file that snap uses. So we'll ignore those. What I'm looking for here on my system is serial disks, labeled sd and then a letter identifier. Here, I can see sdb, serial disk b, the second serial disk the system detected. Serial disks are what SATA and USB disks show up as. And this is my SSD on which I installed the system. I can see that there's two partitions here on this disk. Partitions are referred to by adding a number after the disk identifier. Partition one is my boot partition and partition two is my system partition. Other disks connected through other means will have different identifiers. For example, if the system had a nice fast NVME interface, an SSD attached to that would show up as NVME 0N1, or something similar to that. And on older systems, we'll see ID disks show up as HDA, HDB and so on. What I'm really looking for here is this entry, sda. This is my magnetic hard disk attached through SATA. My particular computer seems to detect SATA devices first and then the MSATA SSD that I'm using as the boot disk. That's why this is sda and the other one is sdb. If I removed the magnetic SATA disk, my boot disk would become sda. We'll talk more about that in just a little bit. But wait, you might say, how do I know this is the right disk? Well, it built the system in, I know, but we can also see which particular disk is associated with which particular name using the command sequence lshw - businfo - c disk. Here, I can see the model of my disks. Here's the Samsung SSD on STP and my ST disk, with its identifier sda. On the loading system, there's always more information to dig into and double checking yourself can be a really useful habit to get into. In order to use this disk to store files, we'll need to take a few steps. Right now, this disk is just a disk and we'll need to create a partition on it and within that partition, we'll need to create a file system. Then, we'll need to set the permissions of the file system so that, in this case, any user on the system can use it. So let's get started. SDA in this case, is my full disk, the block device. To create a partition on it, which will be sda1, in my case, I'll write fdisk dev sda. I should note here, that if you're following along with me, be absolutely certain that the disk you're going to be working with is the right one. Your system may have been installed on sda, for example, and you might have a second disk sdb. The opposite of what my situation is. Fdisk will modify the partition table of a disk without the ability to undo. And that makes it really easy to damage data if you select the wrong disk. Double check and then double check again every time you work with disks and partitions. Here in fdisk, I can press P to print the partition table of this disk. There's a GPT disk label, but no partitions. If there were partitions, such as if this disk was repurposed from another system, I could delete them using the command, D, and follow through with the little wizard to remove partitions. I'll warn you again here, that's a destructive operation and if those changes are written to the wrong disk, you might find yourself having a bit of a bad day. Okay, with no partitions on this disk, I'll make sure this disk is a GTPT disk with a GUID partition table using the G command. Then I'll create a partition using the N command for new. I want this partition to fill the whole disk, so I'll just accept the default suggestions here in this little wizard. If you wanted to make a different partition layout, you need to tell the system what size to make the first partition and then go through this process again to make a second. My disk here is only 500 gigabytes though, so that's already pretty small, and for my purposes, one partition is fine. Fdisk has recognized here that there was a file system on this disk before. I'll tell it to remove that because we won't use that one. Okay, I can press P and see what changes I've made. This is what I want, so I'll write this to the disk, committing the changes and modifying the partition table with the W command. Okay, now I'll run ls block again. And I can see within my disk, there's a new partition. Good. The next step we need to take is to add a file system to this partition so we can store files on it. I'll create an ext4 file system, though others are available. It can be interesting to experiment with different file systems, especially if you're curious about features they offer. Ext4's pretty standard though and it's a pretty good choice for regular file storage. I'll make a file system with mkfs.ext4/dev/sda1. Again, making sure that I'm using the partition that I mean to. Creating a file system is also destructive, but it warns you if there's already a file system on the partition you're working with. Again, always be careful when working with storage devices. This partition has a file system now, so it's ready to hold files. In order to use it for that, we'll need to mount the disk to attach it to the root file system somewhere and in order to mount it, we'll need to tell the system where to attach it. I'll create a directory here in the MNT directory as a placeholder. And then, I'll use the mount command to attach the disks file system to my existing file system at that location. Now if I look at my storage, I can see that that disk is available, and completely empty, aside from the overhead that the file system takes up. Disks are usually mounted by root and that means that only root can create, read or modify files on them, unless we change the permissions. There's a variety of ways to do this, depending on how users might need to interact with the same files on the device. But for now, I'll just use chmod 777 and the path to the disk which will let any user read, write and execute files on this file system. Files will still be protected by normal permissions though so a user wouldn't be able to edit someone else's files unless they changed the permissions to let them do so. Or, if root set you ID or set GID bids. Again, the focus here is to create a general storage place for services, individual users and so on to store files, not as a kind of collaboration space. Now, we have a disk where files can be stored separately from the system partition. We can also go through the same set of steps for external storage, like the USB disk. We can manually mount this disk each time the system restarts. We'll explore automatic mounting in just a little bit. Storage is important to servers both because it can be used to store files served on the network, but also because many services we might want our server to run, need storage of their own. Things like containers, virtual machines, databases, websites, code repositories, applications, datasets, machine learning models and more, all need to be physically stored somewhere. As I mentioned earlier in the course, storage can effectively be expanded indefinitely, as long as you have enough ports and enough large disks. We can connect to storage that already exists on the network too. Storage administration is a useful skill for system administrators and anyone who's running a server.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。