抛个砖头--批处理自动设置--能有玉否?

交流 PrimoCache软件使用过程中遇到的问题以及心得等
这里提供官方的技术支持
回复
msold5
1级用户
1级用户
帖子: 4
注册时间: 周二 1月 05, 2016 6:47 pm

抛个砖头--批处理自动设置--能有玉否?

帖子 msold5 »

我和我推荐给周围朋友的配置,都是固态用于启动+二缓,通常128-512G,机械用于存储数据,通常0.5-18T。
大机械硬盘性价比高,很多家庭都有小孩从小到大的照片视频,收藏的歌曲视频电影、必备的软件什么的
机械盘万一有误操作,数据可恢复,固态就呵呵了
配置不是重点,大家也不必拿这些考虑做讨论,主要想讨论的,是怎么配置PrimoCache更合理,以及简化配置
的方法。诚心地说,用各种语言和脚本,就没有比批处理更差的了,效率低不说,还容易出错。但是,批
处理的即改即用和不要求环境仍然使其生存了下来。本次探讨是以批处理为基础的命令行PrimoCache配置。
由于自身经常下载各大论坛新出的操作系统安装到自用实体机测试,重装系统太频繁,只好大量使用绿色软
件,连最常用的静默安装软件们也要弄个批处理方式来安装。PrimoCache是我的必装软件之一,每次手动
设置时都在想要花点时间弄个自动设置方法出来。
最先想到的就是按照自己的机器配置写个简单批处理:

代码: 全选

    cd /d "C:\Program Files\PrimoCache"
    set disk=没找到盘
    for /f "tokens=2" %%i in ('rxpcc ls^|find "Vol"^|find "Level-2"') do set disk=%%i
    for /f "tokens=4" %%i in ('rxpcc ls^|find "%disk%"') do set disk=%%i
    echo 二级缓存是%disk%--{A340EFBD-5A9F-444A-B642-A42B5A9CD496}

    rxpcc new -v d -m 4096 -l %disk% -r 255,3 -w 20 -a average,freewritten,flushsleep,flushtoL2 -p=fromL2 -s
    rem D盘,内存4G,二级缓存3%写入,缓存时间20秒,均衡模式,待机刷入缓存,L1满时转L2,-p优先取L2,-s静默
    rxpcc new -v c -m 4096 -w INF -a average,freewritten,flushsleep -p=boot -s
    rem C盘,内存4G,缓存时间无限,均衡模式,待机刷入缓存,-p启动时开始,优先取L2,-s静默
后来发现,在帮同事装机时还得按不同的环境用不同的参数,于是写了个自动设置版初稿
以下批处理在Win10x64中文版下调试,不确定能英文版或磁盘没有卷标的电脑上正确运行,仅做技术讨论,
内有删除分区的操作,数据重要的电脑请务必谨慎,数据掉了我也没办法负责任。
希望有探讨的同学共同完善

代码: 全选

 @echo off
setlocal enabledelayedexpansion
rem ============================================检测机械和固态硬盘的数量=======================================================
set HDD_NUM=0&for /f %%i in ('powershell Get-PhysicalDisk^|find "HDD"') do set HDD=%%i && set /a HDD_NUM=!HDD_NUM! + 1
set SSD_NUM=0&for /f %%i in ('powershell Get-PhysicalDisk^|find "SSD"') do set SSD=%%i && set /a SSD_NUM=!SSD_NUM! + 1
if  %SSD_NUM% gtr 1 echo 多于一只固态,请手动设置PrimoCache.& goto END
rem ============================================检测内存大小=======================================================
for /f "tokens=2*" %%i in ('systeminfo^|find "物理内存总量"') do set memory=%%i %%j
if not "%memory:~-2%"=="MB" set msg3=内存大小超出认知,程序无法正确判断应该怎么设缓存.
rem 去除最后两位,去除中间的逗号
set memory=%memory:~0,-2%
set /a memory=%memory:,=% /1000
if %memory% lss 8 echo 内存太小,还是不使用缓存程序好些.& goto END
if %memory% lss 16 if %memory% gtr 15 set mem=4096
if %memory% lss 13 set mem=2048
if %memory% gtr 15 set mem=4096
echo 内存%memory%G,应设置缓存为%mem%。机械硬盘%HDD_NUM%只位于硬盘排序%HDD%,固态硬盘%SSD_NUM%只位于硬盘排序%SSD%。
rem ============================================检测PrimoCache程序=======================================================
cd /d "C:\Program Files\PrimoCache"
if not exist rxpcc.exe echo 没有找到PrimoCache,现在退出.& goto END
rem ============================================检测机械和固态硬盘的分区=======================================================

:SSD_All_Drv
if not %SSD_NUM%==1 goto SSD_NUM_END
(	echo.select disk %SSD%
	echo.detail disk
)|diskpart|find "磁盘分区">%tmp%\diskpart.txt
set SSD_All_Drv=
for /f "tokens=3*" %%i in (%tmp%\diskpart.txt) do (
	call :str_len "%%i"
	if "!str_len!"=="1" set SSD_All_Drv=!SSD_All_Drv!%%i
	rem echo i=%%i,len=!str_len!,SSD_All_Drv=!SSD_All_Drv!
	)
echo 固态硬盘分区驱动器号是:%SSD_All_Drv%
:SSD_NUM_END
:HDD_All_Drv
if not %HDD_NUM%==1 goto HDD_NUM_END
(	echo.select disk %HDD%
	echo.detail disk
)|diskpart|find "磁盘分区">%tmp%\diskpart.txt
set HDD_All_Drv=
for /f "tokens=3*" %%i in (%tmp%\diskpart.txt) do (
	call :str_len "%%i"
	if "!str_len!"=="1" set HDD_All_Drv=!HDD_All_Drv!%%i
	rem echo i=%%i,len=!str_len!,HDD_All_Drv=!HDD_All_Drv!
	)
echo 机械硬盘分区驱动器号是:%HDD_All_Drv%
:HDD_NUM_END
set /a All_Drv_num=%SSD_NUM% + %HDD_NUM%
set All_Drv=%SSD_NUM%%HDD_NUM%
echo 所有硬盘分区驱动器号是:%All_Drv%
rem ============================================检测引导磁盘=======================================================
for /f "tokens=2" %%i in ('echo list vol ^|diskpart^|find "启动"') do set boot_vol=%%i
(	echo.select vol %boot_vol%
	echo.list disk
)|diskpart|find "*">%tmp%\diskpart.txt
for /f "tokens=3,4" %%i in (%tmp%\diskpart.txt) do if "%%j"=="联机" set boot_disk=%%i
echo 硬盘卷%boot_vol%作为启动盘,属于硬盘%boot_disk%
if %All_Drv_num%==1 goto ONE_HDD
if %SSD_NUM%==1 if %HDD_NUM% gtr 0 goto ONE_SSD_HDD
echo 固态硬盘%SSD_NUM%只,机械硬盘%HDD_NUM%只,不知道怎么配置才好,请手工处理...
goto END
:ONE_HDD
rem ============================================单硬盘模式=======================================================
if not %All_Drv_num%==1 goto ONE_HDD_END
rxpcc new -v %All_Drv% -m %mem% -w INF -a average,freewritten,flushsleep -p=boot -s
echo %All_Drv%盘,内存%mem%,缓存时间无限,均衡模式,待机刷入缓存,-p启动时开始,优先取L2,-s静默
echo 设置完成
:ONE_HDD_END
goto END
:ONE_SSD_HDD
rem ============================================硬盘:一固态一机械或多机械==================================================
(	echo.select disk %SSD%
	echo.detail disk
)|diskpart|find "磁盘分区">%tmp%\diskpart.txt
set size=0
for /f "tokens=2,4,5" %%i in (%tmp%\diskpart.txt) do if "%%k"=="GB" set part=%%i && set size=%%j
del %tmp%\diskpart.txt
echo 第1次找出第%part%分区空间为%size%
if not "%size%"=="0" goto SIZE_NEXT
(	echo.select disk %SSD%
	echo.list partition
)|diskpart|find "RAW">%tmp%\diskpart.txt
set size=0
for /f "tokens=2,4,5" %%i in (%tmp%\diskpart.txt) do if "%%k"=="GB" set part=%%i && set size=%%j
del %tmp%\diskpart.txt
echo 第2次找出第%part%分区空间为%size%
if not "%size%"=="0" goto SIZE_NEXT
(	echo.select disk %SSD%
	echo.list partition
)|diskpart|find "分区">%tmp%\diskpart.txt
for /f "tokens=2,4,5" %%i in (%tmp%\diskpart.txt) do if "%%k"=="GB" set part=%%i && set size=%%j
echo 第3次找出第%part%分区空间为%size%
:SIZE_NEXT
set /a size=%size% + 0
if %size% gtr 200 echo SSD二级缓不对,是%size%GB&goto END
if %size% lss 2   echo SSD二级缓不对,是%size%GB&goto END
echo 上面过程已找到未知分区(二级缓存)空间,这个空间按约定,由第一次使用时手工建立在磁盘空间最末尾

echo ================找到固态硬盘是第%SSD%只,二级缓存是其%part%分区%size%GB,下面删除未识别卷后新建=================
(	echo.select disk %SSD%
	echo.select partition %part%
	echo.delete part
	echo.creat part primary
	echo.list partition
)|diskpart
echo ================找最大的卷号,并格式化成二级缓存================
set max_vol=0
for /f "tokens=2" %%i in ('rxpcc ls^|find "Local Volume"') do (
	set num=%%i
	set num=!num:~1,2!
	if !num! lss 0 set num=0
	if !num! gtr 66 set num=0
	if !num! gtr !max_vol! set max_vol=!num!
	rem echo 原始数据“%%i”,NUM=!num!,MAX=!max_vol!
	)
echo 最大的卷号是!max_vol!
if "!max_vol!"=="0" goto echo 二级卷没找到,最大的卷号%max_vol%...goto END
rxpcc storage format -v %max_vol% -s
echo ================找二级缓存盘,并正式建立缓存================

set disk=没找到盘
for /f "tokens=2" %%i in ('rxpcc ls^|find "Vol"^|find "Level-2"') do set disk=%%i&set free=%j
set free=%free:~0,-2%
for /f "tokens=4" %%i in ('rxpcc ls^|find "%disk%"') do set disk=%%i
echo 硬盘%SSD%是%size%G大小,二级缓存是%disk%--类似{A340EFBD-5A9F-444A-B642-A42B5A9CD496}
if "%disk%"=="没找到盘" echo 没有找到固态硬盘上的二级缓存盘,也没有找到足够剩余空间,请手工建立...&goto END
echo CREAT:rxpcc new -v %HDD_All_Drv% -m %mem% -l %disk% -r 255,3 -w 20 -a average,freewritten,flushsleep,flushtoL2 -p=fromL2 -s
rxpcc new -v %HDD_All_Drv% -m %mem% -l %disk% -r 255,3 -w 20 -a average,freewritten,flushsleep,flushtoL2 -p=fromL2 -s
echo 参数解释%HDD_All_Drv%盘,内存4G,二级缓存3%写入,缓存时间20秒,均衡模式,待机刷入缓存,L1满时转L2,-p优先取L2,-s静默
echo.
echo 准备建立:rxpcc new -v %HDD_All_Drv% -m %mem% -w INF -a average,freewritten,flushsleep -p=boot -s
rxpcc new -v %SSD_All_Drv% -m %mem% -w INF -a average,freewritten,flushsleep -p=boot -s
echo %SSD_All_Drv%盘,内存4G,缓存时间无限,均衡模式,待机刷入缓存,-p启动时开始,优先取L2,-s静默
goto END

rem ==========================字符串长度检测子函数============================
:str_len
	set "str_len_string=%~1"
	rem echo 接收到%str_len_string%
	set str_len=0
	:str_len_loop
	set str_len_string=%str_len_string:~0,-1%
	set /a str_len+=1
	if not "%str_len_string%"==""  (goto str_len_loop)
	rem echo 字符串长度为: %str_len%  
	exit /b  







以下是rxpcc说明书

rxpcc del 参数
  -a, -all                    - 对所有缓存任务执行命令
  -c, -cache <cache>          - 通过标识符或索引指定缓存任务
  -s, -silent                 - 静默模式. 没有交互提示,但错误或警告仍会显示


rxpcc new -v <volume>[,<volume>...]
  [-t <cfgfile>]
  [-m <size>] [-i <size>] [-k <options>]
  [-l <storage>[,<size> [-e <speed>] [-j <options>]]
  [-r <r1>[,<r2>]]
  [-b <blocksize>]
  [-w <latency> [-a <options>]]
  [-p[=<options>]]
  [-x <spec>[,<spec>...]]
  [-s]

  -v, -volume <volume>        - Specifies one or more volumes to be cached by
                                this new cache task. At most 16 volumes can be
                                specified.
                                <volume> states the index or drive letter of a
                                volume. Multiple volumes are separated by ",".
                                Examples: -v 2,3,5
                                          -v c,d,5
										  
								指定要缓存的一个或多个卷,新建缓存最多可有16个卷
								<volume>说明的索引或驱动器号,多个卷用“,”分隔。
  -t, -import <cfgfile>       - Imports the cache configuration from the file
                                <cfgfile>. <cfgfile> specifies the full file
                                path and name of the configuration file.
                                Note: When this option is specified, all cache
                                options written before it in the command will
                                be ignored. However you can modify the imported
                                configuration by appending options after it.
								从文件导入缓存配置,<cfgfile>指定配置文件的路径和名称。
								注意:指定此选项时,所有缓存在命令中写在它之前的选项将
									被忽略。但是,您可以修改导入的配置之后添加选项。
  -m, -mm <size>              - Specifies the size of OS Managed Memory (MM)
                                for level-1 cache, in megabytes (MB).
								指定一级缓存的内存(MM)的大小,单位MB
  -i, -im <size>              - Specifies the size of OS Invisible Memory (IM)
                                for level-1 cache, in megabytes (MB).
								指定一级缓存的操作系统不可见内存(IM)大小,单位MB
  -k, -c1adv <options>        - Specifies the advanced level-1 cache options.
                                <options> states one or more of the following
                                options, in a comma-separated format:
                                    freehiber   - Release L1 Cache on Hybrid-
                                                  Sleep or Hibernation
                                    keepoff     - Keep L1 Cache (MM) on Fast
                                                  Shutdown
                                Note: The option "keepoff" is valid only when
                                the option "freehiber" is not specified and
                                requires Windows 8 or later.
                                Example: -k freehiber
								指定高级一级缓存选项。
								<options>声明以下一个或多个选项,以逗号分隔的格式:
								freeliber-在混合上释放一级缓存-睡眠或冬眠
								keepoff-保持一级缓存(MM)快速打开停机
								注意:选项“禁止”仅在以下情况下有效未指定选项“freeliber”,
									并且需要Windows 8或更高版本。示例:-k freeliber
  -l, -l2 <storage>[,<size>]  - Specifies the level-2 storage and cache size
                                for level-2 cache.
                                <storage> specifies the identifier of a level-2
                                storage.
                                <size> specifies the level-2 cache size, in
                                megabytes (MB). <size> is optional, and if not
                                specified all available level-2 storage space
                                will be claimed for level-2 cache.
								
								指定二级存储和缓存大小
								<storage>指定级别2的标识符存储
								<size>指定二级缓存大小,在兆字节(MB)
								<size>是可选的,如果不是指定了所有可用的二级存储空间将申请二级缓存。
  -e, -wspeed <speed>         - Specifies a recurring interval, in seconds, for
                                gathering cache data into level-2 storage when
                                Windows is busy. <speed> ranges from 0 to 250.
                                Valid only when -l option is specified.
                                If -e option is not specified, or <speed> is 0,
                                program uses its internal setting.
                                If you want to gather level-2 cache data only
                                when system is idle, use "-e IDLE".
                                If you want to gather level-2 cache data
                                instantly, use "-e INSTANT".
								指定的重复间隔(以秒为单位)当Windows正忙<速度>的范围从0到250。
								仅当指定了-l选项时有效。如果没有指定-e选项,或者<speed>为0,
								程序使用其内部设置。如果只想收集二级缓存数据当系统空闲时,使用“-e idle”。
								如果要收集二级缓存数据,使用“-e INSTANT”。
  -j, -c2adv <options>        - Specifies the advanced level-2 cache options.
                                <options> states one or more of the following
                                options, in a comma-separated format:
                                    volatile    - Cache contents in the level-2
                                                  storage are not preserved
                                                  when the system is rebooted.
                                                  This option is usually for
                                                  multi-boot computers where
                                                  another operating system may
                                                  modify cached volumes.
                                    ignoresync  - Cache contents in the level-2
                                                  storage are not reset even if
                                                  they might be out of sync
                                                  with source data.
                                    noverify    - Level-2 cache will not attempt
                                                  to verify cache contents when
                                                  an ungraceful shutdown is
                                                  detected, but will simply clear
                                                  all cache contents.
                                NOTE: "volate", "ignoresync" and "noverify" are
                                exclusive. If multiple options are set at the
                                same time, the priority is as follows:
                                  volate > ignoresync > noverify
                                WARNING: The option "ignoresync" may make the
                                program providing outdated or wrong cache data
                                for read/write requests, and consequently cause
                                unexpected errors and corrupt target volumes!
                                DO NOT enable this option in normal scenarios!
                                Example: -j volatile
								指定高级二级缓存选项。
								<options>声明以下一个或多个选项,以逗号分隔的格式:
								volatile-二级缓存内容,未保留存储当系统重新启动时。
								此选项通常用于多引导计算机,其中另一个操作系统可能修改缓存的卷。
								ignoresync-在级别2中缓存内容,即使它们可能不同步具有源数据。
								noverify-二级缓存不会尝试,验证缓存内容时不礼貌的关闭是已检测到,
								但将简单清除所有缓存内容。
								注意:“volate”、“ignoresync”和“noverify”是独家如果在上设置了多个选项
								同时,优先级如下:
								volate>ignoresync>noverify
								警告:选项“ignoresync”可能会使提供过时或错误缓存数据的程序
								用于读/写请求,并因此导致意外错误和损坏的目标卷!
								在正常情况下不要启用此选项!
								示例:-j volatile
								
  -r, -ratio <r1>[,<r2>]      - Specifies that how much percentage of the cache
                                space will be dedicated to writing cache.
                                <r1> is the ratio value for the level-1 cache,
                                and <r2> is the value for the level-2 cache.
                                Possible values are numbers from 0 to 100, or
                                255 which means the whole cache space is shared
                                by reading and writing.
                                The default values are 255 (shared) for level-1
                                cache and 0 (read-only) for level-2 cache.
                                Example: -r 80,60 means that 80 of L1 cache
                                will be used for writing and 20 for reading,
                                while 60 of L2 cache will be used for writing
                                and 40 for reading.
								指定缓存的百分比,空间将专用于写入缓存。
								<r1>是一级高速缓存的比值,并且<r2>是用于二级高速缓存的值。
								可能的值是0到100之间的数字,或者255,这意味着整个缓存空间是共享的
								通过阅读和写作。
								级别1的默认值为255(共享)
								缓存和0(只读)用于二级缓存。
								示例:-r 80,60表示一级缓存的80
								将用于写入,20用于读取,
								而60个L2缓存将用于写入
								以及用于读取的40。
  -b, -block <blocksize>      - Specifies the block size. <blocksize> can be
                                one of the following:
                                    4, 8, 16, 32, 64, 128, 256, 512.
                                If -b option is not specified, command will
                                automatically choose an appropriate block size.
								指定块大小<块大小>可以是以下其中一项:
								4, 8, 16, 32, 64, 128, 256, 512.
								如果未指定-b选项,则命令将自动选择合适的块大小。
  -w, -dw <latency>           - Enables Defer-Write. <latency> specifies the
                                time interval, in seconds, at which cache task
                                periodically flushs deferred write-data from
                                cache to disk. If you want to set an infinite
                                value for latency, use "-w INF".
								启用延迟写入<latency>指定,缓存任务的时间间隔(以秒为单位)
								定期刷新延迟的写入数据缓存到磁盘。如果你想设置一个无限
								值表示延迟,请使用“-w INF”。
  -a, -dwadv <options>        - Specifies the advanced Defer-Write options.
                                Valid only when -w option is specified.
                                <options> states one or more of the following
                                options, in a comma-separated format:
                                    native      - Write Mode: NATIVE
                                    intelligent - Write Mode: INTELLIGENT
                                    idle        - Write Mode: IDLE-FLUSH
                                    buffer      - Write Mode: BUFFER
                                    average     - Write Mode: AVERAGE
                                    ignorebusy  - Ignore Windows Busy State
                                    freewritten - Free Blocks on Written
                                    flushsleep  - Flush on Sleep
                                    flushtoL2   - Flush L1 Cache to L2 Cache
                                    skipflush   - Skip Flush on Shutdown
                                (WARNING: The option "skipflush" may completely
                                corrupt the whole disk partition!
                                DO NOT use this option in normal scenarios!)
                                Example: -a intelligent,freewritten,flushsleep
								指定高级“延时写入”选项。仅当指定-w选项时有效。
								<options>声明以下一个或多个选项,以逗号分隔的格式:
								native-写入模式:原生
								intelligent-写入模式:智能
								idle-写入模式:空闲-刷新
								buffer-写入模式:缓存
								average-写入模式:均匀
								ignorebsy-忽略Windows繁忙状态
								freewritten-书面上的免费块
								flushsleep-睡眠时冲水
								flushtoL2-将一级缓存刷新到二级缓存
								skipflush-关闭时跳过冲洗
								(警告:选项“skipflush”可能完全损坏了整个磁盘分区!
								在正常情况下不要使用此选项!)
								示例:-一个智能的、自由书写的、flushsleep
  -p, -prefetch [=<options>]  - Enables Prefetch. [options] states one or more
                                of the following options, in a comma-separated
                                format:
                                    boot        - Start at Windows Boot
                                    lock        - Lock Cache Content
                                    fromL2      - Read From Level-2 Cache
                                Example 1: -p
                                Example 2: -p=boot,fromL2
  -x, -voladv <spec>          - By default, all volumes within a cache task
                                follow the task's cache settings. This option
                                can specify individual settings and turn off
                                some unwanted functions for each volume.
                                <spec> states a volume and its settings. It
                                has the following format, where "v" represents
                                the volume index and each "x" represents a
                                setting value which is a decimal number.
                                    v-xxxxx
                                The settings represented by "x", from left to
                                right, are Strategy, L2 Cache, Defer-Write,
                                Prefetch and L1 Cache. The following table
                                lists their possible values.
                                    Strategy:
                                        0   Default
                                        2   Read-Only
                                        3   Write-Only
                                    L1 Cache/L2 Cache/Defer-Write/Prefetch:
                                        0   Default
                                        2   Disabled
                                (Default - Follows the cache task setting)
                                For example, "-x 4-32200" specifies individual
                                settings for the volume #4 as below: Write-Only
                                strategy, L2 Cache disabled, Defer-Write
                                disabled, while Prefetch and L1 Cache are not
                                specified and using the cache task setting.
                                Trailing zeros in the "xxxxx" format can be
                                ignored. So above example equals to "-x 4-322".
                                Two or more <spec> are separated by ",". For
                                example, "-x 4-3220,1-02222" sets the settings
                                for the volume #4 and #1.
  -s, -silent                 - Executes this command in silent mode. No
                                interactive prompts and running information.
                                Errors or warnings are still displayed.

EXAMPLES:

The following command creates a cache task for volumes #1 (C:) and #3 (E:),
specifying 1024MB RAM, 20480MB level-2 cache, 8KB block size:

    rxpcc.exe new -v c,e -m 1024 -l {13D8F992-8F9C-4000-A5F3-6A6AD2872E81},20480 -b 8

The following command creates a cache task for volumes #2, specifying 4096MB
RAM, 4KB block size, 10 seconds defer-write latency and 100 writing cache:

    rxpcc.exe new -m 4096 -b 4 -w 10 -r 100 -v 2

The following command creates a cache task for volumes #2 by importing the
configuration stored in the file "c:\configuration.cfg" and specifying 4096MB
RAM cache which is different from the configuration file:

    rxpcc.exe new -v 2 -t "c:\configuration.cfg" -m 4096



:END
echo del /f /q "%~dpnx0"
echo y|del /f /q "%~dpnx0"
echo 按任意键退出
timeout 12

回复