互联网上搜索到的Linux环境新c盘配置方式资料质量都不尽如人意,因而自己整理了一份ubuntu格式化硬盘ext4,日常Linuxc盘分区时查阅足够了,主要是用到了fdisk命令。
fdisk基本使用
新增硬碟后,在linux系统下输入fdisk-l命令查看当前c盘信息:
$ sudo fdisk -l
...
Disk /dev/ram0: 4.8 GiB, 5120000000 bytes, 10000000 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 8192 bytes
I/O size (minimum/optimal): 8192 bytes / 8192 bytes
...
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: ST500DM002-1BD14
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: C16B3C7C-2596-448D-A4BA-4A7554CD734A
Device Start End Sectors Size Type
/dev/sda1 2048 976773119 976771072 465.8G Linux filesystem
...
Disk /dev/sdc: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: ST4000NM000A-2HZ
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
...
注意看最下边多了一块3.7TB的硬碟sdc,下边用命令:fdisk/dev/sdc给新硬碟进行分区:
$ sudo fdisk /dev/sdc
Welcome to fdisk (util-linux 2.33.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
The old LVM2_member signature will be removed by a write command.
Device does not contain a recognized partition table.
The size of this disk is 3.7 TiB (4000787030016 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
Created a new DOS disklabel with disk identifier 0xe461c452.
Command (m for help):
步入fdisk命令,输入m可以看见该命令的帮助:
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
创建分区表
进行分区前须要先明晰采用分区表的格式,目前主流的有MBR和GPT,两者的区别可以自行搜索,总结两点:
晓得里面两点就够了,这儿我使用在Linux服务器上,不须要考虑windows兼容性,再者是一块4T盘,因而采用GPT进行分区,下边两种形式请依照自己的须要选择。
采用GUID(GPT)分区结构
假如使用GPT则输入g创建一张新的空GPT格式分区表。
ommand (m for help): g
Created a new GPT disklabel (GUID: D53735F6-8E14-504D-BDA5-89DB97A770DF).
The old LVM2_member signature will be removed by a write command.
然后输入n进行分区ubuntu格式化硬盘ext4,
Command (m for help): n
选择分区表编号,按照提示目前最多可以创建128个分区,保持默认即可:
Partition number (1-128, default 1):
然后选择该分区的起始c盘数,这儿可自定义也可不做选择,如无特殊需求强烈建议选择默认:
First sector (2048-7814037134, default 2048):
接出来是定义该分区的大小,默认使用整个,直接回车:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-7814037134, default 7814037134):
创建成功:
Created a new partition 1 of type 'Linux filesystem' and of size 3.7 TiB.
最后记得w将分区表写入硬碟。
采用DOS(MBR)分区结构
假如使用MBR则输入o创建一张新的空MBR格式分区表:
Command (m for help): o
The size of this disk is 3.7 TiB (4000787030016 bytes). DOS partition table format cannot be used on drives for volumes larger than 2199023255040 bytes for 512-byte sectors. Use GUID partition table format (GPT).
Created a new DOS disklabel with disk identifier 0xe3e2291c.
The old LVM2_member signature will be removed by a write command.
输入n进行分区:
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
一块化学硬碟只能有:一到四个主分区(但其中只能有一个是活动的主分区),或一到三个主分区,和一个扩充分区。分别对应hda1,hda2,hda3,hda4.
Linux中规定,每一个硬碟设备最多能有4个主分区(其中包含扩充分区)构成,任何一个扩充分区都要占用一个主分区号码,也就是在一个硬碟中,主分区和扩充分区一共最多是4个。
——《Linux主分区linux计划任务linux 常用命令,扩充分区,逻辑分区的联系和区别》
总接出来就是一块化学硬碟起码有一个主分区,在这儿我只须要一个分区,因而下边将这块硬碟全部划为主分区:
Select (default p): p
然后选择该主分区为第几个主分区,因为是楼盘,这儿输入1来分第一个主分区:
Partition number (1-4, default 1): 1
然后选择该分区的起始c盘数,这儿可自定义也可不做选择,如无特殊需求强烈建议选择默认:
First sector (2048-4294967295, default 2048):
接出来是定义该分区的大小,假若按默认(按回车)即是使用全部可用储存额,也可以是用M或m单位结尾的数字(小写M是大B的意思,假如输入1M实际上是X8也就是8m的空间),这儿我保持默认:
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-4294967294, default 4294967294):
Created a new partition 1 of type 'Linux' and of size 2 TiB.
可以看见虽然硬碟是4TB,采用MBR后只能使用其中的2TB空间,其他的就浪费了。
最后记得w将分区表写入硬碟。
后续操作(重要)
完成分区后,记得输入w写入分区:
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
写入分区表成功后会退出fdisk交互界面。
再看一眼c盘信息:
$ sudo fdisk -l
...
Disk /dev/sdc: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: ST4000NM000A-2HZ
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 74A6DB69-8C91-654F-9F61-A9F38D7F2A34
Device Start End Sectors Size Type
/dev/sdc1 2048 7814037134 7814035087 3.7T Linux filesystem
...
没问题后进行低格,假如没有特殊需求就采用ext4:
第四代扩充文件系统(德语:Fourthextendedfilesystem,简写为ext4)是Linux系统下的日志文件系统,是ext3文件系统的后继版本——ext4ByWikipedia。
$ sudo mkfs.ext4 /dev/sdc1
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 976754384 4k blocks and 244195328 inodes
Filesystem UUID: 87746749-1922-4682-9b4e-3a67be1d9498
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544
Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done
c盘挂载
然后将c盘挂载到须要的位置,通常会选择挂载在/mnt,我在这儿因为涉及到多用户,将硬碟挂载我用户目录下的一个文件夹下:
sudo mount /dev/sdc1 /home/songtianlun/data
直接挂载,重启后配置会遗失,可以更改/etc/fstab配置文件配置开机手动挂载,该配置文件的格式如下:
$ cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/sda7 during installation
UUID=4ae9f128-7cee-48fd-998c-4257e394fa4a / ext4 errors=remount-ro 0 1
# /boot/efi was on /dev/sda1 during installation
UUID=A5EB-573A /boot/efi vfat umask=0077 0 1
/swapfile none swap sw 0 0
# /dev/sda1
/dev/sda1 / ext4 rw,relatime 0 1
UUID=4ae9f128-7cee-48fd-998c-4257e394fa4a / ext4 errors=remount-ro 0 1
格式大约是这样的:
#
# 要挂载的分区设备号 挂载点 文件系统类型 挂载选项 是否备份 是否检测
假如须要设备号,可以使用blkid命令获取:
$ blkid
/dev/sda1: UUID="e943af1e-72cf-4b72-b444-859b9610256f" TYPE="ext4" PARTUUID="161a8c3f-871d-45f6-a25f-466bf16f6035"
实测下边这两种都是可以挂载的:
UUID=4ae9f128-7cee-48fd-998c-4257e394fa4a / ext4 errors=remount-ro 0 1
/dev/sda1 / ext4 rw,relatime 0 1
写好配置后可以使用该命令生效(挂载/etc/fstab中所有档案):
mount -a
上次重启设备就不须要再自动挂载该c盘了。
c盘状态
使用lsblk看一下当前硬碟的树状结构:
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
└─sda1 8:1 0 465.8G 0 part /
sdc 8:32 0 3.7T 0 disk
└─sdc1 8:33 0 3.7T 0 part /home/songtianlun/data
使用df-h瞧瞧使用率:
$ df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/sda1 459G 8.6G 427G 2% /
...
/dev/sdc1 3.6T 89M 3.4T 1% /home/songtianlun/data
参考文献