Linux:U盘卸载挂载、系统分区管理、磁盘加密和磁盘阵列

分区方便于数据恢复,fdisk -l查看硬盘,表示真实存在,检测硬件信息
vda第一个虚拟磁盘
vdb第二个虚拟磁盘
–IDE表示老式硬盘(并口硬盘)
/dev/sda表示第一块串口硬盘,a表示第一块
硬盘名字sata
/dev/hd0串口硬盘
/dev/cdrom /dev/sr0表示光驱,cdrom为sro的快捷方式,cdrom可以不存在
/dev/mapper
cat /proc/partitions 查看系统识别的设备
blkid查看系统里面可以使用的设备
df查看系统正在使用的设备
df <= blkid < cat /proc/partitions < fdisk -l
df -h 代表2的n次方,数小准确,1024
df -H 代表10的n次方 1000

一·卸载挂载U盘

打开两个shell:
在第一个shell超级用户:

[root@foundation71 ~]# df          ##查看系统正在使用的设备
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda10     104687676 37537832  67149844  36% /
devtmpfs         1610392        0   1610392   0% /dev
tmpfs            1622996       92   1622904   1% /dev/shm
tmpfs            1622996     8996   1614000   1% /run
tmpfs            1622996        0   1622996   0% /sys/fs/cgroup
/dev/sda8         505580   147456    358124  30% /boot
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             324600        8    324592   1% /run/user/1000
/dev/sdb1       31477552  8148180  23329372  26% /run/media/kiosk/00A4-2CBF     ##识别出u盘
[root@foundation71 ~]# umount /dev/sdb1    ##卸载
[root@foundation71 ~]# mount /dev/sdb1
mount: can't find /dev/sdb1 in /etc/fstab
[root@foundation71 ~]# mount /dev/sdb1  /mnt/  ##挂载到/mnt/下
[root@foundation71 ~]# cd  /mnt/   
[root@foundation71 mnt]# fdisk -l    ##查看真实存在的硬盘

Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0xcf3c9040

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   104861695    52429824    7  HPFS/NTFS/exFAT
/dev/sda2       104861696   976773119   435955712    f  W95 Ext'd (LBA)
/dev/sda5       104863744   322969599   109052928    7  HPFS/NTFS/exFAT
/dev/sda6       322971648   541077503   109052928    7  HPFS/NTFS/exFAT
/dev/sda7       541079552   759185407   109052928    7  HPFS/NTFS/exFAT
/dev/sda8       759187456   760211455      512000   83  Linux
/dev/sda9       760213504   767293439     3539968   82  Linux swap / Solaris
/dev/sda10      767295488   976773119   104738816   83  Linux

Disk /dev/sdb: 32.3 GB, 32296140800 bytes, 63078400 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: 0x00a42cbf

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *          64    63078399    31539168    b  W95 FAT32

这里写图片描述
在第二个shell超级用户:

[root@foundation71 ~]# umount  /mnt/    ##直接卸载是不能的
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))  ##两种查看方式
[root@foundation71 ~]# fuser -vm /dev/sdb1   ##查看后台的PID
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root     kernel mount /mnt
                     root       3479 ..c.. bash
[root@foundation71 ~]# fuser -kvm /dev/sdb1   ##杀死后台
                     USER        PID ACCESS COMMAND
/dev/sdb1:           root     kernel mount /mnt
                     root       3479 ..c.. bash
[root@foundation71 ~]# umount  /mnt/    ##卸载成功(卸载成功后在第一个shell中出 会自动出现一个Killd)
[root@foundation71 ~]# 

第二个shell

第一个shell
这里写图片描述

mount -o添加参数进行挂载

[root@foundation71 ~]# mount  -o  ro  /dev/sdb1  /mnt/
ro:不能建立文件只读
[root@foundation71 ~]# df
Filesystem     1K-blocks     Used Available Use% Mounted on
/dev/sda10     104687676 37523984  67163692  36% /
devtmpfs         1610392        0   1610392   0% /dev
tmpfs            1622996      152   1622844   1% /dev/shm
tmpfs            1622996     8988   1614008   1% /run
tmpfs            1622996        0   1622996   0% /sys/fs/cgroup
/dev/sda8         505580   147456    358124  30% /boot
/dev/loop0       3654720  3654720         0 100% /var/www/html/source7.0
/dev/loop1       3947824  3947824         0 100% /var/www/html/source7.2
tmpfs             324600       12    324588   1% /run/user/1000
/dev/sdb1       31477552  8148180  23329372  26% /mnt
[root@foundation71 ~]# touch  /mnt/linux1
touch: cannot touch ‘/mnt/linux1’: Read-only file system
[root@foundation71 ~]# umount /dev/sdb1
[root@foundation71 ~]# mount  -o  rw  /dev/sdb1  /mnt/
rw:可以建立文件并且可写
[root@foundation71 ~]# touch  /mnt/linux1

这里写图片描述

二·系统分区管理

以下实验在server虚拟机中:
fdisk -l ##查看
实验步骤:

 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 0x9ccd2552.

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
  p   primary    ##主分区
  e   extended   ##扩建分区

Select (default p): p
Partition number (1-4, default 1): 1    ##确定主分区id
First sector (2048-20971519, default 2048):  ##分区起始块的位置,用默认回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M   ##分区结束块位置,用+大小的方式指定 
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (206848-20971519, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +100M
Partition 2 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (411648-20971519, default 411648): 
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +100M
Partition 3 of type Linux and of size 100 MiB is set


Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e        ##第四个需要扩展分区
Selected partition 4
First sector (616448-20971519, default 616448): 
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): 
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496): 
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +100M
Partition 5 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: 0x9ccd2552

   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
Command (m for help): wq    ##保存分区策略并退出fdisk界面
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
partprobe                     ##手动同步分区
cat  /proc/partitions         ##查看设备是否被系统识别
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 253       20          1 vdb4
 253       21     102400 vdb5
 253       22   10073088 vdb6

这里写图片描述

mount /dev/vdb5 /mnt/       ##挂载
mount: /dev/vdb5 is write-protected, mounting read-only    ##不能直接挂载
mount: unknown filesystem type '(null)'

mkfs.xfs  /dev/vdb1           ##格式化设备,在设备上安装文件系统xfs
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

blkid                          ##查看可用设备,可以看到被格式化好的/dev/vdb1
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb5: UUID="c26f6c71-59b7-42e3-aab9-295d369ceb30" TYPE="xfs" 
/dev/vdb1: UUID="b6e4f9cf-1e3e-437f-ae4e-fefb9fad45a5" TYPE="xfs" 

mount /dev/vdb5 /mnt/          ##挂载成功(临时挂载)
df                             ##查看挂载成功

这里写图片描述

做完之后要把挂载的内容要卸载才能进行以下步骤

GPT分区方式:

 parted /dev/vdb                                       
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel 
        --------##输入mklabel
New disk label type?           ##按两次tab键                                                     
aix    amiga  bsd    dvh    gpt    loop   mac    msdos  pc98   sun    
New disk label type? gpt
                     ---##改为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  
       ----- ##输入yes                                                          
(parted) quit                      
        ------- 输入quit (退出)                                                      
Information: You may need to update /etc/fstab.

这里写图片描述

fdisk /dev/vdb   ##再次重新创建分区
partprobe        ##手动同步分区
cat /proc/partitions   ##查看
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 fdisk -l      ##查看
Disk label type: gpt  ##已改为gpt分区


#         Start          End    Size  Type            Name
 1         2048       206847    100M  Linux filesyste 
 2       206848       411647    100M  Linux filesyste 
 3       411648       616447    100M  Linux filesyste

这里写图片描述

改完gpt后继续要改回来
这里写图片描述

三·永久挂载

重新创建一个分区
vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1

/dev/vdb1 /mnt    xfs   defaults   0 0
----------------------------------------    ##编写内容

partprobe ##手动同步
mount -a ##自动全部挂载
这里写图片描述

SWAP的分区方式:

创建一个分区
t:修改分区id(82)
这里写图片描述
vim /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vdb1 /mnt    xfs   defaults   0 0
/dev/vdb2 swap    swap  defaults   0 0
----------------------------------------  ##编写内容
mkswap  /dev/vdb2
swapon -a     ##测试报错
swapon -s     ##查看

这里写图片描述

vim /etc/fstab
删除更改分区编写的内容
swapoff /dev/vdb2
fdisk /dev/vdb
删除创建的分区

四·磁盘加密

fdisk  /dev/vdb                     ##创建新的分区
partprobe
cryptsetup luksFormat /dev/vdb1     ##加密(输入YES是大写)
mount /dev/vdb1  /mnt               ##挂载
ls  /dev/mapper/
cryptsetup open /dev/vdb1  westos   ##打开加密的分区 
ls /dev/mapper
mkfs.xfs  /dev/mapper/westos        ##格式化分区为xfs格式
mount /dev/mapper/westos  /mnt/     ##挂载
umount /mnt/                        ##卸载
df                                  ##查看
cryptsetup close westos             ##关闭加密分区
cryptsetup open /dev/vdb1  westos   ##打开加密分区
mount /dev/vdb1   /mnt              ##用原始挂载形式不能进行
mount /dev/mapper/westos /mnt/      ##用加密挂载形式可以进行
[root@localhost ~]# partprobe     ##报错:没有卸载
Error: Partition(s) 1 on /dev/vdb have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before making further changes.

这里写图片描述
这里写图片描述

五·磁盘加密的开机自动挂载

1.加密开机自动挂载

vim /root/diskpass            ##输入加密密码(必须大于8位,不能过于简单)
chmod  600  /root/diskpass    ##给密码文件权限600
cryptsetup luksAddKey /dev/vdb1 /root/diskpass      ##关联设备和密码文件
vim  /etc/crypttab            ##编辑文件使其自动读入密码
编写内容:westos         /dev/vdb1   /root/lukspsfile
vim  /etc/fstab               ##自动挂载
编写内容:/dev/mapper/westos /mnt    xfs defaults    0 0
df                            ##查看(如果有挂载需要卸载)
reboot                        
df                            ##查看(会有挂载)

这里写图片描述
开机挂载成功后,查看会有自动挂载
这里写图片描述
2.清除加密开机自动挂载
vim /etc/crypttab
vim /etc/fstab
删除两个编写内容
rm -fr /root/diskpass
umount /dev/mapper/westos
cryptsetup close westos
mkfs.xfs /dev/vdb1 -f
这里写图片描述

六·磁盘阵列

1.制作双磁盘闲置

fdisk /dev/vdb     ##创建三个分区,分区标示为raid
watch -n 1 cat /proc/mdstat        ##监控
mdadm  -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{1..3}     ##制作双磁盘一个闲置
-C  ##表示创建
-a  ##表示设备没有自动创建
-l 1   ##表示级别
-n 2   ##表示有两块设备
-x 1   ##表示读的快
-D   ##查看状态
-f   ##损坏
-r   ##卸载掉
mkfs.xfs  /dev/md0                      ##格式化
mount /dev/md0  /mnt/                   ##挂载
watch -n 1 cat /proc/mdstat             ##监控

这里写图片描述
2.设备管理

mdadm -D /dev/md0               ##查看设备状态
mdadm /dev/md0 -f /dev/vdb2     ##失效指定硬盘
mdadm /dev/md0 -r /dev/vdb2     ##删除指定硬盘
mdadm /dev/md0 -a /dev/vdb2     ##添加指定硬盘

查看设备状态:
这里写图片描述
失效指定硬盘:
这里写图片描述
删除指定硬盘:
这里写图片描述
添加指定硬盘:
这里写图片描述
3.设备删除

umount  /mnt/
mdadm -S /dev/md0               ##停止使用
fdisk /dev/vdb                  ##删除创建所有分区

这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/le_anny/article/details/80170490