Ubuntu Common Disk Operations

Ubuntu Common Disk Operations

初始化新磁盘

先使用 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

最后重启系统。

主分区磁盘扩容

先使用 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/