Linux磁盘分区及自动挂载教程
文章目录
一、磁盘分区
1、查看设备
(设备就是磁盘或者USB等)
[root@jydr ~]# lsblk -ip
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
/dev/sda 8:0 0 45G 0 disk
|-/dev/sda1 8:1 0 1M 0 part
|-/dev/sda2 8:2 0 1G 0 part /boot
|-/dev/sda3 8:3 0 39G 0 part
| |-/dev/mapper/centos-root 253:0 0 37G 0 lvm /
| `-/dev/mapper/centos-swap 253:1 0 2G 0 lvm [SWAP]
`-/dev/sda4 8:4 0 1G 0 part /root/mymount
/dev/sr0 11:0 1 1024M 0 rom
/dev/loop0 7:0 0 1G 0 loop /mnt
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
可以看到主要设备是/dev/sda
2、磁盘分区类型
[root@jydr ~]# parted /dev/sda print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 48.3GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: pmbr_boot
Number Start End Size File system Name Flags
1 1049kB 2097kB 1049kB bios_grub
2 2097kB 1076MB 1074MB xfs
3 1076MB 42.9GB 41.9GB lvm
4 42.9GB 44.0GB 1074MB xfs Linux filesystem
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
Partition Table: gpt # 看这里,还有可能是mbr 或者 loop或者不知道类型
gpt时使用gdisk分区 mbr使用fdisk分区,其他类型基本就不是默认的磁盘,直接是一个分区或者外接磁盘
3、给磁盘分区
[root@jydr ~]# gdisk /dev/sda
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): ? # 输入?可以查看相关指令
b back up GPT data to a file
c change a partition's name
d delete a partition # 删除分区
i show detailed information on a partition
l list known partition types
n add a new partition # 添加分区
o create a new empty GUID partition table (GPT)
p print the partition table # 打印已有的分区表信息
q quit without saving changes # 不保存退出
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit # 保存退出
x extra functionality (experts only)
? print this menu
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
查看分区信息 按p
Command (? for help): p
Disk /dev/sda: 94371840 sectors, 45.0 GiB # 注意删除数目 94371840
Logical sector size: 512 bytes
Disk identifier (GUID): 44DCEBA0-6073-4F0C-A880-390C5AF8AE86
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 94371806
Partitions will be aligned on 2048-sector boundaries
Total free space is 8392637 sectors (4.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 4095 1024.0 KiB EF02
2 4096 2101247 1024.0 MiB 0700
3 2101248 83884031 39.0 GiB 8E00
4 83884032 85981183 1024.0 MiB 8300 Linux filesystem
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
注意在上表的Code一栏
- 8300代表Linux文件系统
- 8200代表swpa
- 0700代表Windows文件系统
分区操作
Command (? for help): n
Partition number (5-128, default 5):
First sector (34-94371806, default = 85981184) or {+-}size{KMGTP}:
Last sector (85981184-94371806, default = 94371806) or {+-}size{KMGTP}: +1g # 必设定大小,根据自己需要设定
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): w # 保存
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y # 确定
OK; writing new GUID partition table (GPT) to /dev/sda.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot.
The operation has completed successfully.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
分区后重启一下,lsblk就可以看见新分区了
也可以输入更新指令查看
cat /proc/partitions
partprobe -s
cat /proc/partitions
- 1
- 2
- 3
4、格式化新的分区
查看新建的分区,以及其他分区是什么格式
[root@jydr ~]# blkid
/dev/mapper/centos-root: UUID="f33356bb-33f3-4ff1-923e-253a275b81a0" TYPE="xfs"
/dev/sda3: UUID="83s7NF-qZRT-b2xi-0Bh6-0RSm-3jr1-QpAa7Z" TYPE="LVM2_member" PARTUUID="b5c31c9d-6eb8-4d2f-bbb1-9912031b156f"
/dev/sda2: UUID="663c4d89-a1fd-4921-ba90-8f05993be32c" TYPE="xfs" PARTUUID="a570f71a-1aa1-469c-8ecb-f0370ea96ccb"
/dev/sda4: UUID="ebe2810a-06e4-4df1-94b3-e23f8adf6635" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="87f2bd84-e9ee-4a32-8259-5ab4dff0037d"
/dev/mapper/centos-swap: UUID="1a781a42-1cf3-4a43-82d9-026f9e3701d9" TYPE="swap"
/srv/loopdev: UUID="727b8115-3045-4e36-be77-f870bc6fd248" TYPE="xfs"
/dev/loop0: UUID="727b8115-3045-4e36-be77-f870bc6fd248" TYPE="xfs"
/dev/sda1: PARTUUID="71c738bb-ae3b-4b69-a7fc-02fce086db00"
/dev/sda5: PARTLABEL="Linux filesystem" PARTUUID="3a2b86c1-722e-4c4b-beab-5f81f2990258"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
现在2020年常用的linux格式有ext4和xfs,格式化指令是mkfs.ext4(ubuntu)和mkfs.xfs (CentOS)
[root@jydr ~]# mkfs.xfs /dev/sda5
meta-data=/dev/sda5 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
完成后这个磁盘就可以挂载使用了
5、给已经分区的磁盘区域分区
# 建立一个大小为1*512m 的空文件
dd if=/dev/zero of=/srv/newdev bs=1M count=512
# 格式化该文件
mkfs.xfs -f /srv/newdev
# 查看信息
blkid /srv/newdev
# 找到UUID挂载
mount -o loop UUID="6fb87c04-e80a-4979-b359-98490097f83b" /mnt
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
二、自动挂载
vim /etc/fstab
# 其中添加一行,home目录以后就用挂载的分区了
UUID=ebe2810a-06e4-4df1-94b3-e23f8adf6635 /home xfs defaults 0 0
# 挂载之后要测试一下,不能直接重启系统
mount -a
如果成功就成功,不成功就检测错误。