Ubuntu捏造机使用纯下令行对根分区进行扩展

打印 上一主题 下一主题

主题 907|帖子 907|积分 2721

Ubuntu捏造机使用纯下令行对根分区进行扩展

前排提示

因为Ubuntu再安装时,根分区是没有使用LVM进行磁盘管理的,以是如果想扩展根分区,我们不得不使用别的一种暴力的方法。简单来说就是使用fdisk删除原来的根分区再基于原来的起始块号重新建立一个根分区。从而达到扩展根分区的目标。
步骤

0、我在创建捏造机的时间,硬盘只分配了50G,我的捏造机基本配置如下:

1、首先使用df和lsblk查看我分区情况:
  1. root@lunar-virtual-machine:~/workspace#
  2. root@lunar-virtual-machine:~/workspace# df -h
  3. Filesystem      Size  Used Avail Use% Mounted on
  4. tmpfs           790M  2.0M  788M   1% /run
  5. /dev/sda3        49G   30G   17G  65% /
  6. tmpfs           3.9G     0  3.9G   0% /dev/shm
  7. tmpfs           5.0M  4.0K  5.0M   1% /run/lock
  8. /dev/sda2       512M  6.1M  506M   2% /boot/efi
  9. tmpfs           790M   76K  790M   1% /run/user/128
  10. tmpfs           790M   60K  790M   1% /run/user/0
  11. root@lunar-virtual-machine:~/workspace#
  12. root@lunar-virtual-machine:~/workspace#
  13. root@lunar-virtual-machine:~/workspace# lsblk
  14. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
  15. loop0    7:0    0     4K  1 loop /snap/bare/5
  16. loop1    7:1    0  63.9M  1 loop /snap/core20/2264
  17. loop2    7:2    0  74.2M  1 loop /snap/core22/1122
  18. loop3    7:3    0  63.9M  1 loop /snap/core20/2318
  19. loop4    7:4    0  74.2M  1 loop /snap/core22/1380
  20. loop5    7:5    0   497M  1 loop /snap/gnome-42-2204/141
  21. loop6    7:6    0 349.7M  1 loop /snap/gnome-3-38-2004/143
  22. loop7    7:7    0 268.3M  1 loop /snap/firefox/4090
  23. loop8    7:8    0 505.1M  1 loop /snap/gnome-42-2204/176
  24. loop9    7:9    0 269.6M  1 loop /snap/firefox/4136
  25. loop10   7:10   0  91.7M  1 loop /snap/gtk-common-themes/1535
  26. loop11   7:11   0  12.9M  1 loop /snap/snap-store/1113
  27. loop12   7:12   0  12.3M  1 loop /snap/snap-store/959
  28. loop13   7:13   0  38.7M  1 loop /snap/snapd/21465
  29. loop14   7:14   0  40.4M  1 loop /snap/snapd/20671
  30. loop15   7:15   0   476K  1 loop /snap/snapd-desktop-integration/157
  31. loop16   7:16   0   452K  1 loop /snap/snapd-desktop-integration/83
  32. sda      8:0    0    50G  0 disk
  33. ├─sda1   8:1    0     1M  0 part
  34. ├─sda2   8:2    0   513M  0 part /boot/efi
  35. └─sda3   8:3    0  49.5G  0 part /var/snap/firefox/common/host-hunspell
  36.                                  /
  37. sr0     11:0    1   4.7G  0 rom  
  38. root@lunar-virtual-machine:~/workspace#
复制代码
2、关闭捏造机,修改硬盘大小:

3、启动捏造机,再次使用df、lsblk查看分区情况,从lsblk下令的输出可以看到/dev/sda设备的容量变成了100G,但是因为我们还没进行分区,剩余的50G无法被投入使用,以是下一步开始准备扩展分区:
  1. root@lunar-virtual-machine:~/workspace#
  2. root@lunar-virtual-machine:~/workspace# df -h
  3. Filesystem      Size  Used Avail Use% Mounted on
  4. tmpfs           790M  2.0M  788M   1% /run
  5. /dev/sda3        49G   30G   17G  65% /
  6. tmpfs           3.9G     0  3.9G   0% /dev/shm
  7. tmpfs           5.0M  4.0K  5.0M   1% /run/lock
  8. /dev/sda2       512M  6.1M  506M   2% /boot/efi
  9. tmpfs           790M   76K  790M   1% /run/user/128
  10. tmpfs           790M   60K  790M   1% /run/user/0
  11. root@lunar-virtual-machine:~/workspace#
  12. root@lunar-virtual-machine:~/workspace#
  13. root@lunar-virtual-machine:~/workspace# lsblk
  14. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
  15. loop0    7:0    0     4K  1 loop /snap/bare/5
  16. loop1    7:1    0  63.9M  1 loop /snap/core20/2264
  17. loop2    7:2    0  74.2M  1 loop /snap/core22/1380
  18. loop3    7:3    0  74.2M  1 loop /snap/core22/1122
  19. loop4    7:4    0  63.9M  1 loop /snap/core20/2318
  20. loop5    7:5    0 268.3M  1 loop /snap/firefox/4090
  21. loop6    7:6    0 269.6M  1 loop /snap/firefox/4136
  22. loop7    7:7    0 349.7M  1 loop /snap/gnome-3-38-2004/143
  23. loop8    7:8    0   497M  1 loop /snap/gnome-42-2204/141
  24. loop9    7:9    0 505.1M  1 loop /snap/gnome-42-2204/176
  25. loop10   7:10   0  91.7M  1 loop /snap/gtk-common-themes/1535
  26. loop11   7:11   0  12.9M  1 loop /snap/snap-store/1113
  27. loop12   7:12   0  12.3M  1 loop /snap/snap-store/959
  28. loop13   7:13   0  40.4M  1 loop /snap/snapd/20671
  29. loop14   7:14   0  38.7M  1 loop /snap/snapd/21465
  30. loop15   7:15   0   476K  1 loop /snap/snapd-desktop-integration/157
  31. loop16   7:16   0   452K  1 loop /snap/snapd-desktop-integration/83
  32. sda      8:0    0   100G  0 disk
  33. ├─sda1   8:1    0     1M  0 part
  34. ├─sda2   8:2    0   513M  0 part /boot/efi
  35. └─sda3   8:3    0  49.5G  0 part /var/snap/firefox/common/host-hunspell
  36.                                  /
  37. sr0     11:0    1   4.7G  0 rom  
  38. root@lunar-virtual-machine:~/workspace#
复制代码
4、使用fdisk下令进行扩容,具体步骤是:使用p查看一下初始分区情况,一定要记住sda3的起始块号:1054720,然后使用d删除第3分区(此时很关键,一定不要使用w保持退出!),然后再n一个3分区,然后起始块就是1054720,最后一块默认就行了,将全部块都分配给根分区。然后w保持退出。
  1. root@lunar-virtual-machine:~/workspace# fdisk /dev/sda
  2. Welcome to fdisk (util-linux 2.37.2).
  3. Changes will remain in memory only, until you decide to write them.
  4. Be careful before using the write command.
  5. This disk is currently in use - repartitioning is probably a bad idea.
  6. It's recommended to umount all file systems, and swapoff all swap
  7. partitions on this disk.
  8. Command (m for help): m
  9. Help:
  10.   GPT
  11.    M   enter protective/hybrid MBR
  12.   Generic
  13.    d   delete a partition
  14.    F   list free unpartitioned space
  15.    l   list known partition types
  16.    n   add a new partition
  17.    p   print the partition table
  18.    t   change a partition type
  19.    v   verify the partition table
  20.    i   print information about a partition
  21.   Misc
  22.    m   print this menu
  23.    x   extra functionality (experts only)
  24.   Script
  25.    I   load disk layout from sfdisk script file
  26.    O   dump disk layout to sfdisk script file
  27.   Save & Exit
  28.    w   write table to disk and exit
  29.    q   quit without saving changes
  30.   Create a new label
  31.    g   create a new empty GPT partition table
  32.    G   create a new empty SGI (IRIX) partition table
  33.    o   create a new empty DOS partition table
  34.    s   create a new empty Sun partition table
  35. Command (m for help): p
  36. Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
  37. Disk model: VMware Virtual S
  38. Units: sectors of 1 * 512 = 512 bytes
  39. Sector size (logical/physical): 512 bytes / 512 bytes
  40. I/O size (minimum/optimal): 512 bytes / 512 bytes
  41. Disklabel type: gpt
  42. Disk identifier: 14A69C2D-E6FB-4626-A7E6-10A16A052F8D
  43. Device       Start       End   Sectors  Size Type
  44. /dev/sda1     2048      4095      2048    1M BIOS boot
  45. /dev/sda2     4096   1054719   1050624  513M EFI System
  46. /dev/sda3  1054720 104855551 103800832 49.5G Linux filesystem
  47. Command (m for help): d
  48. Partition number (1-3, default 3): 3
  49. Partition 3 has been deleted.
  50. Command (m for help): p
  51. Disk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectors
  52. Disk model: VMware Virtual S
  53. Units: sectors of 1 * 512 = 512 bytes
  54. Sector size (logical/physical): 512 bytes / 512 bytes
  55. I/O size (minimum/optimal): 512 bytes / 512 bytes
  56. Disklabel type: gpt
  57. Disk identifier: 14A69C2D-E6FB-4626-A7E6-10A16A052F8D
  58. Device     Start     End Sectors  Size Type
  59. /dev/sda1   2048    4095    2048    1M BIOS boot
  60. /dev/sda2   4096 1054719 1050624  513M EFI System
  61. Command (m for help): n
  62. Partition number (3-128, default 3):
  63. First sector (1054720-209715166, default 1054720): 1054720
  64. Last sector, +/-sectors or +/-size{K,M,G,T,P} (1054720-209715166, default 209715166):
  65. Created a new partition 3 of type 'Linux filesystem' and of size 99.5 GiB.
  66. Partition #3 contains a ext4 signature.
  67. Do you want to remove the signature? [Y]es/[N]o: y
  68. The signature will be removed by a write command.
  69. Command (m for help): w
  70. The partition table has been altered.
  71. Syncing disks.
  72. root@lunar-virtual-machine:~/workspace#
复制代码
5、此时再用下令查看一下分区情况,我可以看到lsblk下令将剩余的50G算作根分区的容量,但是df下令显示的根分区还是50个G。这点很好办理,继续下面步骤。
  1. root@lunar-virtual-machine:~/workspace# lsblk
  2. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
  3. loop0    7:0    0     4K  1 loop /snap/bare/5
  4. loop1    7:1    0  63.9M  1 loop /snap/core20/2264
  5. loop2    7:2    0  74.2M  1 loop /snap/core22/1380
  6. loop3    7:3    0  74.2M  1 loop /snap/core22/1122
  7. loop4    7:4    0  63.9M  1 loop /snap/core20/2318
  8. loop5    7:5    0 268.3M  1 loop /snap/firefox/4090
  9. loop6    7:6    0 269.6M  1 loop /snap/firefox/4136
  10. loop7    7:7    0 349.7M  1 loop /snap/gnome-3-38-2004/143
  11. loop8    7:8    0   497M  1 loop /snap/gnome-42-2204/141
  12. loop9    7:9    0 505.1M  1 loop /snap/gnome-42-2204/176
  13. loop10   7:10   0  91.7M  1 loop /snap/gtk-common-themes/1535
  14. loop11   7:11   0  12.9M  1 loop /snap/snap-store/1113
  15. loop12   7:12   0  12.3M  1 loop /snap/snap-store/959
  16. loop13   7:13   0  40.4M  1 loop /snap/snapd/20671
  17. loop14   7:14   0  38.7M  1 loop /snap/snapd/21465
  18. loop15   7:15   0   476K  1 loop /snap/snapd-desktop-integration/157
  19. loop16   7:16   0   452K  1 loop /snap/snapd-desktop-integration/83
  20. sda      8:0    0   100G  0 disk
  21. ├─sda1   8:1    0     1M  0 part
  22. ├─sda2   8:2    0   513M  0 part /boot/efi
  23. └─sda3   8:3    0  99.5G  0 part /var/snap/firefox/common/host-hunspell
  24.                                  /
  25. sr0     11:0    1   4.7G  0 rom  
  26. root@lunar-virtual-machine:~/workspace# df -h
  27. Filesystem      Size  Used Avail Use% Mounted on
  28. tmpfs           790M  2.0M  788M   1% /run
  29. /dev/sda3        49G   30G   17G  65% /
  30. tmpfs           3.9G     0  3.9G   0% /dev/shm
  31. tmpfs           5.0M  4.0K  5.0M   1% /run/lock
  32. /dev/sda2       512M  6.1M  506M   2% /boot/efi
  33. tmpfs           790M   76K  790M   1% /run/user/128
  34. tmpfs           790M   60K  790M   1% /run/user/0
复制代码
6、使用df -Th下令查看根分区的文件系统类型,可以看到挂载在/目录下的文件系统是ext4类型文件系统,以是,我们可以使用resize2fs /dev/sda下令可以更新分区情况。至此,根分区扩展完毕。
  1. root@lunar-virtual-machine:~/workspace# df -Th
  2. Filesystem     Type   Size  Used Avail Use% Mounted on
  3. tmpfs          tmpfs  790M  2.0M  788M   1% /run
  4. /dev/sda4      ext4    49G   30G   17G  65% /
  5. tmpfs          tmpfs  3.9G     0  3.9G   0% /dev/shm
  6. tmpfs          tmpfs  5.0M  4.0K  5.0M   1% /run/lock
  7. /dev/sda2      vfat   512M  6.1M  506M   2% /boot/efi
  8. tmpfs          tmpfs  790M   76K  790M   1% /run/user/128
  9. tmpfs          tmpfs  790M   60K  790M   1% /run/user/0
  10. root@lunar-virtual-machine:~/workspace# resize2fs /dev/sda
  11. resize2fs 1.46.5 (30-Dec-2021)
  12. resize2fs: Device or resource busy while trying to open /dev/sda
  13. Couldn't find valid filesystem superblock.
  14. root@lunar-virtual-machine:~/workspace# resize2fs /dev/sda3
  15. resize2fs 1.46.5 (30-Dec-2021)
  16. Filesystem at /dev/sda3 is mounted on /; on-line resizing required
  17. old_desc_blocks = 7, new_desc_blocks = 13
  18. The filesystem on /dev/sda3 is now 26082555 (4k) blocks long.
  19. root@lunar-virtual-machine:~/workspace#
  20. root@lunar-virtual-machine:~/workspace#
  21. root@lunar-virtual-machine:~/workspace# df -h
  22. Filesystem      Size  Used Avail Use% Mounted on
  23. tmpfs           790M  2.0M  788M   1% /run
  24. /dev/sda3        98G   30G   64G  32% /
  25. tmpfs           3.9G     0  3.9G   0% /dev/shm
  26. tmpfs           5.0M  4.0K  5.0M   1% /run/lock
  27. /dev/sda2       512M  6.1M  506M   2% /boot/efi
  28. tmpfs           790M   76K  790M   1% /run/user/128
  29. tmpfs           790M   60K  790M   1% /run/user/0
  30. root@lunar-virtual-machine:~/workspace# lsblk
  31. NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
  32. loop0    7:0    0     4K  1 loop /snap/bare/5
  33. loop1    7:1    0  63.9M  1 loop /snap/core20/2264
  34. loop2    7:2    0  74.2M  1 loop /snap/core22/1380
  35. loop3    7:3    0  74.2M  1 loop /snap/core22/1122
  36. loop4    7:4    0  63.9M  1 loop /snap/core20/2318
  37. loop5    7:5    0 268.3M  1 loop /snap/firefox/4090
  38. loop6    7:6    0 269.6M  1 loop /snap/firefox/4136
  39. loop7    7:7    0 349.7M  1 loop /snap/gnome-3-38-2004/143
  40. loop8    7:8    0   497M  1 loop /snap/gnome-42-2204/141
  41. loop9    7:9    0 505.1M  1 loop /snap/gnome-42-2204/176
  42. loop10   7:10   0  91.7M  1 loop /snap/gtk-common-themes/1535
  43. loop11   7:11   0  12.9M  1 loop /snap/snap-store/1113
  44. loop12   7:12   0  12.3M  1 loop /snap/snap-store/959
  45. loop13   7:13   0  40.4M  1 loop /snap/snapd/20671
  46. loop14   7:14   0  38.7M  1 loop /snap/snapd/21465
  47. loop15   7:15   0   476K  1 loop /snap/snapd-desktop-integration/157
  48. loop16   7:16   0   452K  1 loop /snap/snapd-desktop-integration/83
  49. sda      8:0    0   100G  0 disk
  50. ├─sda1   8:1    0     1M  0 part
  51. ├─sda2   8:2    0   513M  0 part /boot/efi
  52. └─sda3   8:3    0  99.5G  0 part /var/snap/firefox/common/host-hunspell
  53.                                  /
  54. sr0     11:0    1   4.7G  0 rom  
复制代码
这里额外记录一下,第6步最后的刷新文件系统状态的下令,不同的文件系统必要使用不同的下令:


  • resize2fs /dev/sda 针对文件系统ext2 ext3 ext4 (一样平常是Ubuntu上使用
  • xfs_growfs /dev/sda 针对文件系统xfs (一样平常是centos上使用,而centos是使用lvm来扩展根分区的。
磁盘分区小干货:
硬盘分区有三种,主磁盘分区、扩展磁盘分区、逻辑分区。
三种分区必须满足以下限制:

  • 一个硬盘主分区至少有1个,最多4个。
  • 扩展分区可以没有,最多1个。(扩展分区其实也算一种特殊的主分区
  • 主分区+扩展分区总共不能超过4个。
  • 逻辑分区可以有多少个。
关于分区号:


  • 1~4号:只能被主分区和扩展分区使用。
  • 5~n号:被逻辑分区使用。
主分区可以包含一个操作系统(例如Linux、Windows)。
扩展分区本身不能包含文件系统,只能包含逻辑分区。

本章完结

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表