阿里云 磁盘挂载 教程
在阿里云购买的云计算主机默认是不挂载数据盘的。在登录后,root用户的根目录下面有一个auto_fdisk.sh的 阿里云 磁盘挂载 脚本,可以直接使用;如果需要自定义磁盘的分区和功能,那么必须要手动操作磁盘挂载了。下文将为各位介绍如何在centos(以阿里云为例)系统下对磁盘进行初始化、分区、挂载、启动挂载等操作;以及如何制作、挂载swap分区。
一、fdisk查看 阿里云 磁盘挂载 情况
[root@www.cnphp.info ~]$ fdisk -l
Disk /dev/hda: 21.4 GB, 21474836480 bytes
224 heads, 56 sectors/track, 3343 cylinders
Units = cylinders of 12544 * 512 = 6422528 bytesDevice Boot Start End Blocks Id System
/dev/hda1 * 1 3343 20967268 83 LinuxDisk /dev/xvdc: 10.7 GB, 10737418240 bytes
224 heads, 56 sectors/track, 1671 cylinders
Units = cylinders of 12544 * 512 = 6422528 bytesDisk /dev/xvdc doesn’t contain a valid partition table
Disk /dev/xvdb: 5368 MB, 5368709120 bytes
224 heads, 56 sectors/track, 835 cylinders
Units = cylinders of 12544 * 512 = 6422528 bytesDisk /dev/xvdb doesn’t contain a valid partition table
可以看到在阿里云计算服务器中有两块磁盘(/dev/xvdb /dev/xvdc)没有分区信息,需要使用fdisk对磁盘进行初始化分区工作。
二、fdisk 初始化磁盘、分区
[root@www.cnphp.info ~]$ fdisk /dev/xvdc
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.
The number of cylinders for this disk is set to 1671.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Warning: invalid flag 0×0000 of partition table 4 will be corrected by w(rite)Command (m for help): n
Command action
e extended
p primary partition (1-4)
p #新建主分区
Partition number (1-4): 1 #分区序号1-4选择
First cylinder (1-1671, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1671, default 1671):
Using default value 1671Command (m for help): w
The partition table has been altered!Calling ioctl() to re-read partition table.
Syncing disks.
蓝色加粗字体为需要输入的命令部分。上面的脚本初始化 /dev/xvdc 磁盘,并将整个磁盘格式化为一个分区;命令的描述如下:
Command (m for help): m <== 输入m可以查看fdisk的具体命令说明。
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition <==删除一个分区
l list known partition types
m print this menu
n add a new partition <==新建一个分区
o create a new empty DOS partition table
p print the partition table <==在命令行总输出分区表信息
q quit without saving changes <==不保存分区修改,离开fdisk
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit <==保存分区修改,并退出fdisk
x extra functionality (experts only)
上面的动作完成后,再次使用fdisk查看磁盘分区情况,是不是不一样了呢^_^
[root@www.cnphp.info ~]$ fdisk -l
Disk /dev/hda: 21.4 GB, 21474836480 bytes
224 heads, 56 sectors/track, 3343 cylinders
Units = cylinders of 12544 * 512 = 6422528 bytesDevice Boot Start End Blocks Id System
/dev/hda1 * 1 3343 20967268 83 LinuxDisk /dev/xvdc: 10.7 GB, 10737418240 bytes
224 heads, 56 sectors/track, 1671 cylinders
Units = cylinders of 12544 * 512 = 6422528 bytesDevice Boot Start End Blocks Id System
/dev/xvdc1 1 1671 10480484 83 Linux
三、格式化Linux分区
/dev/xvdc1 就是刚刚我们新建的分区,但是还不能使用哦。需要格式化成需要的磁盘格式如 ext3 才能进行读写操作哦。
[root@www.cnphp.info ~]$ mkfs -t ext3 /dev/xvdc1
#将分区/dev/xvdc1格式化为ext3磁盘格式
[root@www.cnphp.info ~]$ mke2fs -b 4096 /dev/xvdc1
#如果需要4k对齐,这个指令比较有用
[root@www.cnphp.info ~]$ dumpe2fs /dev/xvdc1 | more
#查看4k对齐情况。
Block size: 4096
Fragment size: 4096
如果需要制作swap分区,怎么弄呢?很简单mkswap即可。
[root@www.cnphp.info ~]$ mkswap /dev/xvdc1
#现在把/dev/xvdc1 制作为swap格式。
[root@www.cnphp.info ~]$ swapon /dev/xvdc1
#挂载swap分区
[root@www.cnphp.info ~]$ free
#查看swap是否已经挂载完毕
total used free shared buffers cached
Mem: 509272 185724 323548 0 66844 68056
-/+ buffers/cache: 50824 458448
Swap: 1003512 44344 959168
[root@www.cnphp.info ~]$ swapoff /dev/xvdc1
#卸载swap分区
四、启动挂载分区制作
现在,要让刚刚制作好的分区能够在启动时直接挂载上,而不是每次使用mount命令挂载。
[root@www.cnphp.info ~]$ e2label /dev/xvdc1 /home
#制作启动挂载时要用的磁盘label
[root@www.cnphp.info ~]$ vim /etc/fstab
#编辑启动分区表
关于此表的说明及用法,笔者不想在这里赘述了,大家可以google或百度下。
LABEL=/ / ext3 defaults 1 1 LABEL=/home /home ext3 defaults 1 2 LABEL=/static /static ext3 defaults 1 2 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 /dev/xvdb2 swap swap defaults 0 0