赞
踩
https://ke.qq.com/course/417774?flowToken=1010783
目录
有两种方法,第一种是扩展已有磁盘空间,第二种是添加新磁盘
首先要保证虚拟机处于关机状态
操作如下所示:
前一个界面,可以看到容量信息,当前总大小是80G,可用空间是67.7G,还是挺充足的。
点击扩展弹出扩展磁盘容量对话框,填入扩展后的磁盘总大小,它仅支持增大,不支持缩小。我填了85.
点击扩展后,弹出下图,确定即可
这种方法的好处是简单易用,坏处是要删除当前的快照以后,才能正常扩展。所以如果不想删除快照,就需要第二种方法
首先要保证虚拟机处于关机状态
在虚拟机设置对话框里选择添加
选择磁盘
磁盘类型,选择默认SCSI即可
下一步,创建新虚拟磁盘
下一步,选择大小,因为我是创建一个测试盘,5G就够了,根据需要自己添加大小,其他不用管也行。
填磁盘的名字,按照默认的名字即可。
点击完成。在看硬件列表多出一个磁盘,新硬盘(SCSI) 5GB,根据名字可知,磁盘类型SCSI,磁盘大小5GB。
上一步完成后,就可以启动系统了。
1) 使用ls命令,直接查看/dev目录下的设备文件
看一下新添加的磁盘,已有的磁盘是sda,新添加的磁盘是/dev/sdb,从信息可以,磁盘是块设备b,类型是disk,主设备号是8,/dev/sdb的次设备号是16,
- $ ls -lsa /dev/sd*
- 0 brw-rw---- 1 root disk 8, 0 8月 9 09:15 /dev/sda
- 0 brw-rw---- 1 root disk 8, 1 8月 9 09:15 /dev/sda1
- 0 brw-rw---- 1 root disk 8, 2 8月 9 09:15 /dev/sda2
- 0 brw-rw---- 1 root disk 8, 3 8月 9 09:15 /dev/sda3
- 0 brw-rw---- 1 root disk 8, 16 8月 9 09:15 /dev/sdb
-
2)使用lsblk
- $ lsblk
- NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
- loop0 7:0 0 4K 1 loop /snap/bare/5
- loop1 7:1 0 62M 1 loop /snap/core20/1587
- loop2 7:2 0 62M 1 loop /snap/core20/1593
- loop3 7:3 0 163.3M 1 loop /snap/firefox/1589
- loop4 7:4 0 400.8M 1 loop /snap/gnome-3-38-2004/112
- loop5 7:5 0 248.8M 1 loop /snap/gnome-3-38-2004/99
- loop6 7:6 0 163.3M 1 loop /snap/firefox/1635
- loop7 7:7 0 45.9M 1 loop /snap/snap-store/575
- loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
- loop9 7:9 0 43.6M 1 loop /snap/snapd/15177
- loop10 7:10 0 81.3M 1 loop /snap/gtk-common-themes/1534
- loop11 7:11 0 47M 1 loop /snap/snapd/16292
- loop12 7:12 0 45.9M 1 loop /snap/snap-store/582
- loop13 7:13 0 284K 1 loop /snap/snapd-desktop-integration/10
- loop14 7:14 0 284K 1 loop /snap/snapd-desktop-integration/14
- sda 8:0 0 85G 0 disk
- ├─sda1 8:1 0 1M 0 part
- ├─sda2 8:2 0 513M 0 part /boot/efi
- └─sda3 8:3 0 79.5G 0 part /
- sdb 8:16 0 5G 0 disk
- sr0 11:0 1 3.4G 0 rom /media/lkmao/Ubuntu 22.04 LTS amd64
倒数第二行,sdb 5GB,这也验证了/dev/sdb确实是新添加了磁盘
1.创建挂载点
cd ~(波浪线):表示进入当前用户的家目录
- $cd ~
- $ mkdir new_disk
- $
2. $ sudo mount -t ext4 /dev/sdb ~/new_disk/
- $ sudo mount -t ext4 /dev/sdb ~/new_disk/
- [sudo] password for lkmao:
- 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 ,命令分别如下所示‘
- sudo mkfs.ext4 /dev/sda1 # 格式化为ext4分区
- sudo mkfs.ext3 /dev/sda1 # 格式化为ext3分区
- 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相关的输出
- $ sudo fdisk -l
- Disk /dev/sda: 85 GiB, 91268055040 bytes, 178257920 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: 164A21F0-BB7B-4780-A2CF-E23DCE26CDEB
-
- Device Start End Sectors Size Type
- /dev/sda1 2048 4095 2048 1M BIOS boot
- /dev/sda2 4096 1054719 1050624 513M EFI System
- /dev/sda3 1054720 167770111 166715392 79.5G Linux filesystem
-
-
- Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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
-
好像还不对,没有分区啊,sda有三个分区,sdb没有分区
虚拟分区才可以,步骤如下
先把前面挂载的磁盘umount掉
$ sudo umount new_disk
- $ 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.
-
- This disk is currently in use - repartitioning is probably a bad idea.
- It's recommended to umount all file systems, and swapoff all swap
- partitions on this disk.
-
- 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.
-
- Device does not contain a recognized partition table.
- Created a new DOS disklabel with disk identifier 0x7232900c.
-
- Command (m for help): m
-
- Help:
-
- DOS (MBR)
- a toggle a bootable flag
- b edit nested BSD disklabel
- c toggle the dos compatibility flag
-
- Generic
- d delete a partition
- F list free unpartitioned space
- l list known partition types
- n add a new partition
- p print the partition table
- t change a partition type
- v verify the partition table
- i print information about a partition
-
- Misc
- m print this menu
- u change display/entry units
- x extra functionality (experts only)
-
- Script
- I load disk layout from sfdisk script file
- O dump disk layout to sfdisk script file
-
- Save & Exit
- w write table to disk and exit
- q quit without saving changes
-
- Create a new label
- g create a new empty GPT partition table
- G create a new empty SGI (IRIX) partition table
- o create a new empty DOS partition table
- s create a new empty Sun partition table
选择n ,添加一个分区
- 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 添加一个主分区,后面的保持默认的值,按两次回车即可
- Select (default p): p
- Partition number (1-4, default 1):
- First sector (2048-10485759, default 2048):
- Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485759, default 10485759):
-
- Created a new partition 1 of type 'Linux' and of size 5 GiB.
最后输入w保存,保存后就自动退出了
- Command (m for help): w
- The partition table has been altered.
- Calling ioctl() to re-read partition table.
- Syncing disks.
再次fdisk -l
- sudo fdisk -l
- Disk /dev/sda: 85 GiB, 91268055040 bytes, 178257920 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: 164A21F0-BB7B-4780-A2CF-E23DCE26CDEB
-
- Device Start End Sectors Size Type
- /dev/sda1 2048 4095 2048 1M BIOS boot
- /dev/sda2 4096 1054719 1050624 513M EFI System
- /dev/sda3 1054720 167770111 166715392 79.5G Linux filesystem
-
-
- Disk /dev/sdb: 5 GiB, 5368709120 bytes, 10485760 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: dos
- Disk identifier: 0x43ad818a
-
- Device Boot Start End Sectors Size Id Type
- /dev/sdb1 2048 10485759 10483712 5G 83 Linux
- ~$ sudo mount -t ext4 /dev/sdb1 new_disk/
- 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
- $ sudo mkfs.ext4 /dev/sdb1
- mke2fs 1.46.5 (30-Dec-2021)
- Creating filesystem with 1310464 4k blocks and 327680 inodes
- Filesystem UUID: 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea
- Superblock backups stored on blocks:
- 32768, 98304, 163840, 229376, 294912, 819200, 884736
-
- Allocating group tables: done
- Writing inode tables: done
- Creating journal (16384 blocks): done
- Writing superblocks and filesystem accounting information: done
进入new_disk写东西测试一下
- ~/new_disk$ touch hello
- touch: cannot touch 'hello': Permission denied
修改new_disk文件夹的权限
$ sudo chmod o+w new_disk
- $ll
- drwxr-xrwx 3 root root 4096 8月 9 10:30 new_disk/
new_disk已经具备其他用户的写权限了。也可以将new_disk转换成当前用户吗,试试看
sudo chown -R lkmao new_disk/
- $ls -l
- 4 drwxr-xrwx 3 lkmao root 4096 8月 9 10:30 new_disk
把组归属也改一下,如下,组名也改成lkmao了
- $ sudo chgrp -R lkmao new_disk/
- $ ll
- drwxr-xrwx 3 lkmao lkmao 4096 8月 9 10:30 new_disk/
- :~$ cd new_disk/
- :~/new_disk$ echo "hello world" > hello
- :~/new_disk$ cat hello
- hello world
也可以使用chown一次修改所属用户和所属组
$ sudo chown lkmao:lkmao new_disk -R
磁盘添加测试完毕
- $ df -h
- Filesystem Size Used Avail Use% Mounted on
- tmpfs 390M 3.4M 387M 1% /run
- /dev/sda3 78G 35G 39G 48% /
- tmpfs 2.0G 0 2.0G 0% /dev/shm
- tmpfs 5.0M 4.0K 5.0M 1% /run/lock
- /dev/sda2 512M 5.3M 507M 2% /boot/efi
- tmpfs 390M 2.4M 388M 1% /run/user/1000
- /dev/sr0 3.5G 3.5G 0 100% /media/lkmao/Ubuntu 22.04 LTS amd64
- /dev/sdb1 4.9G 28K 4.6G 1% /home/lkmao/new_disk
上面的信息可以 /dev/sdb1 挂
- $ ls -la
- total 28
- drwxr-xrwx 3 lkmao lkmao 4096 8月 9 10:39 .
- drwxr-x--- 19 lkmao lkmao 4096 8月 9 10:51 ..
- -rw-r--r-- 1 lkmao lkmao 12 8月 9 10:38 hello
- drwx------ 2 lkmao lkmao 16384 8月 9 10:28 lost+found
听说只要看见lost+found就是挂载成功了
开启自动挂载的时候,要使用磁盘的UUID不能使用磁盘名,因为名字是可变的,首先获取UUID
- $ ls /dev/disk/by-uuid/ -ls
- total 0
- 0 lrwxrwxrwx 1 root root 9 8月 9 10:46 2022-04-19-10-23-19-00 -> ../../sr0
- 0 lrwxrwxrwx 1 root root 10 8月 9 10:46 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea -> ../../sdb1
- 0 lrwxrwxrwx 1 root root 10 8月 9 10:46 6220693a-d997-4b19-bde1-b0d8b1ecfde6 -> ../../sda3
- 0 lrwxrwxrwx 1 root root 10 8月 9 10:46 8CC7-4130 -> ../../sda2
修改fstab,新增一个UUID
sudo vi /etc/fstab
- # /etc/fstab: static file system information.
- #
- # Use 'blkid' to print the universally unique identifier for a
- # device; this may be used with UUID= as a more robust way to name devices
- # that works even if disks are added and removed. See fstab(5).
- #
- # <file system> <mount point> <type> <options> <dump> <pass>
- # / was on /dev/sda3 during installation
- UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
- # /boot/efi was on /dev/sda2 during installation
- UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
- /swapfile none swap sw 0 0
从文件里面的内容可以,sda3已经被挂载到了根目录下,在/etc/fstab底部新增内容如下所示:
-
- UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1
然后保存/etc/fstab,重启系统
重新登录系统,进入挂载点,读取文件,读取成功,说明磁盘成功自动挂载了。
- $ cd new_disk/
- :~/new_disk$ ls
- hello lost+found
- :~/new_disk$ cat hello
- hello world
这一节主要验证不使用fdisk分区到底能不能添加磁盘,实验证明,是没问题的。
创建一个新磁盘
- lkmao@lkmao-virtual-machine:/$ mkdir disk_1g
- mkdir: cannot create directory ‘disk_1g’: Permission denied
- lkmao@lkmao-virtual-machine:/$
- lkmao@lkmao-virtual-machine:/$ sudo mkdir disk_1g
- [sudo] password for lkmao:
- lkmao@lkmao-virtual-machine:/$ ls /dev/sd*
- /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1 /dev/sdc
- lkmao@lkmao-virtual-machine:/$ sudo mkfs.ext4 /dev/sdc
- mke2fs 1.46.5 (30-Dec-2021)
- Creating filesystem with 262144 4k blocks and 65536 inodes
- Filesystem UUID: 56b4f3e4-142b-4898-8046-9aab07952dbc
- Superblock backups stored on blocks:
- 32768, 98304, 163840, 229376
-
- Allocating group tables: done
- Writing inode tables: done
- Creating journal (8192 blocks): done
- Writing superblocks and filesystem accounting information: done
-
- lkmao@lkmao-virtual-machine:/$ sudo mount -t ext4 /dev/sdc
- mount: /dev/sdc: can't find in /etc/fstab.
- lkmao@lkmao-virtual-machine:/$ sudo mount -t ext4 /dev/sdc /disk_1g/
- lkmao@lkmao-virtual-machine:/$ cd disk_1g/
- lkmao@lkmao-virtual-machine:/disk_1g$ touch hello
- touch: cannot touch 'hello': Permission denied
- lkmao@lkmao-virtual-machine:/disk_1g$ cd ..
- lkmao@lkmao-virtual-machine:/$ sudo chown lkmao:lkmao disk_1g/
- lkmao@lkmao-virtual-machine:/$ sudo chown lkmao:lkmao disk_1g/ -R
- lkmao@lkmao-virtual-machine:/$ cd disk_1g/
- lkmao@lkmao-virtual-machine:/disk_1g$ ls -lsa
- total 24
- 4 drwxr-xr-x 3 lkmao lkmao 4096 8月 9 12:01 .
- 4 drwxr-xr-x 21 root root 4096 8月 9 12:00 ..
- 16 drwx------ 2 lkmao lkmao 16384 8月 9 12:01 lost+found
- lkmao@lkmao-virtual-machine:/disk_1g$ touch hello
- lkmao@lkmao-virtual-machine:/disk_1g$ echo "hello world" > hello
- lkmao@lkmao-virtual-machine:/disk_1g$ ls
- hello lost+found
- lkmao@lkmao-virtual-m
- $ ls /dev/disk/by-uuid/ -ls
- total 0
- 0 lrwxrwxrwx 1 root root 9 8月 9 11:58 2022-04-19-10-23-19-00 -> ../../sr0
- 0 lrwxrwxrwx 1 root root 10 8月 9 11:58 42113c35-e3bb-4e7f-8c45-fd4a2bf308ea -> ../../sdb1
- 0 lrwxrwxrwx 1 root root 9 8月 9 12:01 56b4f3e4-142b-4898-8046-9aab07952dbc -> ../../sdc
- 0 lrwxrwxrwx 1 root root 10 8月 9 11:58 6220693a-d997-4b19-bde1-b0d8b1ecfde6 -> ../../sda3
- 0 lrwxrwxrwx 1 root root 10 8月 9 11:58 8CC7-4130 -> ../../sda2
添加完毕后的fstab
- # /etc/fstab: static file system information.
- #
- # Use 'blkid' to print the universally unique identifier for a
- # device; this may be used with UUID= as a more robust way to name devices
- # that works even if disks are added and removed. See fstab(5).
- #
- # <file system> <mount point> <type> <options> <dump> <pass>
- # / was on /dev/sda3 during installation
- UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
- # /boot/efi was on /dev/sda2 during installation
- UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
- /swapfile none swap sw 0 0
- UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1
- UUID=56b4f3e4-142b-4898-8046-9aab07952db /disk_1g ext4 errors=remount-ro 0 1
保存重启,然后,就没有然后了...
估计是刚才/etc/fstab/输入错误了,按回车,进行命令行,vi /etc/fstab,然后删掉最后一行新添加的,重启就好了,这个刚才操作太快了,忘记截图了,想想也没啥,其实就是一个shell终端,遇到这种情况别慌,把刚刚干的事情改正就行了。
- # /etc/fstab: static file system information.
- #
- # Use 'blkid' to print the universally unique identifier for a
- # device; this may be used with UUID= as a more robust way to name devices
- # that works even if disks are added and removed. See fstab(5).
- #
- # <file system> <mount point> <type> <options> <dump> <pass>
- # / was on /dev/sda3 during installation
- UUID=6220693a-d997-4b19-bde1-b0d8b1ecfde6 / ext4 errors=remount-ro 0 1
- # /boot/efi was on /dev/sda2 during installation
- UUID=8CC7-4130 /boot/efi vfat umask=0077 0 1
- /swapfile none swap sw 0 0
- UUID=42113c35-e3bb-4e7f-8c45-fd4a2bf308ea /home/lkmao/new_disk ext4 errors=remount-ro 0 1
- UUID=56b4f3e4-142b-4898-8046-9aab07952dbc /disk_1g ext4 errors=remount-ro 0 1
这次没问题了
- $ df -h
- Filesystem Size Used Avail Use% Mounted on
- tmpfs 390M 3.4M 387M 1% /run
- /dev/sda3 78G 35G 39G 48% /
- tmpfs 2.0G 0 2.0G 0% /dev/shm
- tmpfs 5.0M 4.0K 5.0M 1% /run/lock
- /dev/sdc 974M 28K 907M 1% /disk_1g
- /dev/sda2 512M 5.3M 507M 2% /boot/efi
- /dev/sdb1 4.9G 28K 4.6G 1% /home/lkmao/new_disk
- tmpfs 390M 2.4M 388M 1% /run/user/1000
- /dev/sr0 3.5G 3.5G 0 100% /media/lkmao/Ubuntu 22.04 LTS amd64
知识要反复验证,纸上得来终觉浅,绝知此事要躬行。
任何看似简单的事情,做起来的时候,都不简单,上面这些操作涉及了很多的命令,如果能操作一遍,对于熟悉这些命令是很有帮助的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。