linux主机磁盘分区格式化并挂载(以centos6.8为例)
1、创建一块20G硬盘通过控制台加载到主机(步骤省略)
2、登陆到linux主机(步骤省略)
3、使用fdisk -l命令列出所有磁盘信息,找到新增的磁盘盘符sdc,sda表示系统盘
[root@i-pn7p9g0k ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee761
Device Boot Start End Blocks Id System
/dev/sda1 * 1 2611 20970496 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
34 heads, 61 sectors/track, 1011 cylinders
Units = cylinders of 2074 * 512 = 1061888 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
4、使用fdisk /dev/sdc命令对磁盘分区格式化,依次输入n p 1 回车两次,按w保存
[root@i-pn7p9g0k ~]# fdisk /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x914d91f4.
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)
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-20480, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-20480, default 20480):
Using default value 20480
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
5、使用fdisk -l命令 查看分区是否创建完成
Disk /dev/sdc: 21.5 GB, 21474836480 bytes
64 heads, 32 sectors/track, 20480 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x914d91f4
Device Boot Start End Blocks Id System
/dev/sdc1 1 20480 20971504 83 Linux
6、执行partprobe命令,将新的分区表变更同步至操作系统。
[root@i-pn7p9g0k ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy). As a result, it may not reflect all of your changes until after reboot.
7、使用mkfs命令对磁盘分区sdc1进行格式化,比如格式化成ext4格式
[root@i-pn7p9g0k ~]# mkfs.ext4 /dev/sdc1
mke2fs 1.41.12 (17-May-2010)
Discarding device blocks: done
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242876 blocks
262143 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
8、使用mkdir命令创建一个挂载目录 /mnt/sdc
mkdir -p /mnt/sdc
9、使用mount命令将磁盘分区挂载到目录
mount /dev/sdc1 /mnt/sdc
10、使用df命令查看挂载结果
[root@i-pn7p9g0k ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 22G 858M 20G 5% /
tmpfs tmpfs 523M 0 523M 0% /dev/shm
/dev/sdc1 ext4 22G 47M 20G 1% /mnt/sdc
linux主机磁盘分区设置UUID自动挂载(以centos6.8为例)
1、blkid /dev/sdc1” 命令,得到磁盘的 UUID
[root@i-pn7p9g0k ~]# blkid /dev/sdc1
/dev/sdc1: UUID=“f933290c-8266-4f3b-95ca-e19e36eb2b05” TYPE="ext4"
2、使用vi命令编辑fstab文件,在最后一行添加以下内容
UUID=f933290c-8266-4f3b-95ca-e19e36eb2b05 /mnt/sdc ext4 defaults 0
linux主机磁盘分区设置磁盘扩容(以centos6.8为例)
1、登陆到主机,执行umount命令卸载硬盘
[root@i-pn7p9g0k ~]# umount /dev/sdc1
[root@i-pn7p9g0k ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 20G 887M 18G 5% /
tmpfs tmpfs 499M 0 499M 0% /dev/shm
2、通过控制台卸载硬盘,并扩容硬盘的容量
3、主机加载扩容以后的硬盘
4、使用fdisk -l命令列出磁盘的信息
[root@i-pn7p9g0k ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee761
Device Boot Start End Blocks Id System
/dev/sda1 * 1 2611 20970496 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
34 heads, 61 sectors/track, 1011 cylinders
Units = cylinders of 2074 * 512 = 1061888 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdc: 107.4 GB, 107374182400 bytes
64 heads, 32 sectors/track, 102400 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x914d91f4
Device Boot Start End Blocks Id System
/dev/sdc1 1 20480 20971504 83 Linux
5、执行fdisk /dev/sdc命令之后,进入fdisk分区工具,并输入“ d”,删除原来的分区sdc1
[root@i-pn7p9g0k ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).
Command (m for help): d
Selected partition 1
备注:删除分区不会导致数据盘内数据的丢失
6、输入“ n”,按“ Enter”,开始新建分区。输入“ n”表示新增一个分区。
表示磁盘有两种分区类型:
“ p”表示主要分区。
“ e”表示扩展分区。
7、依次输入p,此处分区类型需要与原分区保持一致,以原分区类型是主要分区为例,输入“ p”,按“ Enter”,开始重新创建一个主分区。
8、输入分区编号,此处分区编号需要与原分区保持一致,以原分区编号是“ 1”为例,输入分区编号“ 1”,按“ Enter”。
“ First sector”表示初始磁柱值。
“ Last sector”表示截止磁柱值。
请注意以下操作会导致数据丢失:
1、选择的初始磁柱值与原分区的不一致。
2、 选择的截止磁柱值小于原分区的值。
9、输入起始磁柱值,此处必须与原分区保持一致,以步骤1中记录的初始磁柱值1为例,按“ Enter”。
10、输入截止磁柱值,此处截止磁柱值应大于等于步骤1中记录的截止磁柱值,以选择默认截止磁柱值102400为例,按“ Enter”。并输入w回车保存
[root@i-sr6scmbn ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).
Command (m for help): d
Selected partition 1
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-102400, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-102400, default 102400): 102400
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
11、使用fdisk -l命令查看新建分区的详细信息
[root@i-sr6scmbn ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ee761
Device Boot Start End Blocks Id System
/dev/sda1 * 1 2611 20970496 83 Linux
Disk /dev/sdb: 1073 MB, 1073741824 bytes
34 heads, 61 sectors/track, 1011 cylinders
Units = cylinders of 2074 * 512 = 1061888 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/sdc: 107.4 GB, 107374182400 bytes
64 heads, 32 sectors/track, 102400 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x25b0ff4a
Device Boot Start End Blocks Id System
/dev/sdc1 1 102400 104857584 83 Linux
12、执行e2fsck -f命令,检查“ /dev/sdc1”文件系统的正确性。
[root@i-sr6scmbn ~]# e2fsck -f /dev/sdc1
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdc1: 11/1310720 files (0.0% non-contiguous), 126289/5242876 blocks
13、执行resize2fs命令,扩展“ /dev/sdc1”文件系统的大小。
[root@i-sr6scmbn ~]# resize2fs /dev/sdc1
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdc1 to 26214396 (4k) blocks.
The filesystem on /dev/sdc1 is now 26214396 blocks long.
14、执行mount命令,将新建分区挂载到“ /mnt/sdc”目录下
mount /dev/sdc1 /mnt/sdc
15、使用df命令,查看“ /dev/sdc1”分区挂载结果
[root@i-sr6scmbn ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda1 ext4 20G 790M 18G 5% /
tmpfs tmpfs 499M 0 499M 0% /dev/shm
/dev/sdc1 ext4 99G 60M 94G 1% /mnt/sdc