Linux基础命令学习

来源:互联网 发布:好看的男装淘宝店 编辑:程序博客网 时间:2024/05/17 23:40

Unit13

磁盘管理(上)

1、磁盘的扇区
磁盘的第一个扇区主要记录了两个重要的信息:
主引导分区(MBR):可以安装引导加载程序的地方,有446bytes
分区表:记录整块硬盘分区的状态,有64bytes
硬盘有效性标示
主分区
扩展分区
逻辑分区
MBR是很重要的,因为当系统在开机的时候会主动去读取这个区块的内容

fdisk -l   ##系统中的所有磁盘设备df  -TH    ##系统正在挂载的磁盘设备blkid      ##系统可以挂载的磁盘设备id

2、分区划分
fidsk /dev/vdb

[root@localhost ~]# fdisk /dev/vdbWelcome 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): 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 (1 primary, 0 extended, 3 free)  ##分区类型为主分区   e   extended                 ##分区类型为扩展分区Select (default p): pPartition number (2-4, default 2): 2        ##主分区idFirst sector (206848-20971519, default 206848): ##此分区的起始位置Using default value 206848Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +200M ##分区大小Partition 2 of type Linux and of size 200 MiB is setCommand (m for help): wq   ##**保存并退出,只输入q则表示放弃更改退出**The partition table has been altered!
partprobe       ##同步分区表mkfs.xfs /dev/vdb1  ##格式化mount /dev/vdb1  /mnt   ##临时挂载vim /etc/fstab      ##永久挂载编写格式:device    mountpoint    ftype   defaults(mountpoint)   0   0/dev/vdb1    /mnt        xfs    defaults       0     0mount -a        ##使/etc/fastab中记录的挂载生效

分区删除
先使用umount命令解除挂载
PS:当挂载点被使用时,无法解除挂载,需要用fuser -kvm结束使用的进程,然后进行解除挂载
解除挂载之后,使用fdisk /dev/vdb命令 删除分区

5.设定分区方式,将mkdocs 改为 gpt
更改之前需要把正在使用的分区关闭,才可以进行更改
partoff /dev/vdb2
parted /dev/vdb

添加swap分区
使用磁盘分区

[root@localhost ~]# fdisk /dev/vdbWARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.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): nPartition number (2-128, default 2): First sector (34-20971486, default 2099200): Last sector, +sectors or +size{K,M,G,T,P} (2099200-20971486, default 20971486): +4GCreated partition 2Command (m for help): pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: gpttart          End    Size  Type            Name 1         2048      2099199      1G  Linux filesyste  2      2099200     10487807      4G  Linux filesyste Command (m for help): tPartition number (1,2, default 2): 2Partition type (type L to list all types): l **14 Linux swap                     0657FD6D-A4AB-43C4-84E5-0933C84B4F4F**Partition type (type L to list all types): 14Changed type of partition 'Linux filesystem' to 'Linux swap'Command (m for help): pDisk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: gptStart          End    Size  Type            Name 1         2048      2099199      1G  Linux filesyste  2      2099200     10487807      4G  Linux swap      Command (m for help): wqThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: Device or resource busy.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.[root@localhost ~]# [root@localhost ~]# partprobe      ##同步分区表[root@localhost ~]# mkswap /dev/vdb2   ##格式化成swap类型Setting up swapspace version 1, size = 4194300 KiBno label, UUID=b933737a-1409-48b5-8f92-d663d388134b[root@localhost ~]# swapon -a /dev/vdb2 ##加入swap[root@localhost ~]# swapon -sFilename                Type        Size    Used    Priority/dev/vdb2                               partition   4194300 0   -1[root@localhost ~]# 

使用文件添加swap

dd if=/dev/zero of=/swapfile bs=1M count=1000 ##创建一个1G大小的文件mkswap /swapfile    ##格式化为swap类型swapon -a /swapfile ##临时添加到swap里-p + 数字     ##更改优先级 vim /etc/fstab      ##永久添加swap分区

类型:
/swapfile swap swap defaults,pri=1 0 0 #pri优先级

删除swap

vim /etc/fstab  ##删除此文件中添加的swap行swapoff /swapfile ##断开swap文件链接swapoff /dev/vdb1 ##断开swap磁盘链接rm -rf /swapfile  ##删除文件fdisk /dev/vdb    ##删除磁盘分区partprobe     ##同步分区表
原创粉丝点击