win10 安装wsl2 centos
<h3>win10 powershell(管理员身份)操作</h3><p>安装choco(windows的包管理工具类似于brew)</p>
<div class="cnblogs_code">
<pre>Set-ExecutionPolicy Bypass -Scope <span style="color: rgba(0, 0, 255, 1)">Process</span> -Force; ::SecurityProtocol = ::SecurityProtocol <span style="color: rgba(0, 128, 128, 1)">-bor</span> 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))</pre>
</div>
<p>安装lxrunoffline 管理wsl的一个工具</p>
<div class="cnblogs_code">
<pre>choco install lxrunoffline -y</pre>
</div>
<h3>centos操作</h3>
<p>生成centos7文件系统</p>
<p>脚本来源https://github.com/mishamosher/CentOS-WSL/releases/tag/7.9-2009(可以直接下载CentOS7.zip一键安装里面也有rootfs.tar.gz 哈哈哈)</p>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_3d6069f7-6cc8-432f-bb4e-3d7550a308cc" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_3d6069f7-6cc8-432f-bb4e-3d7550a308cc" class="cnblogs_code_hide">
<pre>#!/bin/<span style="color: rgba(0, 0, 0, 1)">bash
# Environment variables </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> the CentOS cloud image
ARCH</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">x86_64</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
OS_VER</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">7</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
ROOTFS_VER</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2009</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
ROOTFS_FN</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">CentOS-${OS_VER}-${ARCH}-GenericCloud-${ROOTFS_VER}.qcow2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
ROOTFS_URL</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://cloud.centos.org/centos/${OS_VER}/images/${ROOTFS_FN}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
# Environment variables </span><span style="color: rgba(0, 0, 255, 1)">for</span> Yuk7<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s wsldl</span>
LNCR_BLD=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">20112500</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
LNCR_ZIP</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">icons.zip</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
LNCR_NAME</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">CentOS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
LNCR_FN</span>=<span style="color: rgba(0, 0, 0, 1)">${LNCR_NAME}.exe
LNCR_ZIPFN</span>=<span style="color: rgba(0, 0, 0, 1)">${LNCR_NAME}${OS_VER}.exe
LNCR_URL</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://github.com/yuk7/wsldl/releases/download/${LNCR_BLD}/${LNCR_ZIP}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
# Waits </span><span style="color: rgba(0, 0, 255, 1)">until</span> a <span style="color: rgba(0, 0, 255, 1)">file</span><span style="color: rgba(0, 0, 0, 1)"> appears or disappears
# </span>- $<span style="color: rgba(128, 0, 128, 1)">1</span> File path to <span style="color: rgba(0, 0, 255, 1)">wait</span> <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> its existence
# </span>- [$<span style="color: rgba(128, 0, 128, 1)">2</span>] The <span style="color: rgba(0, 0, 255, 1)">string</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span> (default) to <span style="color: rgba(0, 0, 255, 1)">wait</span> <span style="color: rgba(0, 0, 255, 1)">until</span> the <span style="color: rgba(0, 0, 255, 1)">file</span> appears, or <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">'</span> to <span style="color: rgba(0, 0, 255, 1)">wait</span> <span style="color: rgba(0, 0, 255, 1)">until</span> the <span style="color: rgba(0, 0, 255, 1)">file</span><span style="color: rgba(0, 0, 0, 1)"> disappears
# </span>- [$<span style="color: rgba(128, 0, 128, 1)">3</span>] Timeout <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> seconds
waitFile() {
local START</span>=$(<span style="color: rgba(0, 0, 255, 1)">cut</span> -d <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span> -f <span style="color: rgba(128, 0, 128, 1)">1</span> /proc/<span style="color: rgba(0, 0, 255, 1)">uptime</span><span style="color: rgba(0, 0, 0, 1)">)
local MODE</span>=${<span style="color: rgba(128, 0, 128, 1)">2</span>:-<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}
</span><span style="color: rgba(0, 0, 255, 1)">until</span> [[ <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${MODE}</span><span style="color: rgba(128, 0, 0, 1)">"</span> = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">"</span> && -e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$1</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]] || [[ <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${MODE}</span><span style="color: rgba(128, 0, 0, 1)">"</span> = <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">"</span> && ( ! -e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$1</span><span style="color: rgba(128, 0, 0, 1)">"</span> ) ]]; <span style="color: rgba(0, 0, 255, 1)">do</span>
<span style="color: rgba(0, 0, 255, 1)">sleep</span><span style="color: rgba(0, 0, 0, 1)"> 1s
</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ -n <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$3</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span><span style="color: rgba(0, 0, 0, 1)">
local NOW</span>=$(<span style="color: rgba(0, 0, 255, 1)">cut</span> -d <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.</span><span style="color: rgba(128, 0, 0, 1)">'</span> -f <span style="color: rgba(128, 0, 128, 1)">1</span> /proc/<span style="color: rgba(0, 0, 255, 1)">uptime</span><span style="color: rgba(0, 0, 0, 1)">)
local ELAPSED</span>=$(( NOW -<span style="color: rgba(0, 0, 0, 1)"> START ))
</span><span style="color: rgba(0, 0, 255, 1)">if</span> [ $ELAPSED -ge <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$3</span><span style="color: rgba(128, 0, 0, 1)">"</span> ]; <span style="color: rgba(0, 0, 255, 1)">then</span> break; <span style="color: rgba(0, 0, 255, 1)">fi</span>
<span style="color: rgba(0, 0, 255, 1)">fi</span>
<span style="color: rgba(0, 0, 255, 1)">done</span><span style="color: rgba(0, 0, 0, 1)">
}
# Create a work </span><span style="color: rgba(0, 0, 255, 1)">dir</span>
<span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> wsl
cd wsl
# Download the CentOS cloud image and Yuk7</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">s WSLDL</span>
<span style="color: rgba(0, 0, 255, 1)">wget</span> --no-verbose ${ROOTFS_URL} -<span style="color: rgba(0, 0, 0, 1)">O ${ROOTFS_FN}
</span><span style="color: rgba(0, 0, 255, 1)">wget</span> --no-verbose ${LNCR_URL} -<span style="color: rgba(0, 0, 0, 1)">O ${LNCR_ZIP}
# Extract the CentOS WSL launcher
</span><span style="color: rgba(0, 0, 255, 1)">unzip</span><span style="color: rgba(0, 0, 0, 1)"> ${LNCR_ZIP} ${LNCR_FN}
# Clean up
</span><span style="color: rgba(0, 0, 255, 1)">rm</span><span style="color: rgba(0, 0, 0, 1)"> ${LNCR_ZIP}
# Mount the qcow2 image
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> mntfs
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">modprobe</span><span style="color: rgba(0, 0, 0, 1)"> nbd
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> qemu-nbd -c /dev/nbd0 --read-only ./<span style="color: rgba(0, 0, 0, 1)">${ROOTFS_FN}
waitFile </span>/dev/nbd0p1 <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">30</span>
<span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">mount</span> -o ro /dev/<span style="color: rgba(0, 0, 0, 1)">nbd0p1 mntfs
# Clone the qcow2 image contents to a writable directory
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">cp</span> -<span style="color: rgba(0, 0, 0, 1)">a mntfs rootfs
# Unmount the qcow2 image
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">umount</span><span style="color: rgba(0, 0, 0, 1)"> mntfs
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> qemu-nbd -d /dev/<span style="color: rgba(0, 0, 0, 1)">nbd0
waitFile </span>/dev/nbd0p1 <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">d</span><span style="color: rgba(128, 0, 0, 1)">"</span> <span style="color: rgba(128, 0, 128, 1)">30</span>
<span style="color: rgba(0, 0, 255, 1)">sudo</span><span style="color: rgba(0, 0, 0, 1)"> rmmod nbd
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">rmdir</span><span style="color: rgba(0, 0, 0, 1)"> mntfs
# Clean up
</span><span style="color: rgba(0, 0, 255, 1)">rm</span><span style="color: rgba(0, 0, 0, 1)"> ${ROOTFS_FN}
# Create a </span><span style="color: rgba(0, 0, 255, 1)">tar</span><span style="color: rgba(0, 0, 0, 1)">.gz of the rootfs
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">tar</span> -zcpf rootfs.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz -C ./<span style="color: rgba(0, 0, 0, 1)">rootfs .
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">chown</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">$(id -un)</span><span style="color: rgba(128, 0, 0, 1)">"</span> rootfs.<span style="color: rgba(0, 0, 255, 1)">tar</span><span style="color: rgba(0, 0, 0, 1)">.gz
# Clean up
</span><span style="color: rgba(0, 0, 255, 1)">sudo</span> <span style="color: rgba(0, 0, 255, 1)">rm</span> -<span style="color: rgba(0, 0, 0, 1)">rf rootfs
# Create the distribution </span><span style="color: rgba(0, 0, 255, 1)">zip</span><span style="color: rgba(0, 0, 0, 1)"> of WSL CentOS
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> out
</span><span style="color: rgba(0, 0, 255, 1)">mkdir</span><span style="color: rgba(0, 0, 0, 1)"> dist
</span><span style="color: rgba(0, 0, 255, 1)">mv</span> -f ${LNCR_FN} ./out/<span style="color: rgba(0, 0, 0, 1)">${LNCR_ZIPFN}
</span><span style="color: rgba(0, 0, 255, 1)">mv</span> -f rootfs.<span style="color: rgba(0, 0, 255, 1)">tar</span>.gz ./out/<span style="color: rgba(0, 0, 0, 1)">
pushd out
</span><span style="color: rgba(0, 0, 255, 1)">zip</span> -r ../dist/CentOS${OS_VER}.<span style="color: rgba(0, 0, 255, 1)">zip</span> .<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
popd
# Clean up
rm -rf out</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p> </p>
<p>如果不方便可以到某盘下载</p>
<p>链接:https://pan.baidu.com/s/1FPrRf_vklHggx-UcKdlO7w 提取码:2ifl <br> </p>
<h3>win10 powershell(管理员身份)操作</h3>
<div class="cnblogs_code">
<pre>lxrunoffline install -n CentOS -d D:/centos <span style="color: rgba(0, 128, 128, 1)">-f</span> .\rootfs.tar.gz<br>wsl --set-version CentOS 2</pre>
</div>
<p> </p>
<p>最后强烈建议安装一下window terminal 跟wsl是绝配</p>
<p><img alt="" loading="lazy" src="https://img2020.cnblogs.com/blog/1133264/202106/1133264-20210620121231350-1696605795.png"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/37yan/p/14903821.html
頁:
[1]