当前位置:   article > 正文

shell命令实践:给ubuntu扩展磁盘空间学习linux的磁盘管理命令_this disk is currently in use - repartitioning is

this disk is currently in use - repartitioning is probably a bad idea. it's

https://ke.qq.com/course/417774?flowToken=1010783

目录

一 扩展已有磁盘空间

二 添加新磁盘,并挂载到文件系统中

1 添加磁盘硬件

2 将磁盘设备挂载到文件系统

 查看磁盘

使用mount命令挂载磁盘sdb

使用fdisk命令对新磁盘分区

三 参看系统的信息

1 使用df命令查看磁盘状态

四 设置开机自动挂载

1 配置/etc/fstab文件

2 验证

五 再加一个1G的盘,测试不分区的情况

1.创建挂载点,格式化/dev/sdc 并挂载。

2.添加到系统启动的fstab

3.写错遇到问题了

4 重新添加一次fstab

总结


有两种方法,第一种是扩展已有磁盘空间,第二种是添加新磁盘

一 扩展已有磁盘空间

首先要保证虚拟机处于关机状态

操作如下所示: 

前一个界面,可以看到容量信息,当前总大小是80G,可用空间是67.7G,还是挺充足的。

点击扩展弹出扩展磁盘容量对话框,填入扩展后的磁盘总大小,它仅支持增大,不支持缩小。我填了85.

 点击扩展后,弹出下图,确定即可

 这种方法的好处是简单易用,坏处是要删除当前的快照以后,才能正常扩展。所以如果不想删除快照,就需要第二种方法

二 添加新磁盘,并挂载到文件系统中

首先要保证虚拟机处于关机状态

1 添加磁盘硬件

在虚拟机设置对话框里选择添加

选择磁盘 

 磁盘类型,选择默认SCSI即可

 下一步,创建新虚拟磁盘

 下一步,选择大小,因为我是创建一个测试盘,5G就够了,根据需要自己添加大小,其他不用管也行。

 填磁盘的名字,按照默认的名字即可。

 点击完成。在看硬件列表多出一个磁盘,新硬盘(SCSI) 5GB,根据名字可知,磁盘类型SCSI,磁盘大小5GB。

 上一步完成后,就可以启动系统了。

2 将磁盘设备挂载到文件系统

 查看磁盘

1) 使用ls命令,直接查看/dev目录下的设备文件

看一下新添加的磁盘,已有的磁盘是sda,新添加的磁盘是/dev/sdb,从信息可以,磁盘是块设备b,类型是disk,主设备号是8,/dev/sdb的次设备号是16,

  1. $ ls -lsa /dev/sd*
  2. 0 brw-rw---- 1 root disk 8, 0 89 09:15 /dev/sda
  3. 0 brw-rw---- 1 root disk 8, 1 89 09:15 /dev/sda1
  4. 0 brw-rw---- 1 root disk 8, 2 89 09:15 /dev/sda2
  5. 0 brw-rw---- 1 root disk 8, 3 89 09:15 /dev/sda3
  6. 0 brw-rw---- 1 root disk 8, 16 89 09:15 /dev/sdb

2)使用lsblk

  1. $ 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 62M 1 loop /snap/core20/1587
  5. loop2 7:2 0 62M 1 loop /snap/core20/1593
  6. loop3 7:3 0 163.3M 1 loop /snap/firefox/1589
  7. loop4 7:4 0 400.8M 1 loop /snap/gnome-3-38-2004/112
  8. loop5 7:5 0 248.8M 1 loop /snap/gnome-3-38-2004/99
  9. loop6 7:6 0 163.3M 1 loop /snap/firefox/1635
  10. loop7 7:7 0 45.9M 1 loop /snap/snap-store/575
  11. loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
  12. loop9 7:9 0 43.6M 1 loop /snap/snapd/15177
  13. loop10 7:10 0 81.3M 1 loop /snap/gtk-common-themes/1534
  14. loop11 7:11 0 47M 1 loop /snap/snapd/16292
  15. loop12 7:12 0 45.9M 1 loop /snap/snap-store/582
  16. loop13 7:13 0 284K 1 loop /snap/snapd-desktop-integration/10
  17. loop14 7:14 0 284K 1 loop /snap/snapd-desktop-integration/14
  18. sda 8:0 0 85G 0 disk
  19. ├─sda1 8:1 0 1M 0 part
  20. ├─sda2 8:2 0 513M 0 part /boot/efi
  21. └─sda3 8:3 0 79.5G 0 part /
  22. sdb 8:16 0 5G 0 disk
  23. sr0 11:0 1 3.4G 0 rom /media/lkmao/Ubuntu 22.04 LTS amd64

倒数第二行,sdb 5GB,这也验证了/dev/sdb确实是新添加了磁盘

使用mount命令挂载磁盘sdb

1.创建挂载点

cd ~(波浪线):表示进入当前用户的家目录

  1. $cd ~
  2. $ mkdir new_disk
  3. $

2. $ sudo mount -t ext4 /dev/sdb ~/new_disk/

  1. $ sudo mount -t ext4 /dev/sdb ~/new_disk/
  2. [sudo] password for lkmao:
  3. mount: /home/lkmao/new_disk: wrong fs type, bad option, bad superblock on /dev/sdb, missing codepage or helper program, or other error.

这个是说,新磁盘wrong fs type,因为我们还没有给它格式化,里面当然没有文件系统啦,根据需要可以格式化为ext2 ext3 ext4 ,命令分别如下所示‘

  1. sudo mkfs.ext4 /dev/sda1 # 格式化为ext4分区
  2. sudo mkfs.ext3 /dev/sda1 # 格式化为ext3分区
  3. sudo mkfs.ext2 /dev/sda1 #格式化为ext2分区

我们需要挂载为ext4 文件系统,所以要格式化为ext4

$ sudo mkfs.ext4 /dev/sdb

然后再次挂载

$ sudo mount -t ext4 /dev/sdb ~/new_disk/

这样/dev/sdb就挂载到~/new_disk目录了

执行fdisk -l命令,下面的信息省略了loop相关的输出

  1. $ sudo fdisk -l
  2. Disk /dev/sda: 85 GiB, 91268055040 bytes, 178257920 sectors
  3. Disk model: VMware Virtual S
  4. Units: sectors of 1 * 512 = 512 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disklabel type: gpt
  8. Disk identifier: 164A21F0-BB7B-4780-A2CF-E23DCE26CDEB
  9. Device Start End Sectors Size Type
  10. /dev/sda1 2048 4095 2048 1M BIOS boot
  11. /dev/sda2 4096 1054719 1050624 513M EFI System
  12. /dev/sda3 1054720 167770111 166715392 79.5G Linux filesystem
  13. Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
  14. Disk model: VMware Virtual S
  15. Units: sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes

好像还不对,没有分区啊,sda有三个分区,sdb没有分区

使用fdisk命令对新磁盘分区

虚拟分区才可以,步骤如下

先把前面挂载的磁盘umount掉

$ sudo umount new_disk 
  1. $ sudo fdisk /dev/sdb
  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. The device contains 'ext4' signature and it will be removed by a write command. See fdisk(8) man page and --wipe option for more details.
  9. Device does not contain a recognized partition table.
  10. Created a new DOS disklabel with disk identifier 0x7232900c.
  11. Command (m for help): m
  12. Help:
  13. DOS (MBR)
  14. a toggle a bootable flag
  15. b edit nested BSD disklabel
  16. c toggle the dos compatibility flag
  17. Generic
  18. d delete a partition
  19. F list free unpartitioned space
  20. l list known partition types
  21. n add a new partition
  22. p print the partition table
  23. t change a partition type
  24. v verify the partition table
  25. i print information about a partition
  26. Misc
  27. m print this menu
  28. u change display/entry units
  29. x extra functionality (experts only)
  30. Script
  31. I load disk layout from sfdisk script file
  32. O dump disk layout to sfdisk script file
  33. Save & Exit
  34. w write table to disk and exit
  35. q quit without saving changes
  36. Create a new label
  37. g create a new empty GPT partition table
  38. G create a new empty SGI (IRIX) partition table
  39. o create a new empty DOS partition table
  40. s create a new empty Sun partition table

选择n ,添加一个分区

  1. Command (m for help): n
  2. Partition type
  3. p primary (0 primary, 0 extended, 4 free)
  4. e extended (container for logical partitions)
  5. Select (default p):

选择p 添加一个主分区,后面的保持默认的值,按两次回车即可

  1. Select (default p): p
  2. Partition number (1-4, default 1):
  3. First sector (2048-10485759, default 2048):
  4. Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485759, default 10485759):
  5. Created a new partition 1 of type 'Linux' and of size 5 GiB.

最后输入w保存,保存后就自动退出了

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

再次fdisk -l

  1. sudo fdisk -l
  2. Disk /dev/sda: 85 GiB, 91268055040 bytes, 178257920 sectors
  3. Disk model: VMware Virtual S
  4. Units: sectors of 1 * 512 = 512 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disklabel type: gpt
  8. Disk identifier: 164A21F0-BB7B-4780-A2CF-E23DCE26CDEB
  9. Device Start End Sectors Size Type
  10. /dev/sda1 2048 4095 2048 1M BIOS boot
  11. /dev/sda2 4096 1054719 1050624 513M EFI System
  12. /dev/sda3 1054720 167770111 166715392 79.5G Linux filesystem
  13. Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 sectors
  14. Disk model: VMware Virtual S
  15. Units: sectors of 1 * 512 = 512 bytes
  16. Sector size (logical/physical): 512 bytes / 512 bytes
  17. I/O size (minimum/optimal): 512 bytes / 512 bytes
  18. Disklabel type: dos
  19. Disk identifier: 0x43ad818a
  20. Device Boot Start End Sectors Size Id Type
  21. /dev/sdb1 2048 10485759 10483712 5G 83 Linux
  1. ~$ sudo mount -t ext4 /dev/sdb1 new_disk/
  2. mount: /home/lkmao/new_disk: wrong fs type, bad option, bad superblock on /dev/sdb1, missing codepage or helper program, or other error.

再次格式化分区/dev/sdb1

  1. $ sudo mkfs.ext4 /dev/sdb1
  2. mke2fs 1.46.5 (30-Dec-2021)
  3. Creating filesystem with 1310464 4k blocks and 327680 inodes
  4. Filesystem UUID: 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea
  5. Superblock backups stored on blocks:
  6. 32768, 98304, 163840, 229376, 294912, 819200, 884736
  7. Allocating group tables: done
  8. Writing inode tables: done
  9. Creating journal (16384 blocks): done
  10. Writing superblocks and filesystem accounting information: done

进入new_disk写东西测试一下

  1. ~/new_disk$ touch hello
  2. touch: cannot touch 'hello': Permission denied

修改new_disk文件夹的权限

$ sudo chmod o+w new_disk
  1. $ll
  2. drwxr-xrwx 3 root root 4096 89 10:30 new_disk/

new_disk已经具备其他用户的写权限了。也可以将new_disk转换成当前用户吗,试试看

sudo chown -R lkmao new_disk/
  1. $ls -l
  2. 4 drwxr-xrwx 3 lkmao root 4096 89 10:30 new_disk

把组归属也改一下,如下,组名也改成lkmao了

  1. $ sudo chgrp -R lkmao new_disk/
  2. $ ll
  3. drwxr-xrwx 3 lkmao lkmao 4096 89 10:30 new_disk/
  1. :~$ cd new_disk/
  2. :~/new_disk$ echo "hello world" > hello
  3. :~/new_disk$ cat hello
  4. hello world

也可以使用chown一次修改所属用户和所属组

$ sudo chown lkmao:lkmao new_disk -R

磁盘添加测试完毕

三 参看系统的信息

1 使用df命令查看磁盘状态

  1. $ df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. tmpfs 390M 3.4M 387M 1% /run
  4. /dev/sda3 78G 35G 39G 48% /
  5. tmpfs 2.0G 0 2.0G 0% /dev/shm
  6. tmpfs 5.0M 4.0K 5.0M 1% /run/lock
  7. /dev/sda2 512M 5.3M 507M 2% /boot/efi
  8. tmpfs 390M 2.4M 388M 1% /run/user/1000
  9. /dev/sr0 3.5G 3.5G 0 100% /media/lkmao/Ubuntu 22.04 LTS amd64
  10. /dev/sdb1 4.9G 28K 4.6G 1% /home/lkmao/new_disk

上面的信息可以 /dev/sdb1 挂

  1. $ ls -la
  2. total 28
  3. drwxr-xrwx 3 lkmao lkmao 4096 89 10:39 .
  4. drwxr-x--- 19 lkmao lkmao 4096 89 10:51 ..
  5. -rw-r--r-- 1 lkmao lkmao 12 89 10:38 hello
  6. drwx------ 2 lkmao lkmao 16384 89 10:28 lost+found

听说只要看见lost+found就是挂载成功了

四 设置开机自动挂载

1 配置/etc/fstab文件

开启自动挂载的时候,要使用磁盘的UUID不能使用磁盘名,因为名字是可变的,首先获取UUID

  1. $ ls /dev/disk/by-uuid/ -ls
  2. total 0
  3. 0 lrwxrwxrwx 1 root root 9 89 10:46 2022-04-19-10-23-19-00 -> ../../sr0
  4. 0 lrwxrwxrwx 1 root root 10 89 10:46 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea -> ../../sdb1
  5. 0 lrwxrwxrwx 1 root root 10 89 10:46 6220693a-d997-4b19-bde1-b0d8b1ecfde6 -> ../../sda3
  6. 0 lrwxrwxrwx 1 root root 10 89 10:46 8CC7-4130 -> ../../sda2

修改fstab,新增一个UUID

sudo vi /etc/fstab
  1. # /etc/fstab: static file system information.
  2. #
  3. # Use 'blkid' to print the universally unique identifier for a
  4. # device; this may be used with UUID= as a more robust way to name devices
  5. # that works even if disks are added and removed. See fstab(5).
  6. #
  7. # <file system> <mount point> <type> <options> <dump> <pass>
  8. # / was on /dev/sda3 during installation
  9. UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
  10. # /boot/efi was on /dev/sda2 during installation
  11. UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
  12. /swapfile none swap sw 0 0

从文件里面的内容可以,sda3已经被挂载到了根目录下,在/etc/fstab底部新增内容如下所示:

  1. UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1

然后保存/etc/fstab,重启系统

2 验证

重新登录系统,进入挂载点,读取文件,读取成功,说明磁盘成功自动挂载了。

  1. $ cd new_disk/
  2. :~/new_disk$ ls
  3. hello lost+found
  4. :~/new_disk$ cat hello
  5. hello world

五 再加一个1G的盘,测试不分区的情况

这一节主要验证不使用fdisk分区到底能不能添加磁盘,实验证明,是没问题的。

创建一个新磁盘

1.创建挂载点,格式化/dev/sdc 并挂载。

  1. lkmao@lkmao-virtual-machine:/$ mkdir disk_1g
  2. mkdir: cannot create directory ‘disk_1g’: Permission denied
  3. lkmao@lkmao-virtual-machine:/$
  4. lkmao@lkmao-virtual-machine:/$ sudo mkdir disk_1g
  5. [sudo] password for lkmao:
  6. lkmao@lkmao-virtual-machine:/$ ls /dev/sd*
  7. /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1 /dev/sdc
  8. lkmao@lkmao-virtual-machine:/$ sudo mkfs.ext4 /dev/sdc
  9. mke2fs 1.46.5 (30-Dec-2021)
  10. Creating filesystem with 262144 4k blocks and 65536 inodes
  11. Filesystem UUID: 56b4f3e4-142b-4898-8046-9aab07952dbc
  12. Superblock backups stored on blocks:
  13. 32768, 98304, 163840, 229376
  14. Allocating group tables: done
  15. Writing inode tables: done
  16. Creating journal (8192 blocks): done
  17. Writing superblocks and filesystem accounting information: done
  18. lkmao@lkmao-virtual-machine:/$ sudo mount -t ext4 /dev/sdc
  19. mount: /dev/sdc: can't find in /etc/fstab.
  20. lkmao@lkmao-virtual-machine:/$ sudo mount -t ext4 /dev/sdc /disk_1g/
  21. lkmao@lkmao-virtual-machine:/$ cd disk_1g/
  22. lkmao@lkmao-virtual-machine:/disk_1g$ touch hello
  23. touch: cannot touch 'hello': Permission denied
  24. lkmao@lkmao-virtual-machine:/disk_1g$ cd ..
  25. lkmao@lkmao-virtual-machine:/$ sudo chown lkmao:lkmao disk_1g/
  26. lkmao@lkmao-virtual-machine:/$ sudo chown lkmao:lkmao disk_1g/ -R
  27. lkmao@lkmao-virtual-machine:/$ cd disk_1g/
  28. lkmao@lkmao-virtual-machine:/disk_1g$ ls -lsa
  29. total 24
  30. 4 drwxr-xr-x 3 lkmao lkmao 4096 8月 9 12:01 .
  31. 4 drwxr-xr-x 21 root root 4096 8月 9 12:00 ..
  32. 16 drwx------ 2 lkmao lkmao 16384 8月 9 12:01 lost+found
  33. lkmao@lkmao-virtual-machine:/disk_1g$ touch hello
  34. lkmao@lkmao-virtual-machine:/disk_1g$ echo "hello world" > hello
  35. lkmao@lkmao-virtual-machine:/disk_1g$ ls
  36. hello lost+found
  37. lkmao@lkmao-virtual-m

2.添加到系统启动的fstab

  1. $ ls /dev/disk/by-uuid/ -ls
  2. total 0
  3. 0 lrwxrwxrwx 1 root root 9 89 11:58 2022-04-19-10-23-19-00 -> ../../sr0
  4. 0 lrwxrwxrwx 1 root root 10 89 11:58 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea -> ../../sdb1
  5. 0 lrwxrwxrwx 1 root root 9 89 12:01 56b4f3e4-142b-4898-8046-9aab07952dbc -> ../../sdc
  6. 0 lrwxrwxrwx 1 root root 10 89 11:58 6220693a-d997-4b19-bde1-b0d8b1ecfde6 -> ../../sda3
  7. 0 lrwxrwxrwx 1 root root 10 89 11:58 8CC7-4130 -> ../../sda2

添加完毕后的fstab

  1. # /etc/fstab: static file system information.
  2. #
  3. # Use 'blkid' to print the universally unique identifier for a
  4. # device; this may be used with UUID= as a more robust way to name devices
  5. # that works even if disks are added and removed. See fstab(5).
  6. #
  7. # <file system> <mount point> <type> <options> <dump> <pass>
  8. # / was on /dev/sda3 during installation
  9. UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
  10. # /boot/efi was on /dev/sda2 during installation
  11. UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
  12. /swapfile none swap sw 0 0
  13. UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1
  14. UUID=56b4f3e4-142b-4898-8046-9aab07952db /disk_1g ext4 errors=remount-ro 0 1

保存重启,然后,就没有然后了...

3.写错遇到问题了

 估计是刚才/etc/fstab/输入错误了,按回车,进行命令行,vi /etc/fstab,然后删掉最后一行新添加的,重启就好了,这个刚才操作太快了,忘记截图了,想想也没啥,其实就是一个shell终端,遇到这种情况别慌,把刚刚干的事情改正就行了。

4 重新添加一次fstab

  1. # /etc/fstab: static file system information.
  2. #
  3. # Use 'blkid' to print the universally unique identifier for a
  4. # device; this may be used with UUID= as a more robust way to name devices
  5. # that works even if disks are added and removed. See fstab(5).
  6. #
  7. # <file system> <mount point> <type> <options> <dump> <pass>
  8. # / was on /dev/sda3 during installation
  9. UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
  10. # /boot/efi was on /dev/sda2 during installation
  11. UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
  12. /swapfile none swap sw 0 0
  13. UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1
  14. UUID=56b4f3e4-142b-4898-8046-9aab07952dbc /disk_1g ext4 errors=remount-ro 0 1

这次没问题了

  1. $ df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. tmpfs 390M 3.4M 387M 1% /run
  4. /dev/sda3 78G 35G 39G 48% /
  5. tmpfs 2.0G 0 2.0G 0% /dev/shm
  6. tmpfs 5.0M 4.0K 5.0M 1% /run/lock
  7. /dev/sdc 974M 28K 907M 1% /disk_1g
  8. /dev/sda2 512M 5.3M 507M 2% /boot/efi
  9. /dev/sdb1 4.9G 28K 4.6G 1% /home/lkmao/new_disk
  10. tmpfs 390M 2.4M 388M 1% /run/user/1000
  11. /dev/sr0 3.5G 3.5G 0 100% /media/lkmao/Ubuntu 22.04 LTS amd64

总结

知识要反复验证,纸上得来终觉浅,绝知此事要躬行。

任何看似简单的事情,做起来的时候,都不简单,上面这些操作涉及了很多的命令,如果能操作一遍,对于熟悉这些命令是很有帮助的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/154246?site
推荐阅读
相关标签
  

闽ICP备14008679号