命令:nvme id-ctrl /dev/nvme0n1 |grep tnvmcap
输出:tnvmcap : 30725971992576
因为nvme-cli 单次只能trim一个不超过2TB的盘,所以要将容量分段,使用容量除以当前Lba sector的容量,比如当前lba sector是512B
30725971992576 / 512 / 0xffffffff
然后将余数转换成16进制
使用命令trim
nvme dsm /dev/nvme0n1 -n 1 --blocks=0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xffffffff,0xf8f952bd --slbs=0x0,0xffffffff,0x1fffffffe,0x2fffffffd,0x3fffffffc,0x4fffffffb,0x5fffffffa,0x6fffffff9,0x7fffffff8,0x8fffffff7,0x9fffffff6,0xafffffff5,0xbfffffff4,0xcfffffff3 -d 1
–blocks= 后面跟的是一次写入量,因为硬盘是30.72TB的,所以有15个0xffffffff(2TB) + 余数0xf8f952bd(0.72TB)
–sbls= 后面跟的是从什么位置开始写,从0开始写写入blocks的2TB,第二笔从2TB位置开始向后写
python 脚本自动trim
def full_drive_trim_test(drive):
blocks, slbs = get_trim_info(drive)
trim_test_command = 'nvme dsm %s -n 1 --blocks=%s --slbs=%s -d 1' % (drive, blocks, slbs)
trim_test = os.popen(trim_test_command).read()
if 'success' in trim_test:
return True
else:
return False
def get_trim_info(drive):
read_namespace = os.popen('nvme id-ns %s -H' % drive).read()
current_flba_sector = int(re.search('flbas\s+:\s+(.*)', read_namespace).group(1), 16)
lba_sector_list = re.search('LBA Format %s : Metadata Size: (\d+).*Data Size: (\d+)\s+bytes' % current_flba_sector,
read_namespace)
lba_sector = (int(re.search('tnvmcap\s+:\s+([\d,]+)', os.popen('nvme id-ctrl %s' % drive).read()).group(1).replace(
',', '')) / (int(lba_sector_list.group(1)) + int(lba_sector_list.group(2))))
total_list = lba_sector / 0xffffffff
remainder = lba_sector % 0xffffffff
blocks_list = []
for i in range(int(total_list)):
blocks_list.append('0xffffffff')
blocks_list.append(hex(int(remainder)))
slbs_list = []
for i in range(int(total_list) + 1):
slbs_list.append(hex(i * 0xffffffff))
blocks = ','.join(blocks_list)
slbs = ','.join(slbs_list)
print('Blocks info :{}\nSlbs info :{}'.format(blocks, slbs))
return blocks, slbs
full_drive_trim_test('/dev/nvme0n1')