Onela@胡邹不胡诌
_

Ubuntu磁盘挂载以及自动重启挂载配置

Mar 20, 2023 · 10 min · Ubuntu

Ubuntu磁盘挂载以及自动重启挂载配置

当我们服务器SSD存储达到上限,需要对现有机器进行扩容。

首先我们要知道,文件系统大概分2中,即:xfs 和 ext4,centos/redhat 7 默认将文件系统设置为xfs。高并发压力下 xfs 的性能比 ext4 高 5-10% 左右。对应的io利用率 xfs 明显比ext4低。所以xfs是当前的最佳选择。

将实例存储卷添加到实例

当我们得到一张卷(Volume)时,需要将其添加到EC2实例,之后需要连接到 Linux 实例,进行安装:

1.用 df -h 命令查看已格式化并挂载的卷

root@ip-172-31-62-192:/# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        39G  4.4G   35G  12% /
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           788M  864K  787M   1% /run
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/loop1       56M   56M     0 100% /snap/core18/2128
/dev/loop2       56M   56M     0 100% /snap/core18/2253
/dev/loop0       25M   25M     0 100% /snap/amazon-ssm-agent/4046
/dev/loop3       62M   62M     0 100% /snap/core20/1270
/dev/loop4       68M   68M     0 100% /snap/lxd/21835
/dev/loop7       62M   62M     0 100% /snap/core20/1169
/dev/loop5       68M   68M     0 100% /snap/lxd/21545
/dev/loop6       44M   44M     0 100% /snap/snapd/14295
/dev/loop8       33M   33M     0 100% /snap/snapd/13640
tmpfs           788M     0  788M   0% /run/user/0

2.使用 lsblk 查看在启动时已映射但未格式化和装载的所有卷

root@ip-172-31-62-192:/# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0   25M  1 loop /snap/amazon-ssm-agent/4046
loop1         7:1    0 55.4M  1 loop /snap/core18/2128
loop2         7:2    0 55.5M  1 loop /snap/core18/2253
loop3         7:3    0 61.9M  1 loop /snap/core20/1270
loop4         7:4    0 67.2M  1 loop /snap/lxd/21835
loop5         7:5    0 67.3M  1 loop /snap/lxd/21545
loop6         7:6    0 43.3M  1 loop /snap/snapd/14295
loop7         7:7    0 61.9M  1 loop /snap/core20/1169
loop8         7:8    0 32.5M  1 loop /snap/snapd/13640
nvme1n1     259:0    0  100G  0 disk
nvme0n1     259:1    0   40G  0 disk
└─nvme0n1p1 259:2    0   40G  0 part /

3.使用 mkfs 命令在设备上创建文件系统

sudo mkfs -t xfs /dev/nvme1n1

4.使用 mkdir 命令创建要将设备挂载到的目录

sudo mkdir /data

5.使用 mount 命令在新建目录上挂载设备

sudo mount /dev/nvme1n1 /data

6.查看挂载结果

sudo lsblk -f 或 df -h

重启后自动挂载附加的卷

1.(可选)创建 /etc/fstab 文件的备份,以便在编辑时误损坏或删除此文件时使用。

sudo cp /etc/fstab /etc/fstab.orig

2.使用 blkid 命令查找设备的 UUID。/dev/nvme1n1是我们要操作的卷,记下 UUID,在下一步中您将需要用到它。

root@ip-172-31-62-192:/# blkid
/dev/nvme1n1: UUID="c9c99018-1fcb-4f95-a991-4039fa738b8c" TYPE="xfs"
/dev/nvme0n1p1: LABEL="cloudimg-rootfs" UUID="2a29f520-1100-4824-b5d9-d841f1267838" TYPE="ext4" PARTUUID="3fa6b62f-01"

3.编辑fstab文件

sudo vim /etc/fstab

我们将 UUID 为 c9c99018-1fcb-4f95-a991-4039fa738b8c 的设备挂载到挂载点 /revoloiot,然后我们使用 xfs 文件系统。我们还使用 defaultsnofail 标志。我们指定 0 以防止文件系统被转储,并且我们指定 2 以指示它是非根设备。

LABEL=cloudimg-rootfs   /        ext4   defaults,discard        0 1
# 根据uuid挂载
UUID=c9c99018-1fcb-4f95-a991-4039fa738b8c   /data xfs  defaults 0 1
# 根据路径挂载(推荐)
/dev/sdb /data xfs  defaults 0 1

UUID和文件类型必须与第2步中一致,否则将失败

4.检查是否生效

sudo mount -a

🍺 如果未产生错误,则说明 /etc/fstab 文件正常,您的文件系统会在重启后自动挂载。

ubuntu 如何将Amazon EBS(作为根目录装载)的大小调整

ubuntu 磁盘扩容与liunx有所区别,同时,需要注意磁盘分区格式的不同,命令有细微差异。

$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0         7:0    0  55.6M  1 loop /snap/core18/2679
loop1         7:1    0 116.7M  1 loop /snap/core/14447
loop2         7:2    0  25.1M  1 loop /snap/amazon-ssm-agent/5656
loop3         7:3    0 116.7M  1 loop /snap/core/14399
loop4         7:4    0  55.6M  1 loop /snap/core18/2667
loop5         7:5    0  24.4M  1 loop /snap/amazon-ssm-agent/6312
nvme0n1     259:0    0     3T  0 disk 
└─nvme0n1p1 259:1    0     2T  0 part /
$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  7.7G     0  7.7G   0% /dev
tmpfs          tmpfs     1.6G  780K  1.6G   1% /run
/dev/nvme0n1p1 ext4      2.0T  1.9T   43G  98% /
tmpfs          tmpfs     7.7G   64K  7.7G   1% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/loop0     squashfs   56M   56M     0 100% /snap/core18/2679
/dev/loop1     squashfs  117M  117M     0 100% /snap/core/14447
/dev/loop2     squashfs   26M   26M     0 100% /snap/amazon-ssm-agent/5656
/dev/loop3     squashfs  117M  117M     0 100% /snap/core/14399
/dev/loop5     squashfs   25M   25M     0 100% /snap/amazon-ssm-agent/6312
/dev/loop4     squashfs   56M   56M     0 100% /snap/core18/2667
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1000
$ sudo gdisk /dev/nvme1n1
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
  MBR: MBR only
  BSD: not present
  APM: not present
  GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format
in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by
typing 'q' if you don't want to convert your MBR partitions
to GPT format!
***************************************************************
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/nvme1n1.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
$ sudo gdisk /dev/nvme1n1
GPT fdisk (gdisk) version 1.0.3
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): q
$ sudo parted /dev/nvme1n1
GNU Parted 3.2
Using /dev/nvme1n1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                                                            
Model: Amazon Elastic Block Store (nvme)
Disk /dev/nvme1n1: 3221GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system  Name              Flags
 1      1049kB  2199GB  2199GB  ext4         Linux filesystem
(parted) resizepart                                                       
Partition number? 1                                                       
Warning: Partition /dev/nvme0n1p1 is being used. Are you sure you want to continue?
Yes/No? yes                                                               
End?  [2199GB]? 3000GB                                                    
(parted) print                                                            
Model: Amazon Elastic Block Store (nvme)
Disk /dev/nvme1n1: 3221GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start   End     Size    File system  Name              Flags
 1      1049kB  3000GB  3000GB  ext4         Linux filesystem
(parted) q                                                                
Information: You may need to update /etc/fstab.
$ blkid                                           
/dev/nvme1n1: LABEL="cloudimg-rootfs" UUID="90e1dfca-b055-4f93-b62e-6347bcb451a7" TYPE="ext4" PARTUUID="f7355124-01"
$ cat /etc/fstab 
LABEL=cloudimg-rootfs   /    ext4   defaults,discard    0 0
$ lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0         7:0    0  55.6M  1 loop /snap/core18/2679
loop1         7:1    0 116.7M  1 loop /snap/core/14447
loop2         7:2    0  25.1M  1 loop /snap/amazon-ssm-agent/5656
loop3         7:3    0 116.7M  1 loop /snap/core/14399
loop4         7:4    0  55.6M  1 loop /snap/core18/2667
loop5         7:5    0  24.4M  1 loop /snap/amazon-ssm-agent/6312
nvme0n1     259:0    0     3T  0 disk 
└─nvme0n1p1 259:1    0   2.7T  0 part /
$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  7.7G     0  7.7G   0% /dev
tmpfs          tmpfs     1.6G  780K  1.6G   1% /run
/dev/nvme0n1p1 ext4      2.0T  1.9T   43G  98% /
tmpfs          tmpfs     7.7G   64K  7.7G   1% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/loop0     squashfs   56M   56M     0 100% /snap/core18/2679
/dev/loop1     squashfs  117M  117M     0 100% /snap/core/14447
/dev/loop2     squashfs   26M   26M     0 100% /snap/amazon-ssm-agent/5656
/dev/loop3     squashfs  117M  117M     0 100% /snap/core/14399
/dev/loop5     squashfs   25M   25M     0 100% /snap/amazon-ssm-agent/6312
/dev/loop4     squashfs   56M   56M     0 100% /snap/core18/2667
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1000
#$ sudo resize2fs /dev/nvme1n1 
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing required
old_desc_blocks = 256, new_desc_blocks = 350
The filesystem on /dev/nvme0n1p1 is now 732421619 (4k) blocks long.
$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  7.7G     0  7.7G   0% /dev
tmpfs          tmpfs     1.6G  780K  1.6G   1% /run
/dev/nvme0n1p1 ext4      2.0T  1.9T   43G  98% /
tmpfs          tmpfs     7.7G   64K  7.7G   1% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/loop0     squashfs   56M   56M     0 100% /snap/core18/2679
/dev/loop1     squashfs  117M  117M     0 100% /snap/core/14447
/dev/loop2     squashfs   26M   26M     0 100% /snap/amazon-ssm-agent/5656
/dev/loop3     squashfs  117M  117M     0 100% /snap/core/14399
/dev/loop5     squashfs   25M   25M     0 100% /snap/amazon-ssm-agent/6312
/dev/loop4     squashfs   56M   56M     0 100% /snap/core18/2667
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1000
## resize2fs 命令 :针对的是ext2、ext3、ext4文件系统
## xfs_growfs 命令 :针对的是xfs文件系统
$ sudo xfs_growfs /dev/nvme1n1 
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/nvme0n1p1 is mounted on /; on-line resizing required
old_desc_blocks = 256, new_desc_blocks = 350
The filesystem on /dev/nvme0n1p1 is now 732421619 (4k) blocks long.
$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
udev           devtmpfs  7.7G     0  7.7G   0% /dev
tmpfs          tmpfs     1.6G  780K  1.6G   1% /run
/dev/nvme0n1p1 ext4      2.7T  1.9T  767G  72% /
tmpfs          tmpfs     7.7G   64K  7.7G   1% /dev/shm
tmpfs          tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs          tmpfs     7.7G     0  7.7G   0% /sys/fs/cgroup
/dev/loop0     squashfs   56M   56M     0 100% /snap/core18/2679
/dev/loop1     squashfs  117M  117M     0 100% /snap/core/14447
/dev/loop2     squashfs   26M   26M     0 100% /snap/amazon-ssm-agent/5656
/dev/loop3     squashfs  117M  117M     0 100% /snap/core/14399
/dev/loop5     squashfs   25M   25M     0 100% /snap/amazon-ssm-agent/6312
/dev/loop4     squashfs   56M   56M     0 100% /snap/core18/2667
tmpfs          tmpfs     1.6G     0  1.6G   0% /run/user/1000

ubuntu默认磁盘空间未全部占用

# 查看未分配磁盘空间
vgdisplay

# 将剩余的全部空间给根目录
lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

# 应用生效
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

Edit this page on GitHub Last updated: 2024/6/16 13:40:09