Linux Storage System
[x] ordinary file can also be formatted and mounted
[x] terraform mount disk /dev/sdb, why this name?
[x] do experiment, using vagrant mount extra disk
[x] blkid, lsblk 使用场景
[x] fstab mount 设置
Vagrant demo please see vagrant-storage. This is for VirtualBox provider, at the time of writting this is a experimental feature.
After launch the vagrant machine:
1 | # up |
1 | # check block device, their name, type, size and mount point |
If you copy a big file to /data3, its use% may not change because flush does not happen, run sync to flush file system buffer.
1 | # format whole disk sdb without partition |
For creating logical volumes, please see LVM section. For creating loop device, please see Loop Device section.
From Linkedin Learn Linux: Storage Systems.
首先需要理解加入一个新的disk到系统之后,发生了什么,以及需要做什么工作才能使用这个新加入的disk。可以参考一下这篇文章. 主要用到了fdisk(partition), mkfs(make filesystem), mount or fstab(make accessable)
新加入的disk 的device file 如何命名的, Name conventions of device file, see here, 比如/dev/sda, /dev/sdb, etc.
总的来看,可以用这样的顺序去观察block storage system:
- lsblk, blkid 查看block device的大致状态,filesystem, disk, partition, mount point等
- /etc/fstab 查看是否persistent 以及 mount option 是否合适
- mount 查看一下defaults mount option 具体内容是什么
- df -k/-i 查看mount point space使用情况
- dd 测试I/O performance (或者结合iperf3如果是NFS 之类的分布式存储)
Partition
lsblk and blkid are used to identify block storages (linux also has character device).
有一点要注意,使用lsblk的时候,比如:
1 | NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT |
区分TYPE的不同类型(也可以从缩进结构看出), MOUNTPOINT中有值的部分才会在mount command中显示。在/etc/fstab中也会看到对应的entries.
Check storage partition:
1 | # list partitions and see type of a disk |
Formating
Formatting partition or disk is to make a filesystem on it.
1 | # format as ext4 type to /dev/sdb1 partition |
Mounting
Mounting is associating a filesystem with a directory, mornally we would mount an empty directory, otherwise the content in existing directory will be hidden.
1 | mount /dev/sdb1 /data |
The persistent mounts are in /etc/fstab file, If you type mount command on shell, you will see several mount points, like above proc, sysfs, debugfs. These are special mounts not in /etc/fstab, systemd mounts them on boot automatically.
For the mount options specific to file system type, see man page fstab and mount.
You can unmount by umount command, filesystem cannot be unmounted while in use, for example files are open, process has dir in it. Can check by lsof command.
1 | umount /data |
Filesystem Types
Commonly used ones: ext2/3/4, xfs, btrfs. they have different properties.
1 | man 5 filesystemd |
Note that sometime you cannot create file because of no space left on device, but when you check df -k ., it is not full, check df -i . may help, you may use up the inode capacity even if there are lots of space remain.
LVM
logical volume manager, a layer above physical partitions that allows multiple partitions to appear as one. Provides for growing a filesystem by adding physical space, can stripe and mirror.
There are 3 levels of abstraction, one onto another:
- physical volumes: disk or disk partitions
- volume groups: collections of physical volumes
- logical volumes: partitioning up a volume group
Assume we have new disk partitions /dev/sdc1, /dev/sdc2, /dev/sdc3:
1 | lvm |
Let’s create volume group:
1 | # -c n: cluster no |
Then create logical volume:
1 | # create a logical volume 600M from group vg1 |
1 | // apple will across 2 physocal volume |
1 | umount /apple |
现在就明白了,在Vagrantfile demo中,最开始已经有了2个logical volume: root and swap:
1 | NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT |
可以查看这2个lv 的信息, 这表示/dev/sda2是一个physical volume,centos_centos7 是volume group name. 对于logical volume的每一个抽象层,都有对应的command:
1 | # show physical volume info |
Swapping
Swapping 对于很多应用和服务都不太好,影响性能,所以注意是否需要关闭。
Usually swapping is set in /etc/fstab file, for example, in Vagrant machine:
1 | // as mentioned, /dev/mapper/centos_centos7-swap |
To disable it, just comment out and run swapoff /dev/mapper/centos_centos7-swap or swapoff -a
A partition or file can be configured as swap space:
1 | # a partition |
Uses a file as swap space (loop device也是类似的情况)
1 | # generate 1G file |
Then if you check free -h, swap space gets extended 1G.
Loop Device
这里不得不说到Pseudo-devices, 也就是常用的/dev/null, /dev/zero, /dev/full, /dev/random, etc. 它们都是character based devices.
What is loop device wiki, is a pseudo-device that makes a file accessible as block device. files of this kind are often used for CD, ISO images, etc. So after mounting them you can access the content.
In my blog <<What's New in CentOS 8>>, I have mentioned the commands:
1 | # shrink or extend file to specific size |
Or without losetup command:
1 | truncate -s 1g /tmp/loop.img2 |
So let’s see
1 | // see first unused loop device |
RAID Partitioning
Here is software RAID, combining multiple disks to improve performance and/or reliability, we can have striping, redundancy features, etc.
1 | # level1: mirror |
SSHFS
Filesystem client based on ssh. Fuse-based in user space, not privileged. Communication securely over SSH. Using standard SSH port (you can specify other ports).
类似于NFS mount, 通过SSH实现,这种方式还是挺方便,比如需要在bastion host中运行程序,可以把在develop host上的code repo 同步挂在到bastion中,就不用scp了.
1 | # on client install |