Ubuntu 常用磁盘操作

Ubuntu 常用磁盘操作

初始化新磁盘

先使用 fdisk -l 命令查看所有检测到的磁盘与分区,找到新添加磁盘的路径:

Disk /dev/sdb: 256 GiB, 274877906944 bytes, 536870912 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

对新添加的磁盘进行分区:

 ~  sudo fdisk /dev/sdb                                                                                                                                         

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x08c3917d.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-268435455, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-268435455, default 268435455):

Created a new partition 1 of type 'Linux' and of size 128 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

再次查看所有磁盘与分区:

Disk /dev/sdb: 128 GiB, 137438953472 bytes, 268435456 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x08c3917d

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdb1        2048 268435455 268433408  128G 83 Linux

格式化分区:

sudo mkfs -t ext4 /dev/sdb1

挂载:

sudo mkdir /data
sudo mount /dev/sdb1 /data

查看空间占用:

 ~  sudo df -Th                                                                                                                                                 
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  197M  1.2M  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    61G  5.9G   53G  11% /
tmpfs                             tmpfs  982M     0  982M   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   2.0G  251M  1.6G  14% /boot
tmpfs                             tmpfs  197M  4.0K  197M   1% /run/user/1000
/dev/sdb1                         ext4   126G   24K  120G   1% /data

添加至 /etc/fstab 以实现开机自动挂载:
使用 blkidls -l /dev/disk/by-uuid/ 命令得到分区的 uuid,然后向 /etc/fstab 文件末尾追加一行:

UUID=10fd13d0-5d0f-42cc-9c14-331858bbcf01 /data ext4 defaults 0 2

最后重启系统。

附加磁盘扩容

使用 df -Th 查看当前磁盘分区容量、挂载信息:

/dev/sdc1                         ext4     126G   90G   30G  76% /data

首先通过 hypervisor (这里我使用的是 pve,其他虚拟机软件同理)直接配置扩展磁盘大小。pve 可以方便地进行在线容量扩展,不需要进行关机或重新挂载磁盘的操作。

进入系统,输入 fdisk -l 查看所有磁盘及分区,可以观察到硬盘有分区表大小不匹配的警告:

GPT PMBR size mismatch (268435455 != 536870911) will be corrected by write.
The backup GPT table is not on the end of the device.

先使用 parted -l 解决分区表不匹配问题:

Warning: Not all of the space available to /dev/sdc appears to be used, you can
fix the GPT to use all of the space (an extra 268435456 blocks) or continue with
the current setting?
Fix/Ignore? Fix
Model: QEMU QEMU HARDDISK (scsi)
Disk /dev/sdc: 275GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End    Size   File system  Name  Flags
 1      1049kB  137GB  137GB  ext4

接下来的操作就很简单了,直接使用 cfdisk /dev/sdc 命令进行 resize,然后使用 resize2fs /dev/sdc1 命令同步系统容量到内核。

再次使用 df -Th 查看当前磁盘分区容量:

 /dev/sdc1                         ext4     252G   90G  151G  38% /data

磁盘的容量已成功扩大并应用。

主分区磁盘扩容

类似地,先使用 cfdisk resize 磁盘并更新分区表。

然后,若直接 resize2fs:

 ~  sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv                                                                                                     
resize2fs 1.46.5 (30-Dec-2021)
The filesystem is already 7863296 (4k) blocks long.  Nothing to do!

先使用 pvresize 命令 resize,再使用 lvresize 扩大文件系统:

 ~  sudo pvresize /dev/sda3                                                                                                                                     
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
 ~  sudo lvresize -l  +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv                                                                                               
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <30.00 GiB (7679 extents) to <62.00 GiB (15871 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

再次使用 fdisk -l 命令查看,则观察到分区由:

Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 62 GiB, 66567798784 bytes, 130015232 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

变为:

Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 254 GiB, 272726228992 bytes, 532668416 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

最后使用 resize2fs 应用更改:

 ~  sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv                                                                                                            
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 8
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 16251904 (4k) blocks long.

=>

 ~  sudo df -Th                                                                                                                                                 ok | twikor@sodium
Filesystem                        Type   Size  Used Avail Use% Mounted on
tmpfs                             tmpfs  197M  1.2M  196M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4    61G  5.9G   53G  11% /
tmpfs                             tmpfs  982M     0  982M   0% /dev/shm
tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
/dev/sda2                         ext4   2.0G  251M  1.6G  14% /boot
tmpfs                             tmpfs  197M  4.0K  197M   1% /run/user/1000

参考链接

  1. https://www.reddit.com/r/Proxmox/comments/12ubr84/ive_increased_a_vms_partition_size_but_the_lvm/
  2. http://linux.51yip.com/