win10 安装wsl2 centos

win10 powershell(管理员身份)操作

安装choco(windows的包管理工具类似于brew)

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

安装lxrunoffline 管理wsl的一个工具

choco install lxrunoffline -y

centos操作

生成centos7文件系统

脚本来源https://github.com/mishamosher/CentOS-WSL/releases/tag/7.9-2009(可以直接下载CentOS7.zip一键安装里面也有rootfs.tar.gz 哈哈哈)

#!/bin/bash

# Environment variables for the CentOS cloud image
ARCH="x86_64"
OS_VER="7"
ROOTFS_VER="2009"
ROOTFS_FN="CentOS-${OS_VER}-${ARCH}-GenericCloud-${ROOTFS_VER}.qcow2"
ROOTFS_URL="http://cloud.centos.org/centos/${OS_VER}/images/${ROOTFS_FN}"

# Environment variables for Yuk7's wsldl
LNCR_BLD="20112500"
LNCR_ZIP="icons.zip"
LNCR_NAME="CentOS"
LNCR_FN=${LNCR_NAME}.exe
LNCR_ZIPFN=${LNCR_NAME}${OS_VER}.exe
LNCR_URL="https://github.com/yuk7/wsldl/releases/download/${LNCR_BLD}/${LNCR_ZIP}"

# Waits until a file appears or disappears
# - $1   File path to wait for its existence
# - [$2] The string 'a' (default) to wait until the file appears, or 'd' to wait until the file disappears
# - [$3] Timeout in seconds
waitFile() {
  local START=$(cut -d '.' -f 1 /proc/uptime)
  local MODE=${2:-"a"}
  until [[ "${MODE}" = "a" && -e "$1" ]] || [[ "${MODE}" = "d" && ( ! -e "$1" ) ]]; do
    sleep 1s
    if [ -n "$3" ]; then
      local NOW=$(cut -d '.' -f 1 /proc/uptime)
      local ELAPSED=$(( NOW - START ))
      if [ $ELAPSED -ge "$3" ]; then break; fi
    fi
  done
}

# Create a work dir
mkdir wsl
cd wsl

# Download the CentOS cloud image and Yuk7's WSLDL
wget --no-verbose ${ROOTFS_URL} -O ${ROOTFS_FN}
wget --no-verbose ${LNCR_URL} -O ${LNCR_ZIP}

# Extract the CentOS WSL launcher
unzip ${LNCR_ZIP} ${LNCR_FN}

# Clean up
rm ${LNCR_ZIP}

# Mount the qcow2 image
sudo mkdir mntfs
sudo modprobe nbd
sudo qemu-nbd -c /dev/nbd0 --read-only ./${ROOTFS_FN}
waitFile /dev/nbd0p1 "a" 30
sudo mount -o ro /dev/nbd0p1 mntfs

# Clone the qcow2 image contents to a writable directory
sudo cp -a mntfs rootfs

# Unmount the qcow2 image
sudo umount mntfs
sudo qemu-nbd -d /dev/nbd0
waitFile /dev/nbd0p1 "d" 30
sudo rmmod nbd
sudo rmdir mntfs

# Clean up
rm ${ROOTFS_FN}

# Create a tar.gz of the rootfs
sudo tar -zcpf rootfs.tar.gz -C ./rootfs .
sudo chown "$(id -un)" rootfs.tar.gz

# Clean up
sudo rm -rf rootfs

# Create the distribution zip of WSL CentOS
mkdir out
mkdir dist
mv -f ${LNCR_FN} ./out/${LNCR_ZIPFN}
mv -f rootfs.tar.gz ./out/
pushd out
zip -r ../dist/CentOS${OS_VER}.zip ./*
popd

# Clean up
rm -rf out
View Code

 

如果不方便可以到某盘下载

链接:https://pan.baidu.com/s/1FPrRf_vklHggx-UcKdlO7w  提取码:2ifl 
 

win10 powershell(管理员身份)操作

lxrunoffline install -n CentOS -d D:/centos -f .\rootfs.tar.gz
wsl --set-version CentOS 2

 

最后强烈建议安装一下window terminal 跟wsl是绝配

 

posted @ 2021-06-19 18:15  三木燕  阅读(5883)  评论(0编辑  收藏  举报