Linux VPS 挂载磁盘

一、扩展 CentOS 系统磁盘大纲

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#查看当前硬盘和分区信息
lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 200G 0 disk
└─sdb1 8:17 0 200G 0 part
sr0 11:0 1 1024M 0 rom


#创建新的物理卷PV
pvcreate /dev/sdb1

#若提示格式盘符不符标准请使用fdisk
WARNING: ntfs signature detected on /dev/sdb1 at offset 3. Wipe it? [y/n]
fdisk /dev/sdb

#查看详细信息
p

磁盘标签类型:dos
磁盘标识符:0x0a904f37

设备 Boot Start End Blocks Id System
/dev/sdb1 2048 419430399 209714176 7 HPFS/NTFS/exFAT

#使用change a partition's system id更改System
t

#更改目的Hex选择Linux LVM
8e

#保存配置项
w

#继续创建物理卷PV,以下两个警告可以yes忽略
pvcreate /dev/sdb1
WARNING: ntfs signature detected on /dev/sdb1 at offset 3. Wipe it? [y/n]
y
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]
y
Physical volume "/dev/sdb1" successfully created.

#成功后查看PVS物理卷信息
pvs

#查看现扩展的VG卷组(vgdisplay/vgs)
vgdisplay

--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size <99.00 GiB
PE Size 4.00 MiB
Total PE 25343
Alloc PE / Size 25342 / 98.99 GiB
Free PE / Size 1 / 4.00 MiB
VG UUID 5GZJnr-E1gX-xxxx-xxxx-xxxx-Id1m-YoK1Jf

#使用输出的VG NAME扩展卷组
vgextend centos /dev/sdb1

#查看现LV逻辑卷
lvdisplay

--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID bxJPDc-TeYX-xxxx-xxxx-xxxx-ZRJH-at3eah
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 2
LV Size <3.88 GiB
Current LE 992
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1

--- Logical volume ---
LV Path /dev/centos/home
LV Name home
VG Name centos
LV UUID OptJBh-cD7A-xxxx-xxxx-xxxx-E5pn-saZCtr
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 1
LV Size <45.12 GiB
Current LE 11550
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2

--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID KnPGiV-1eJp-xxxx-xxxx-xxxx-V6Ps-bdQ7l7
LV Write Access read/write
LV Creation host, time localhost, 2021-12-22 13:02:25 +0800
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12800
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

#通常root根目录对应的逻辑卷进行全额不分区扩展
lvextend -l +100%FREE /dev/centos/root

#查看文件系统
df -TH -h

devtmpfs devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs tmpfs 7.8G 8.7M 7.8G 1% /run
tmpfs tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 50G 3.5G 47G 7% /
/dev/sda1 xfs 1014M 171M 844M 17% /boot
/dev/mapper/centos-home xfs 46G 100M 45G 1% /home
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0

#使用XFS文件系统命令扩展
xfs_growfs /

#验证结果
df -h

二、具体挂载 EXT3/4 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
[root@MyVPS ~]# fdisk -l (查看磁盘分区)
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/xvdb doesn't contain a valid partition table
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
[root@MyVPS ~]# df -hal //*查看已划分区空间使用情况*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
[root@MyVPS ~]# fdisk /dev/xvdb //*对硬盘/dev/xvdb进行增加分区操作*//
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help): n //*输入n新建分区*//
Command action
e extended
p primary partition (1-4)
p //*输入p主分区*//
Partition number (1-4): 1 //*输入1第一个分区*//
First cylinder (1-913, default 1): //*回车跳过*//
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-913, default 913): //*回车跳过*//
Using default value 913
Command (m for help): w //*输入W保存退出*//
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@MyVPS ~]# mkfs -t ext3 /dev/xvdb1 //*将新分区xvdb1(此名称因系统而异,后缀+1)格式化为ext3格式*//
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
917504 inodes, 1833410 blocks
91670 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1879048192
56 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@MyVPS ~]# mount /dev/xvdb1 /home //*将新分区xvdb1挂载到目录/home*//
[root@MyVPS ~]# df -hal
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
/dev/xvdb1 6.9G 144M 6.4G 3% /home
[root@MyVPS ~]# echo "/dev/xvdb1 /home ext3 defaults 1 2" >> /etc/fstab //*添加开机自动挂载*//
//*至此硬盘挂载成功*//

三、具体挂载 LVM 格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
[root@MyVPS ~]# fdisk -l //*查看硬盘分区情况*//
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/xvdb doesn't contain a valid partition table
[root@MyVPS ~]#df -hal //*查看已划分区空间使用情况*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
9.3G 1.2G 7.7G 13% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
[root@MyVPS ~]#fdisk /dev/xvdb //*对硬盘/dev/xvdb进行增加分区操作*//
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help):n //*输入n新建分区*//
Command action
e extended
p primary partition (1-4)
p //*输入p,主分区*//
Partition number (1-4): 1 //*由于xvdb还没有分区.我们这里输入1.第1个分区*//
First cylinder (1-913, default 1)://*回车跳过*//
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-913, default 913)://*回车跳过*//
Using default value 913
Command (m for help): t //*通过t来指定分区系统格式*//
Selected partition 1
Hex code (type L to list codes):8e //*输入8e LVM系统格式*//
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help):w //*输入w保存退出*//
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@MyVPS ~]#reboot //*重启使之生效*//
Broadcast message from root (pts/0) (Mon Aug 15 00:43:03 2011):
The system is going down for reboot NOW!
[root@MyVPS ~]#
Connection closed by foreign host.
Type `help' to learn how to use Xshell prompt.
Xshell:\>
Connecting to :22...
Connection established.
Escape character is '^@]'.
Last login: Mon Aug 15 00:41:06 2011 from
[root@MyVPS1280 ~]#fdisk -l //*查看硬盘分区情况*//
Disk /dev/xvda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvda1 * 1 13 104391 83 Linux
/dev/xvda2 14 1044 8281507+ 8e Linux LVM
/dev/xvda3 1045 1305 2096482+ 8e Linux LVM
Disk /dev/xvdb: 7516 MB, 7516192768 bytes
255 heads, 63 sectors/track, 913 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/xvdb1 1 913 7333641 8e Linux LVM
[root@MyVPS1280 ~]#pvcreate /dev/xvdb1 //*创建物理卷*//
Physical volume "/dev/xvdb1" successfully created
[root@MyVPS1280 ~]#vgextend VolGroup00 /dev/xvdb1 //*将物理卷加入到组VolGroup00 *//
Volume group "VolGroup00" successfully extended
[root@MyVPS1280 ~]# vgdisplay //*查看物理卷组情况.可以看到我们有6.97G的容量可以扩展*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 315 / 9.84 GB
Free PE / Size 223 / 6.97 GB
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# lvresize -L +6G /dev/VolGroup00/LogVol00 //*扩容6G(我们第一次加6G)*//
Extending logical volume LogVol00 to 15.59 GB
Logical volume LogVol00 successfully resized
[root@MyVPS1280 ~]#resize2fs /dev/VolGroup00/LogVol00 //*动态扩容分区大小*//
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 4087808 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 4087808 blocks long.
[root@MyVPS1280 ~]# vgdisplay //*查看物理卷组情况.可以看到我们还有992M的容量可以扩展*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 507 / 15.84 GB
Free PE / Size 31 / 992.00 MB
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# lvresize -L +992M /dev/VolGroup00/LogVol00 //*扩容992M(我们第2次加992M)*//
Extending logical volume LogVol00 to 16.56 GB
Logical volume LogVol00 successfully resized
[root@MyVPS1280 ~]# resize2fs /dev/VolGroup00/LogVol00 //*动态扩容分区大小*//
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 4341760 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 4341760 blocks long.
[root@MyVPS1280 ~]#vgdisplay //*最后再查看扩容完没有 可以看到已经扩容完毕*//
--- Volume group ---
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 3
Act PV 3
VG Size 16.81 GB
PE Size 32.00 MB
Total PE 538
Alloc PE / Size 538 / 16.81 GB
Free PE / Size 0 / 0
VG UUID ONGPxy-HBvY-xrrQ-IjEW-PIiO-2cX7-tg6tuI
[root@MyVPS1280 ~]# df -hal //*查看已划分区空间使用情况 可以看到我们挂载成功*//
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
17G 1.2G 15G 8% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
/dev/xvda1 99M 29M 66M 31% /boot
tmpfs 151M 0 151M 0% /dev/shm
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
//*至此硬盘挂载成功*//

常见问题:

Q:挂载过程中错误如何卸载

1
2
3
4
5
6
mount /dev/xvdb1 /mnt
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 40G 1.5G 36G 4% /
tmpfs 498M 0 498M 0% /dev/shm
/dev/xvdb1 5.0G 139M 4.6G 3% /mnt

使用命令卸载:umount /dev/xvdb1  假如提示设备忙碌,无法卸载,请尝试硬卸载  fuser -km /dev/xvdb1

作者

Catooilg

发布于

2020-09-02

更新于

2025-02-10

许可协议

评论