20200118 在阿里云 ECS linux 上尝试云盘卸载、挂载操作,先在本地 virtualbox 上尝试和学习
1、停止虚拟机,设置–存储–增加虚拟硬盘
** 注意:要选 控制器 SATA
选择 创建新的虚拟盘(使用现有磁盘的话,后续就不用格式化、挂载了)
** 因为是学习,所以,全部使用缺省,一路回车即可
2、挂载新硬盘
-
查看文件系统 (file system)
df -h
Filesystem Size Used Avail Use% Mounted on udev 1.9G 0 1.9G 0% /dev tmpfs 395M 1008K 394M 1% /run /dev/sda2 98G 11G 83G 11% / tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/loop0 91M 91M 0 100% /snap/core/6405 /dev/loop1 90M 90M 0 100% /snap/core/6673 /dev/loop2 92M 92M 0 100% /snap/core/6531 Downloads 234G 171G 63G 74% /media/sf_Downloads tmpfs 395M 0 395M 0% /run/user/1000
没有看到我们新建的硬盘,看不到分区信息
-
查看块设备 (block device)
lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT loop0 squashfs /snap/core/6405 loop1 squashfs /snap/core/6673 loop2 squashfs /snap/core/6531 sda ├─sda1 └─sda2 ext4 c9db3d96-f9f2-11e8-b72e-0800274e1103 / sdb sr0 iso9660 VBox_GAs_5.2.22 2018-11-08-19-58-33-33
最后部分 sdb 就是刚刚新增加的硬盘,可以看到 FSTYPE 和 UUID、MOUNTPOINT 都是空白,看不到分区信息
-
查看分区信息
$ sudo fdisk -l
Disk /dev/loop0: 91 MiB, 95416320 bytes, 186360 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/loop1: 89.3 MiB, 93581312 bytes, 182776 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/loop2: 91.1 MiB, 95522816 bytes, 186568 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/sda: 100 GiB, 107374182400 bytes, 209715200 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 Disklabel type: gpt Disk identifier: 82F6786F-CA1E-4E0B-9D35-560A90F23622 Device Start End Sectors Size Type /dev/sda1 2048 4095 2048 1M BIOS boot /dev/sda2 4096 209713151 209709056 100G Linux filesystem Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
就是刚刚新增加的硬盘,没有Start End Sectors Size Type 信息
也就是分区信息是空白 -
给硬盘 /dev/sdb 创建新分区
$ sudo fdisk /dev/sdbChanges 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 0xd97b7fdf. Command (m for help):
m 表示命令行提示,每次输入 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
Command (m for help):
其中, 输入 n add a new partition (新建一个分区)
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
分区类型直接回车,缺省即可
Using default response p.
Partition number (1-4, default 1):
分区计数,缺省从 1 开始(前一次理解成:个数,错了!)
Using default response p.
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519):
Created a new partition 1 of type 'Linux' and of size 10 GiB.
Command (m for help):
起始扇区和结束扇区 (sector)也缺省
最后,又来到了 Command (m for help):
Save & Exit
w write table to disk and exit
q quit without saving changes
输入 w 写入磁盘分区表,生效!
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
- 查看分区结果,确认!
$ df -h
…
和刚才一样,没有出现 sdb1
分别查询一下文件系统
$ sudo file -s /dev/sda1
/dev/sda1: data
$ sudo file -s /dev/sda2
/dev/sda2: Linux rev 1.0 ext4 filesystem data, UUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 (needs journal recovery) (extents) (64bit) (large files) (huge files)
$ sudo file -s /dev/sdb1
/dev/sdb1: data
可见,sdb1 和 sda1 是一样的,没有格式化成为 Linux 文件系统
$ lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
loop0 squashfs /snap/core/6405
loop1 squashfs /snap/core/6673
loop2 squashfs /snap/core/6531
sda
├─sda1
└─sda2 ext4 c9db3d96-f9f2-11e8-b72e-0800274e1103 /
sdb
└─sdb1
sr0 iso9660 VBox_GAs_5.2.22 2018-11-08-19-58-33-33
可以看到 , sdb1 的 FSTYPE 还是空白,sda2 的格式是 ext4
$ sudo fdisk -l
...
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xd97b7fdf
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 20971519 20969472 10G 83 Linux
这次最后部分可以看到 /dev/sdb1 的分区信息 2048 20971519 20969472 10G 83 Linux
但是没有文件系统信息
3、格式化新硬盘
-
格式化新硬盘/dev/sdb1 ,格式: ext4
$ sudo mkfs -t ext4 /dev/sdb1
mke2fs 1.44.1 (24-Mar-2018) Creating filesystem with 2621184 4k blocks and 655360 inodes Filesystem UUID: b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done
-
df -h 还是看不到 sdb1 的文件系统格式,因为该硬盘还没有被挂载
-
查看硬盘 /dev/sdb1 的文件系统
$ sudo file -s /dev/sdb1
/dev/sdb1: Linux rev 1.0 ext4 filesystem data, UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 (extents) (64bit) (large files) (huge files)
-
查看块设备信息
$ lsblk -fNAME FSTYPE LABEL UUID MOUNTPOINT loop0 squashfs /snap/core/6405 loop1 squashfs /snap/core/6673 loop2 squashfs /snap/core/6531 sda ├─sda1 └─sda2 ext4 c9db3d96-f9f2-11e8-b72e-0800274e1103 / sdb └─sdb1 ext4 b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 sr0 iso9660 VBox_GAs_5.2.22 2018-11-08-19-58-33-33
可以看到新硬盘已经被正确的格式化成为 Linux 系统格式 (ext4)了
4、 挂载新硬盘 sdb1
-
建立挂载点 (挂载目录)
在根目录上创建一个新的挂载目录/data02
sudo mkdir /data02 -
将新磁盘分区挂载到/data02目录下
sudo mount /dev/sdb1 /data02 -
检查文件系统
$ df -lFilesystem 1K-blocks Used Available Use% Mounted on udev 1988768 0 1988768 0% /dev tmpfs 403972 1016 402956 1% /run /dev/sda2 102684600 10562244 86863248 11% / tmpfs 2019848 0 2019848 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 2019848 0 2019848 0% /sys/fs/cgroup /dev/loop0 93184 93184 0 100% /snap/core/6405 /dev/loop1 91392 91392 0 100% /snap/core/6673 /dev/loop2 93312 93312 0 100% /snap/core/6531 Downloads 244912536 178979984 65932552 74% /media/sf_Downloads tmpfs 403968 0 403968 0% /run/user/1000 /dev/sdb1 10254612 36888 9677104 1% /data02
可以看到,/dev/sdb1 已经存在于当前文件系统了
-
测试实际使用
$ cd /data02
$ ls
lost+found
$ sudo touch test20200119.txt
$ sudo vim test20200119.txt
输入一些内容,
$ cat test20200119.txt
确认是可以使用的! -
重启试试
目前虽然已经挂载了新硬盘 sdb1,但是,并没有设置为开机挂载,所以,重启之后,应该是看不到该硬盘的
$ sudo reboot
5、设置为开机挂载
** 接上:没有设置,直接重启之后, df -h 是看不到 sdb1 的
** 不同的系统文件内容可能会有些差别,我这里是 Ubuntu 18.04
-
查询 sdb1 的 UUID ,后续需要使用
$ blkid
/dev/sr0: UUID="2018-11-08-19-58-33-33" LABEL="VBox_GAs_5.2.22" TYPE="iso9660" /dev/sda2: UUID="c9db3d96-f9f2-11e8-b72e-0800274e1103" TYPE="ext4" PARTUUID="e4d00f7b-dcc8-42a4-b9cf-1bacf911c6ce" /dev/sdb1: UUID="b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6" TYPE="ext4" PARTUUID="d97b7fdf-01"
或者,
$ cd /dev/disk
$ lsby-id by-label by-partuuid by-path by-uuid
$ ls by-uuid/ -l
total 0 lrwxrwxrwx 1 root root 9 Jan 19 15:18 2018-11-08-19-58-33-33 -> ../../sr0 lrwxrwxrwx 1 root root 10 Jan 19 15:19 b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 -> ../../sdb1 lrwxrwxrwx 1 root root 10 Jan 19 15:19 c9db3d96-f9f2-11e8-b72e-0800274e1103 -> ../../sda2
-
增加配置
sudo vim /etc/fstabUUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 / ext4 defaults 0 0 /swap.img none swap sw 0 0 # by wzh 20190102 restart error! # 192.168.1.191:/home /common/home # 192.168.1.191:/var/nfs/general /common/general 192.168.1.191:/home /common/home nfs defaults 0 0 192.168.1.191:/var/nfs/general /common/general nfs defaults 0 0
后面部分是从前设置共享磁盘留下的,和本次无关
其中总共 6 个字段参数,具体意义我也没有太明白
照着第一行照猫画虎,增加一行
UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 /data02 ext4 defaults 0 0
完成之后,内容如下
UUID=c9db3d96-f9f2-11e8-b72e-0800274e1103 / ext4 defaults 0 0
# add new HDD by wzh 20200119
UUID=b3a3cec3-93bb-4d8d-95cf-cc9aadd9f4a6 /data02 ext4 defaults 0 0
/swap.img none swap sw 0 0
# by wzh 20190102 restart error!
# 192.168.1.191:/home /common/home
# 192.168.1.191:/var/nfs/general /common/general
# del by wzh 20200119 ,do not mount common
# 192.168.1.191:/home /common/home nfs defaults 0 0
# 192.168.1.191:/var/nfs/general /common/general nfs defaults 0 0
-
sudo mount -a 生效
因为实验用的虚拟机刚才有以前的共享目录设置,所以, mount -a 一直不能结束,注释掉之后,ok! -
重启之后验证
$ df -l
Filesystem 1K-blocks Used Available Use% Mounted on udev 1988768 0 1988768 0% /dev tmpfs 403972 1012 402960 1% /run /dev/sda2 102684600 10553136 86872356 11% / tmpfs 2019848 0 2019848 0% /dev/shm tmpfs 5120 0 5120 0% /run/lock tmpfs 2019848 0 2019848 0% /sys/fs/cgroup /dev/loop0 93184 93184 0 100% /snap/core/6405 /dev/loop2 91392 91392 0 100% /snap/core/6673 /dev/loop1 93312 93312 0 100% /snap/core/6531 /dev/sdb1 10254612 36896 9677096 1% /data02 Downloads 244912536 179000660 65911876 74% /media/sf_Downloads tmpfs 403968 0 403968 0% /run/user/1000
其中新硬盘信息:
/dev/sdb1 10254612 36896 9677096 1% /data02
查看刚才测试的文件
$ cd /data02
dhbm@ubuntu1804192:/data02$ ls
lost+found test test20200119.txt
6、 总结
Linux 挂载新硬盘的路线
- 挂接新硬盘
- 查看分区,如果没有或者不支持,则创建新分区
- 格式化分区
- 创建挂载点(目录)
- 挂载设备到挂载点
- 如果需要开机挂载,修改 /etc/fstab 配置
7、后记
-
20200120 上阿里云 ECS 查看以前的云盘分区信息,发现 磁盘标签类型(Disklabel typ)都是 dos
我自己的 virtualbox 里面 Disk /dev/sda 的类型是 gpt
大于 2 T 磁盘的时候再说了! -
再次追加一个磁盘,分区的时候做成了 extend ,显示 fdisk -l 中是 sdc 下的 sdc1 ,然后
格式化的时候, 故意 sudo mkfs -t ext4 /dev/sdc 漏掉 sdc1,直接格式化 sdc ,回头在看的时候,发现 sdc1 没有了 -
重新分区sdc (sudo fdisk /dev/sdc),这次故意制作了一个小的分区,并且
分区计数,缺省从 1 开始(前一次理解成:个数,错了!)
Using default response p.
Partition number (1-4, default 1):
输入 4
,然后,完成之后,继续 n 新建分区,这次提示的是逻辑分区
Command (m for help): n
Partition type
p primary (0 primary, 1 extended, 3 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (4096-12048, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-12048, default 12048):
Created a new partition 5 of type ‘Linux’ and of size 3.9 MiB.
以上完成之后,是这样子的
Device Boot Start End Sectors Size Id Type
/dev/sdc4 2048 12048 10001 4.9M 5 Extended
/dev/sdc5 4096 12048 7953 3.9M 83 Linux
- 说明富余的地方还没有分区,再次 sudo fdisk /dev/sdc
这次全部按照缺省,不是创建的 extend,也不是 logical 逻辑分区,直接分区剩余部分,最后结果是这样子的
Device Boot Start End Sectors Size Id Type
/dev/sdc1 12288 20971519 20959232 10G 83 Linux
/dev/sdc4 2048 12048 10001 4.9M 5 Extended
/dev/sdc5 4096 12048 7953 3.9M 83 Linux
sdc1 是最后这次操作的结果
- linux 上的分区和早先 dos 操作很想,所以,也很复杂!所以,一般都是简单的处理:附加的磁盘整个就是一个 primary !