Linux Debian 分区出错Partition 2 does not start on physical sector boundary 解决方法 + swap

也许是杞人忧天,也就是说没有必要!但如果你看懂了,那你就基本可以熟练掌握fdisk技巧了。
系统环境:Debian 9,10 在 DELL和联想 笔记本上
用Debian已经很长时间了,最近刚刚发现硬盘有分区出错,虽然在使用上没有发现什么问题,据说 Debian (可能其他发行版也一样)可以容忍这个错误,但心里还是有点不爽,在网上查了方法,但觉得写得不够清楚,心中有不少疑问,毕竟是否会把盘重写而丢失数据,但网上的分享都没有交代得很清楚,所以在此特意记录下来并分享。
整篇分享主要用到的是fdisk功能,主要的篇幅都是fdisk的问答,如果有不明白的地方,试一下fdisk就一目了然了,只要最后不用w写入硬盘就什么也没有发生,但写入之前一定要知道自己在做什么,fdisk用错了是有严重后果的,所以保险起见请在之前务必做磁盘备份

Debian 9 + DELL/联想 笔记本 + 偶然的发现

user1@dell_notebook:~$ sudo fdisk -l
[sudo] password for user1: 
Disk /dev/sda: 465.8 GiB, 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
Disklabel type: dos
Disk identifier: 0x6593c9a1

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 960239613 960237566 457.9G 83 Linux
/dev/sda2       960239614 976771071  16531458   7.9G  5 Extended
/dev/sda5       960239616 976771071  16531456   7.9G 82 Linux swap / Solaris

Partition 2 does not start on physical sector boundary.

Partition 2 does not start on physical sector boundary. 是一条报错信息

出错是因为 “/dev/sda2 960239614” 中的960239614无法被512整除,调整以后的 960237568 可以被512整除;

解决方案很简单

  1. 删除分区 d
  2. 马上添加这个分区 n
  3. 重写分区 w
  4. reboot

在使用命令前请确认分区是primary还是extended !!
在这个例子里下面的命令
user1@dell_notebook:~$ sudo fdisk /dev/sda (保险起见请在之前务必做磁盘备份),关于备份将在下次介绍,一种特别牛叉的方法。
用 d 删除分区,先删除 1 ,然后马上用 n 添加这个分区,是 primary,编号为 1,出现Do you want to remove the signature? [Y]es/[N]o:时答复 n;然后再用 d 删除分区 2,马上用 n 添加这个分区 2,其他的数字问题按回车就可以了;最后再出现 Command (m for help): 时用 w 写入磁盘,重新启动就大功告成。

调整好后的信息:

user1@dell_notebook:~$ sudo fdisk -l
[sudo] password for user1: 
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: TOSHIBA MKO1ACF0
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: dos
Disk identifier: 0xb95bacf7

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 960237567 960235520 457.9G 83 Linux
/dev/sda2       960237568 976773167  16535600   7.9G  5 Extended

还没有完全完,在纠正了上面的错误以后要看一下在启动过程中是否会出现
a start job is running for dev-disk-by UUID 1min 30s 启动错误,原因:swap或其他分区的UUID错误。这个虽然不是严重的错误,但在启动会让你都等30秒到1分30秒,不同的系统等待时间不一样。在这个例子中的原因是实际的swap id 跟/etc/fstab表中的swap id对不上;具体在这个案例中,就是把fstab表中的swap id 注释掉:
先查看swap的状况

user1@dell_notebook:~$ free -h
              total        used        free      shared  buff/cache   available
Mem:           7.7G        750M        6.3G         95M        640M        6.6G
Swap:            0B          0B          0B

swap为0,说明没有swap,所以只要在fstab表中把swap 这行注释掉:

user1@dell_notebook:~$ cat /etc/fstab
[sudo] password for user1: 

# /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/sda1 during installation
UUID=616c317d-6256-43b1-b669-4a7304f72576 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda5 during installation
# UUID=67d5b7a6-d098-47d7-a3da-0d19fecc30f0 none            swap    sw              0       0

然后重新启动系统就不会再出现1分30秒的等待时间。

另外网上有不少分享 把 /etc/fstab 中的 errors=remount-ro 0当作出错来处理,但实际这是系统的出错处理方案,具体意思是:出现errors时remount 重新挂载系统 ro 意思是只读/不能写,一般情况下只要系统正常运行根本就到不了这里。

如果需要设置swap可以参考这篇分享;方法一:这是一种用文件设置swap方法,比较安全没有什么风险,分享也写得非常清楚。

方法二:在之前的/dev/sda2 7.9G 5 Extended 建swap区
user1@dell_notebook:~$ sudo fdisk /dev/sda # 保险起见请在之前务必做磁盘备份

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.

Command (m for help): p      #显示目前的状况
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: TOSHIBA MQ01ACF0
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: dos
Disk identifier: 0xb95fedf7

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 960237567 960235520 457.9G 83 Linux
/dev/sda2       960237568 976773167  16535600   7.9G  5 Extended     #这分区可以分配给swap,但不能直接用

Command (m for help): n      #新建逻辑分区
All space for primary partitions is in use.
Adding logical partition 5
First sector (960239616-976773167, default 960239616):    #直接回车
Last sector, +/-sectors or +/-size{K,M,G,T,P} (960239616-976773167, default 976773167):     #直接回车

Created a new partition 5 of type 'Linux' and of size 7.9 GiB.
Partition #5 contains a swap signature.

Do you want to remove the signature? [Y]es/[N]o: n       #保持原来的编号

Command (m for help): p      #显示目前的状况

Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: TOSHIBA MQ01ACF0
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: dos
Disk identifier: 0xb95fedf7

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 960237567 960235520 457.9G 83 Linux
/dev/sda2       960237568 976773167  16535600   7.9G  5 Extended
/dev/sda5       960239616 976773167  16533552   7.9G 83 Linux      #还是Linux格式,要改成swap格式

Command (m for help): t
Partition number (1,2,5, default 5):
Hex code (type L to list all codes): l

0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT            
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor      
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary  
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS    
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep        
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT            
Hex code (type L to list all codes): 82     #选82 swap 格式

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p
Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
Disk model: TOSHIBA MQ01ACF0
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: dos
Disk identifier: 0xb95fedf7

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sda1            2048 960237567 960235520 457.9G 83 Linux
/dev/sda2       960237568 976773167  16535600   7.9G  5 Extended
/dev/sda5       960239616 976773167  16533552   7.9G 82 Linux swap / Solaris

Command (m for help): w      #写入磁盘
The partition table has been altered.
Syncing disks.

user1@dell_notebook:~$ sudo vim /etc/fstab     # 在reboot前修改fstab说明,把之前注释掉的# UUID=67d5b7a6-d098-47d7-a3da-0d19fecc30f0 none            swap    sw              0       0  再去掉注释号“#”
user1@dell_notebook:~$ free -m    #查看swap状况
              total        used        free      shared  buff/cache   available
Mem:           7857         509         164          46        7183        7004
Swap:             0           0           0            #swap为零
user1@dell_notebook:~$ sudo swapon /dev/sda5  #激活swap
user1@dell_notebook:~$ free -m    #查看swap状况
              total        used        free      shared  buff/cache   available
Mem:           7857         511         174          46        7172        7003
Swap:          8071           0        8071
user1@dell_notebook:~$ sudo reboot 
user1@dell_notebook:~$ lsblk     #列出块设备信息补充说明
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 465.8G  0 disk 
├─sda1   8:1    0 457.9G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   7.9G  0 part [SWAP]

方法二还是有点复杂,但要搞明白了,对分区这块的操作部分就比较得心应手了。

在此再介绍几个相关辅助的命令:

cat /etc/fstab   #查看自动挂载磁盘文件

更多关于自动挂载文件请看分享文章;

ls -l /dev/disk/by-uuid/    #查看磁盘UUID
sudo blkid -o list          #查看磁盘UUID
sudo blkid                  #查看磁盘UUID,另外的格式

更多关于uuid的分享文章;

mkswap /dev/sda5    #格式化swap分区

更多关于swap分区的分享文章;

mkfs.ext4 /dev/sda1         #格式化linux分区

更多关于linux分区格式化的分享文章;

仔细查看了所有的deb笔记本电脑和服务器,笔记本电脑上都有这个错误,不知是什么原因,也不管是如何安装的,云服务器上都没有这个问题;有高手路过请告知缘由,不胜感激!

未解决的问题: 如何在笔记本上用parted做第一块硬盘分区?发现自己的系统都是用fdisk来分区的,不管是用自动分区还是手动分区,用U盘、CD还是网络安装。

关于为什么要分区的分享。

你可能感兴趣的:(Linux,工作环境,linux,debian,运维)