铊代码网
Published on 2024-12-13 / 5 Visits
2
0

Ubuntu - 扩展磁盘空间

[!NOTE] 导读
本文内容介绍了Ubuntu 扩展磁盘空间的过程。
转载请注明原文出处!

运行环境:

  • VMware WorkStation 17
  • Ubuntu 24.04 LTS

报错场景:在安装 GitLab 程序时,提示空间不足。

dpkg: error processing archive /var/cache/apt/archives/gitlab-jh_17.6.1-jh.0_amd64.deb (--unpack): cannot copy extracted data for './opt/gitlab/embedded/service/gitlab-rails/public/assets/webpack/gitlab-vscode/0.0.1-dev-20240909013227/vscode/extensions/groovy/language-configuration.json' to '/opt/gitlab/embedded/service/gitlab-rails/public/assets/webpack/gitlab-vscode/0.0.1-dev-20240909013227/vscode/extensions/groovy/language-configuration.json.dpkg-new': failed to write (No space left on device) No apport report written because the error message indicates a disk full error dpkg-deb: error: paste subprocess was killed by signal (Broken pipe) Errors were encountered while processing: /var/cache/apt/archives/gitlab-jh_17.6.1-jh.0_amd64.deb E: Sub-process /usr/bin/dpkg returned an error code (1)

After this operation, 4,252 MB of additional disk space will be used. E: You don't have enough free space in /var/cache/apt/archives/.

根据这段提示信息,需要扩充 /var/cache/apt/archives/ 目录所在的存储空间。

完整过程

尝试清理缓存

sudo apt-get clean
sudo apt-get autoremove

并且在删除未使用的镜像后,仍提示空间不足。

查看磁盘空间

使用 fdisk, parted, gparted 等工具来调整分区大小。

#查看磁盘和分区信息
sudo fdisk -l

Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VMware Virtual S
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: gpt
Disk identifier: AF1CAFE0-877B-4BC2-95CE-689275D33D3F

Device       Start       End   Sectors Size Type
/dev/sda1     2048      4095      2048   1M BIOS boot
/dev/sda2     4096   4198399   4194304   2G Linux filesystem
/dev/sda3  4198400 104855551 100657152  48G Linux filesystem


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 24 GiB, 25765609472 bytes, 50323456 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
#查看文件系统
df -h

Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              790M  2.5M  787M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   24G   18G  5.1G  78% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          2.0G  182M  1.7G  10% /boot

#查看某个目录所在的文件系统
df -h /opt

Filesystem                         Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv   24G   18G  5.1G  78% /

此时,需要定位错误信息的 /var/cache/apt/archives/、``:

df /var/cache/apt/archives

Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  24590672 17972472   5343732  78% /
  • Filesystem/dev/mapper/ubuntu--vg-ubuntu--lv
  • 1K-blocks (总大小): 24,590,672 KB 或大约 24 GB
  • Used (已使用空间): 22,578,456 KB 或大约 22.5 GB
  • Available (可用空间): 737,748 KB 或大约 0.7 GB
  • Use% (使用率): 97%
  • Mounted on (挂载点)/ (根目录)

查看逻辑卷:

# 查看当前 LVM 状态
vgdisplay ubuntu-vg
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID           
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <48.00 GiB
  PE Size               4.00 MiB
  Total PE              12287
  Alloc PE / Size       6143 / <24.00 GiB
  Free  PE / Size       6144 / 24.00 GiB
  VG UUID               UEkenS-231t-TRnt-uMHD-OKLp-XAqH-30ghO6

扩展逻辑卷

将剩余的可用空间全部(100%)分配:

# 如果有足够的空闲空间,可以直接扩展逻辑卷
lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv

  Size of logical volume ubuntu-vg/ubuntu-lv changed from <24.00 GiB (6143 extents) to <48.00 GiB (12287 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized

注意,可以修改为 50% 等占比。

应用:

# 调整文件系统以利用新空间
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv

resize2fs 1.47.0 (5-Feb-2023)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 6
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 12581888 (4k) blocks long.

从可用空间新建逻辑卷

若操作了上一步 “扩展逻辑卷”,这一步忽略即可。实际上,[铊代码网] 没有执行这一步,有点子繁琐。

新建两个10G的逻辑卷:

sudo lvcreate -L 10G -n var-lv ubuntu-vg
sudo lvcreate -L 10G -n opt-lv ubuntu-vg

检查逻辑卷是否创建成功:

sudo lvdisplay /dev/ubuntu-vg/var-lv
sudo lvdisplay /dev/ubuntu-vg/opt-lv

格式化逻辑卷:

sudo mkfs.ext4 /dev/ubuntu-vg/var-lv
sudo mkfs.ext4 /dev/ubuntu-vg/opt-lv

创建挂载点:

sudo mkdir -p /mnt/new-var
sudo mkdir -p /mnt/new-opt

挂载逻辑卷:

sudo mount /dev/ubuntu-vg/var-lv /mnt/new-var
sudo mount /dev/ubuntu-vg/opt-lv /mnt/new-opt

emmm,有点子麻烦啊~这里仅记录未验证

迁移现有数据:

sudo rsync -avxHAX /var/ /mnt/new-var/
sudo rsync -avxHAX /opt/ /mnt/new-opt/

更新 /etc/fstab

echo '/dev/mapper/ubuntu--vg-var--lv /var ext4 defaults 0 2' | sudo tee -a /etc/fstab
echo '/dev/mapper/ubuntu--vg-opt--lv /opt ext4 defaults 0 2' | sudo tee -a /etc/fstab

再次检查磁盘空间

df -h /var/cache/apt/archives

Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv  48G   23G   23G  50% /

本文内容到此为止,仅供参考;转载需注明出处。


Comment