linux磁盘管理

xiaoxiao2021-02-28  28

设备查看命令

  fdisk -l              系统存在的设备   cat /proc/partitions  系统识别的设备   blkid                 系统中可以用的设备   df                    系统中正在挂载的设备   lsblk 命令用于列出所有可用块设备的信息,而且还能显示他们之间的依赖关系  

磁盘分区

   硬盘的分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个。且基本分区可以马上被使用但不能再分区。扩充分区必须再进行分区后才能使用,也就是说它必须还要进行二次分区。那么由扩充分区再分下去的是什么呢?它就是逻辑分区(logical partion),况且逻辑分区没有数量上限制。

用fdisk命令磁盘分区 

[root@server11 Desktop]# fdisk /dev/vdb Welcome to fdisk (util-linux 2.23.2). Changes 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 Building a new DOS disklabel with disk identifier 0x079cb101. Command (m for help): m ##帮助 Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition ##删除分区 g create a new empty GPT partition table G create an IRIX (SGI) partition table 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 ##退出 s create a new empty Sun disklabel t change a partition's system id ##修改分区功能id u change display/entry units v verify the partition table w write table to disk and exit ##保存更改到分区表中 x extra functionality (experts only) Command (m for help): n ##新建分区 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p ##主分区 Partition number (1-4, default 1): 1 ##分区号1-4 First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M ##分100m大小 Partition 1 of type Linux and of size 100 MiB is set .......... Command (m for help): p 查看分区信息 Disk /dev/vdb: 10.7 GB, 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 label type: dos Disk identifier: 0x079cb101 Device Boot Start End Blocks Id System /dev/vdb1 2048 206847 102400 83 Linux /dev/vdb2 206848 411647 102400 83 Linux /dev/vdb3 411648 616447 102400 83 Linux /dev/vdb4 616448 20971519 10177536 5 Extended ##扩展分区 /dev/vdb5 618496 823295 102400 83 Linux ##逻辑分区 /dev/vdb6 825344 20971519 10073088 83 Linux       ##逻辑分区 Command (m for help): wq        ##保存退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@server11 Desktop]# fdisk -l    ##显示设备信息 Disk /dev/vda: 10.7 GB, 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 label type: dos Disk identifier: 0x00013f3e Device Boot Start End Blocks Id System /dev/vda1 * 2048 20970332 10484142+ 83 Linux Disk /dev/vdb: 10.7 GB, 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 label type: dos Disk identifier: 0x079cb101 Device Boot Start End Blocks Id System /dev/vdb1 2048 206847 102400 83 Linux /dev/vdb2 206848 411647 102400 83 Linux /dev/vdb3 411648 616447 102400 83 Linux /dev/vdb4 616448 20971519 10177536 5 Extended /dev/vdb5 618496 823295 102400 83 Linux /dev/vdb6 825344 20971519 10073088 83 Linux [root@server11 Desktop]#

磁盘分区后需要格式化后才能挂载

格式化命令:mkfs.xfs /dev/vdb1 -f

修改分区方式为gpt

[root@server11 /]# parted /dev/vdb GNU Parted 3.1 Using /dev/vdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel ##设置磁盘文件系统 New disk label type? gpt ##文件系统格式 Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to continue? Yes/No? yes (parted) quit ##推出 Information: You may need to update /etc/fstab. [root@server11 /]# fdisk -l    ##查看磁盘信息 Disk /dev/vda: 10.7 GB, 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 label type: dos Disk identifier: 0x00013f3e Device Boot Start End Blocks Id System /dev/vda1 * 2048 20970332 10484142+ 83 Linux WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion. Disk /dev/vdb: 10.7 GB, 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 label type: gpt    ##磁盘格式 # Start End Size Type Name

设置/dev/vdb1开机时自动挂载在/mnt

   vim /etc/fstab    ##自动挂载文件

     

最后加上      /dev/vdb   /mnt  xfs defaults 0 0 

   mount -a  重新读取该文件,使其挂载

修改/dev/sdb2为swap分区

第一步:fdisk 中修改swap分区标识

[root@localhost Desktop]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): t    ##修改分区功能id Partition number (1,2, default 2): 2     Hex code (type L to list all codes): 82    ##swap code Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p     ##查看分区 Disk /dev/sdb: 15.5 GB, 15479597056 bytes, 30233588 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 label type: dos Disk identifier: 0xbe1a927f Device Boot Start End Blocks Id System /dev/sdb1 2048 2050047 1024000 0 Empty /dev/sdb2 2050048 4098047 1024000 82 Linux swap / Solaris Command (m for help): wq    ##保存

第三步:

mkswap /dev/sdb2        ##创建一个swap区域

第四步:

swapon -a /dev/sdb2    ##挂载swap分区

第五步:开机自动挂载

vim /etc/fstab    ##编辑文件

/dev/vdbn swap swap defaults,pri=1 0 0

last:取消swap分区

swapoff /dev/vdbn

 

 

分区LUKS锁

cryptsetup luksFormat /dev/vdb1    ##分区加锁,加锁后分区不能挂载,需开锁后才可以 cryptsetup open /dev/vdb1 westos    ##开锁    开锁后需格式化才能挂载 mkfs.xfs  /dev/mapper/westos        ##格式化 mount /dev/mapper/westos  /mnt/    ##挂载 umount /mnt/            ##取消挂载

cryptsetup close westos        ##关锁

若想取消锁,需格式化分区。

开机自动开锁

第一步:保存密码文件中。

/root/passwd     ##密码文件

cryptsetup luksAddKey /dev/vdb1  /root/passwd    ##加密磁盘建立key

vim /etc/crypttab    ##配置文件

解密后设备管理文件 设备 加密字符存放文件 westos /dev/vdb1 /root/passwd

vim /etc/fstab     ##配置文件

/dev/mapper/westos /mnt xfs defaults 0 0

这样就好了,root后自动解密到/dev/mapper/westos ,自动挂载在/mnt上

 

 

 

磁盘阵列(实验)

先准备3块分区磁盘,修改分区为fd(分区功能id)

Command (m for help): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/vdb: 10.7 GB, 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 label type: dos Disk identifier: 0xd4f8b483 Device Boot Start End Blocks Id System /dev/vdb1 2048 2099199 1048576 fd Linux raid autodetect /dev/vdb2 2099200 4196351 1048576 fd Linux raid autodetect /dev/vdb3 4196352 6293503 1048576 fd Linux raid autodetect

 

[root@localhost Desktop]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}     ##创建磁盘阵列 mdadm: Note: this array has metadata at the start and may not be suitable as a boot device. If you plan to store '/boot' on this device please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@localhost Desktop]# mkfs.xfs /dev/md0 [root@localhost Desktop]# mount /dev/md0 /mnt/ [root@localhost Desktop]# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3809516 6664384 37% / devtmpfs 469344 0 469344 0% /dev tmpfs 484932 140 484792 1% /dev/shm tmpfs 484932 12756 472176 3% /run tmpfs 484932 0 484932 0% /sys/fs/cgroup /dev/md0 1044588 32928 1011660 4% /mnt [root@localhost Desktop]# lsblk ##磁盘依赖关系 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT vda 253:0 0 10G 0 disk └─vda1 253:1 0 10G 0 part / vdb 253:16 0 10G 0 disk ├─vdb1 253:17 0 1G 0 part │ └─md0 9:0 0 1023.4M 0 raid1 /mnt ├─vdb2 253:18 0 1G 0 part │ └─md0 9:0 0 1023.4M 0 raid1 /mnt └─vdb3 253:19 0 1G 0 part └─md0 9:0 0 1023.4M 0 raid1 /mnt [root@localhost Desktop]#

mdadm -f /dev/md0  /dev/vdb1    ##强制损坏磁盘阵列分区,

mdadm -D /dev/md0                    ##查看阵列信息

mdadm -r /dev/md0  /dev/vdb1         ##删除磁盘阵列的分区

mdadm -a /dev/md0 /dev/vdb1        ##添加磁盘阵列的分区

mdadm  -S /dev/md0        ##删除磁盘阵列

[root@localhost Desktop]# mdadm /dev/md0 -f /dev/vdb2 mdadm: set /dev/vdb2 faulty in /dev/md0 [root@localhost Desktop]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Mon Apr 30 04:43:16 2018 Raid Level : raid1 Array Size : 1048000 (1023.61 MiB 1073.15 MB) Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Apr 30 05:04:28 2018 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 1 Spare Devices : 0 Name : localhost:0 (local to host localhost) UUID : 023371cb:4bb91656:964a77fc:84256a07 Events : 36 Number Major Minor RaidDevice State 0 253 17 0 active sync /dev/vdb1 2 253 19 1 active sync /dev/vdb3 1 253 18 - faulty /dev/vdb2 [root@localhost Desktop]# mdadm -r /dev/md0 /dev/vdb2 mdadm: hot removed /dev/vdb2 from /dev/md0 [root@localhost Desktop]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Mon Apr 30 04:43:16 2018 Raid Level : raid1 Array Size : 1048000 (1023.61 MiB 1073.15 MB) Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB) Raid Devices : 2 Total Devices : 2 Persistence : Superblock is persistent Update Time : Mon Apr 30 05:07:53 2018 State : clean Active Devices : 2 Working Devices : 2 Failed Devices : 0 Spare Devices : 0 Name : localhost:0 (local to host localhost) UUID : 023371cb:4bb91656:964a77fc:84256a07 Events : 37 Number Major Minor RaidDevice State 0 253 17 0 active sync /dev/vdb1 2 253 19 1 active sync /dev/vdb3 [root@localhost Desktop]# mdadm /dev/md0 -a /dev/vdb2 mdadm: added /dev/vdb2 [root@localhost Desktop]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Mon Apr 30 04:43:16 2018 Raid Level : raid1 Array Size : 1048000 (1023.61 MiB 1073.15 MB) Used Dev Size : 1048000 (1023.61 MiB 1073.15 MB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Mon Apr 30 05:08:57 2018 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Name : localhost:0 (local to host localhost) UUID : 023371cb:4bb91656:964a77fc:84256a07 Events : 38 Number Major Minor RaidDevice State 0 253 17 0 active sync /dev/vdb1 2 253 19 1 active sync /dev/vdb3 3 253 18 - spare /dev/vdb2 [root@localhost Desktop]# umount /dev/md0

 

转载请注明原文地址: https://www.6miu.com/read-2625502.html

最新回复(0)