首页 > 代码库 > LINUX创建分区并格式化

LINUX创建分区并格式化

1.创建一个10G分区,并格式为ext4文件系统;

(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA,默认挂载属性包含acl;

(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

fdisk -l 查看当前磁盘分区情况

[root@localhost~]# fdisk -l

 

Disk /dev/sda: 21.5GB, 21474836480 bytes

255 heads, 63sectors/track, 2610 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x000e609a

 

   Device Boot      Start         End      Blocks  Id  System

/dev/sda1   *          1          26      204800  83  Linux

Partition 1 does notend on cylinder boundary.

/dev/sda2              26        1332   10485760   83  Linux

/dev/sda3            1332        2351    8192000   83  Linux

/dev/sda4            2351        2611    2087936    5  Extended

/dev/sda5            2352        2479    1024000   82  Linux swap / Solaris

 

Disk /dev/sdb: 10.8GB, 10844372992 bytes

255 heads, 63sectors/track, 1318 cylinders

Units = cylinders of16065 * 512 = 8225280 bytes

Sector size(logical/physical): 512 bytes / 512 bytes

I/O size(minimum/optimal): 512 bytes / 512 bytes

Disk identifier:0x0000000

在新增的硬盘上新建分区一个10G主分区:

[root@localhost~]# fdisk /dev/sdb

Device containsneither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOSdisklabel with disk identifier 0x6c5ee8d0.

Changes will remainin memory only, until you decide to write them.

After that, ofcourse, the previous content won‘t be recoverable.

 

Warning: invalidflag 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‘) andchange display units to

         sectors (command ‘u‘).

 

Command (m forhelp): n

Command action

   e  extended

   p  primary partition (1-4)

p

Partition number(1-4): 1

First cylinder(1-1318, default 1): 1

Last cylinder,+cylinders or +size{K,M,G} (1-1318, default 1318): +10G

 

Command (m forhelp): w

The partition tablehas been altered!

 

Calling ioctl() tore-read partition table.

Syncing disks.

[root@localhost ~]#

查看创建是否成功:cat  /proc/partitions

(1):要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl

[root@localhost~]# mke2fs -t ext4 -b 2048 -m 2 -L ‘MYDATA‘ /dev/sdb1

mke2fs 1.41.12(17-May-2010)

文件系统标签=MYDATA

操作系统:Linux

块大小=2048(log=1)

分块大小=2048(log=1)

Stride=0 blocks,Stripe width=0 blocks

657408 inodes,5245206 blocks

104904 blocks(2.00%) reserved for the super user

第一个数据块=0

Maximum filesystemblocks=543162368

321 block groups

16384 blocks pergroup, 16384 fragments per group

2048 inodes pergroup

Superblock backupsstored on blocks:

    16384, 49152, 81920, 114688, 147456, 409600,442368, 802816, 1327104,

    2048000, 3981312

 

正在写入inode表: 完成                           

Creating journal(32768 blocks): 完成

Writing superblocksand filesystem accounting information: 完成

查看默认挂载属性:

[root@localhost~]# tune2fs -l /dev/sdb1

tune2fs 1.41.12(17-May-2010)

Filesystem volumename:   MYDATA

Last mountedon:          <not available>

FilesystemUUID:         53f64cbc-fa58-495c-9bcd-89b12745bedb

Filesystem magicnumber:  0xEF53

Filesystem revision#:    1 (dynamic)

Filesystemfeatures:      has_journal ext_attrresize_inode dir_index filetype needs_recov

ery extent flex_bgsparse_super huge_file uninit_bg dir_nlink extra_isizeFilesystem flags:         signed_directory_hash

Defaultmount options:    acl

Filesystemstate:         clean

Errors behavior:          Continue

Filesystem OStype:       Linux

Inode count:              657408

Block count:              5245206

Reserved blockcount:     104904

Free blocks:              5123382

Free inodes:              657397

First block:              0

Block size:               2048

Fragment size:            2048

Reserved GDTblocks:      512

Blocks pergroup:         16384

Fragments pergroup:      16384

Inodes pergroup:         2048

Inode blocks pergroup:   256

Flex block groupsize:    16

Filesystem created:       Wed Apr 5 18:10:34 2017

Last mounttime:          Wed Apr  5 18:25:27 2017

Last writetime:          Wed Apr  5 18:50:49 2017

Mount count:              1

Maximum mountcount:      27

Last checked:             Wed Apr  5 18:10:34 2017

Check interval:           15552000 (6 months)

Next checkafter:         Mon Oct  2 18:10:34 2017

Lifetimewrites:          226 MB

Reserved blocksuid:      0 (user root)

Reserved blocksgid:      0 (group root)

First inode:              11

Inode size:          256

Required extraisize:     28

Desired extraisize:      28

Journal inode:            8

Default directoryhash:   half_md4

Directory HashSeed:     49adf471-138d-47cb-af38-3f1c1107891e

Journal backup:           inode blocks

[root@localhost ~]#

 

(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;

[root@localhost~]# mkdir -p /data/mydata

[root@localhost~]# mount -o noexec,noatime /dev/sdb1 /data/mydata

 

2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;(没有那么大空间,创建30M的swap分区)

[root@localhost~]# fdisk /dev/sdb

 

WARNING: DOS-compatiblemode is deprecated. It‘s strongly recommended to

         switch off the mode (command ‘c‘) andchange display units to

         sectors (command ‘u‘).

 

Command (m forhelp): n

Command action

   e  extended

   p  primary partition (1-4)

p

Partition number(1-4): 2

First cylinder(1307-1318, default 1307):

Using default value1307

Last cylinder,+cylinders or +size{K,M,G} (1307-1318, default 1318): +30M

 

Command (m forhelp): t

Partition number(1-4): 2

Hex code (type L tolist codes): 82

Changed system typeof partition 2 to 82 (Linux swap / Solaris)

 

创建文件系统,并启用。

               

    交换分区的启用和禁用:

        创建交换分区的命令:mkswap

        mkswap [OPTIONS]  device

                -L LABEL:指明卷标

                -f:强制

        启用:swapon

            swapon  [OPTION] [DEVICE]

                -a:定义在/etc/fstab文件中的所有swap设备;

               

        禁用:swapoff

            swapoffDEVICE

[root@localhost~]# mkswap /dev/sdb2

Setting up swapspaceversion 1, size = 40156 KiB

no label,UUID=295c704e-0ed0-4613-b4bd-0db002a6012c

[root@localhost~]# swapon  /dev/sdb2

 

3、写一个脚本

(1)、获取并列出当前系统上的所有磁盘设备;

(2)、显示每个磁盘设备上每个分区相关的空间使用信息;

[root@localhosttmp]# cat disk_test.sh

#!/bin/bash

#

echo"All disks"

fdisk -l |egrep "^Disk[[:space:]]/dev"

echo"disk partition"

df -h|egrep "/dev/sd[a-z]"

[root@localhosttmp]#

 

4、总结RAID的各个级别及其组合方式和性能的不同;

常见的几种:

RAID 0:硬盘轮流读写,读写性能提升,空间每块硬盘直接叠加,无容错能力,最少需要2块盘组合。增加需要2的倍数

 

RAID 1 : 硬盘写时每块硬盘各保存一份,读取性能提升,写性能下降,空间减半,有容错能力,一块坏,不影响另一块使用,最少2块盘,增加需要2的倍数

 

RAID 5: 分布式奇偶校验独立磁盘结构,数据交叉存储,读写性能提升,空间磁盘数量N-1,有容错能力,只能坏1块盘,最少需要3块磁盘组合

 

RAID-6:读、写性能提升,可用空间:(N-2)*min(S1,S2,...),有容错能力:2块磁盘,最少磁盘数:4, 4+

 

RAID-10:读、写性能提升,可用空间:N*min(S1,S2,...)/2,有容错能力:每组镜像最多只能坏一块;最少磁盘数:4, 4+

 

5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k。

    mdadm:模式化的工具

        命令的语法格式:mdadm [mode] <raiddevice> [options] <component-devices>

            支持的RAID级别:LINEAR, RAID0, RAID1,RAID4, RAID5, RAID6, RAID10;

 

        模式:

            创建:-C

            装配: -A

            监控: -F

            管理:-f, -r, -a

 

        <raiddevice>: /dev/md#

        <component-devices>: 任意块设备

 

 

        -C: 创建模式

            -n #: 使用#个块设备来创建此RAID;

            -l #:指明要创建的RAID的级别;

            -a {yes|no}:自动创建目标RAID设备的设备文件;

            -c CHUNK_SIZE: 指明块大小;

            -x #: 指明空闲盘的个数;

            -D:显示raid的详细信息;

            mdadm -D /dev/md#

 

        管理模式:

            -f: 标记指定磁盘为损坏;

            -a: 添加磁盘

            -r: 移除磁盘

 

        观察md的状态:

            cat /proc/mdstat

 

        停止md设备:

            mdadm -S /dev/md#

 

    watch命令:

        -n #: 刷新间隔,单位是秒;

 

        watch -n# ‘COMMAND‘

 

 

条件有限,使用3个磁盘100M来创建。

[root@localhost~]# fdisk /dev/sdc

Device containsneither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOSdisklabel with disk identifier 0xaccd8a6e.

Changes will remainin memory only, until you decide to write them.

After that, ofcourse, the previous content won‘t be recoverable.

 

Warning: invalidflag 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‘) andchange display units to

         sectors (command ‘u‘).

 

Command (m forhelp): n

Command action

   e  extended

   p  primary partition (1-4)

p

Partition number(1-4): 1

First cylinder(1-512, default 1):

Using default value1

Last cylinder,+cylinders or +size{K,M,G} (1-512, default 512): +100M

 

Command (m forhelp): t

Selected partition 1

Hex code (type L tolist codes): fd

Changed system typeof partition 1 to fd (Linux raid autodetect)

 

Command (m forhelp): w

The partition tablehas been altered!

 

Calling ioctl() tore-read partition table.

Syncing disks.

[root@localhost ~]#

其余两个磁盘同样方法创建,得到

[root@localhost~]# fdisk -l |egrep "^/dev/sd[c-e]"

/dev/sdc1               1         101      103408  fd  Linux raid autodetect

/dev/sdd1               1         101      103408  fd  Linux raid autodetect

/dev/sde1               1         101      103408  fd  Linux raid autodetect

 

[root@localhost~]# mdadm -C /dev/md0 -a yes -n 2 -l 1 -c 128 -x 1 /dev/sd[c-e]1

mdadm: Note: thisarray has metadata at the start and

    may not be suitable as a boot device.  If you plan to

    store ‘/boot‘ on this device please ensurethat

    your boot-loader understands md/v1.xmetadata, or use

    --metadata=http://www.mamicode.com/0.90

Continue creatingarray? y

mdadm: Defaulting toversion 1.2 metadata

mdadm: array/dev/md0 started.

[root@localhost~]# mdadm -D /dev/md0

/dev/md0:

        Version : 1.2

  Creation Time : Wed Apr  5 21:31:19 2017

     Raid Level : raid1

     Array Size : 103296 (100.89 MiB 105.78 MB)

  Used Dev Size : 103296 (100.89 MiB 105.78 MB)

   Raid Devices : 2

  Total Devices : 3

   Persistence : Superblock is persistent

 

    Update Time : Wed Apr  5 21:31:21 2017

          State : clean

 Active Devices : 2

Working Devices : 3

 Failed Devices : 0

  Spare Devices : 1

 

           Name : localhost.localdomain:0  (local to host localhost.localdomain)

           UUID :798d316e:c1da5c3e:a9587515:8ea7373b

         Events : 17

 

    Number  Major   Minor   RaidDevice State

       0      8       33        0     active sync   /dev/sdc1

       1      8       49        1     active sync   /dev/sdd1

 

       2      8       65        -     spare   /dev/sde1

6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,

而且不更新访问时间戳,且支持acl功能;

在5基础上创建,

[root@localhost~]# mdadm -C /dev/md1 -l 5 -n 3 -c 256 -a yes /dev/sd[c-e]1

mdadm: /dev/sdc1appears to be part of a raid array:

    level=raid1 devices=2 ctime=Wed Apr  5 21:31:19 2017

mdadm: /dev/sdd1appears to be part of a raid array:

    level=raid1 devices=2 ctime=Wed Apr  5 21:31:19 2017

mdadm: /dev/sde1appears to be part of a raid array:

    level=raid1 devices=2 ctime=Wed Apr  5 21:31:19 2017

Continue creatingarray? y

mdadm: Defaulting toversion 1.2 metadata

mdadm: array/dev/md1 started.

[root@localhost~]# mdadm -D /dev/md1

/dev/md1:

        Version : 1.2

  Creation Time : Wed Apr  5 21:43:11 2017

     Raid Level : raid5

     Array Size : 206336 (201.53 MiB 211.29 MB)

  Used Dev Size : 103168 (100.77 MiB 105.64 MB)

   Raid Devices : 3

  Total Devices : 3

    Persistence : Superblock is persistent

 

    Update Time : Wed Apr  5 21:43:13 2017

          State : clean

 Active Devices : 3

Working Devices : 3

 Failed Devices : 0

  Spare Devices : 0

 

         Layout : left-symmetric

     Chunk Size : 256K

 

           Name : localhost.localdomain:1  (local to host localhost.localdomain)

           UUID :23e12709:4b969199:fd6aef00:77795114

         Events : 18

 

    Number  Major   Minor   RaidDevice State

       0      8       33        0     active sync   /dev/sdc1

       1      8       49        1     active sync   /dev/sdd1

       3      8       65        2     active sync   /dev/sde1

格式化为ext4文件系统

root@localhost~]#  mke2fs -t ext4 /dev/md1

mke2fs 1.41.12(17-May-2010)

文件系统标签=

操作系统:Linux

块大小=1024(log=0)

分块大小=1024(log=0)

Stride=256 blocks,Stripe width=512 blocks

51584 inodes, 206336blocks

10316 blocks (5.00%)reserved for the super user

第一个数据块=1

Maximum filesystemblocks=67371008

26 block groups

8192 blocks pergroup, 8192 fragments per group

1984 inodes pergroup

Superblock backupsstored on blocks:

    8193, 24577, 40961, 57345, 73729, 204801

 

正在写入inode表: 完成                           

Creating journal(4096 blocks): 完成

Writing superblocksand filesystem accounting information: 完成

 

This filesystem willbe automatically checked every 38 mounts or

180 days, whichevercomes first.  Use tune2fs -c or -i tooverride.

 

要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;

编辑  /etc/fstab 文件,添加一行信息。

/dev/md1/backup ext4 noatime,acl 0 0


本文出自 “11290766” 博客,请务必保留此出处http://rylan.blog.51cto.com/11290766/1917403

LINUX创建分区并格式化