forked from IAL32/WSL2-Create-Distro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateLinuxDistro.ps1
195 lines (174 loc) · 6.56 KB
/
CreateLinuxDistro.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<#
.SYNOPSIS
Creates a WSL2 distro and optionally adds a user with group
.DESCRIPTION
As a general pourpose, when working for different projects or companies,
one might need to create a self-contained WSL environment, just as one creates a
Virtual Machine.
This script will do just that. It creates a WSL distribution from a tarball file,
and optionally creates a user and adds it to a group (sudo by default).
.EXAMPLE
Create-Linux-Distro.ps1 -INPUT_FILENAME focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "%LOCALAPPDATA%/ubuntu2004-1" -OUTPUT_DISTRONAME ubuntu2004-1 -CREATE_USER $true -CREATE_USER_USERNAME test1 -ADD_USER_TO_GROUP $true -ADD_USER_TO_GROUP_NAME sudo
.EXAMPLE
Create-Linux-Distro.ps1 -INPUT_FILENAME focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "" -OUTPUT_DISTRONAME DISTRONAME -CREATE_USER $true -CREATE_USER_USERNAME test1 -ADD_USER_TO_GROUP $true -ADD_USER_TO_GROUP_NAME blabla
.INPUTS
Help needed here!
.OUTPUTS
Help needed here!
.NOTES
Help needed here!
.COMPONENT
Help needed here!
.ROLE
Help needed here!
.FUNCTIONALITY
Help needed here!
#>
[CmdletBinding(DefaultParameterSetName = 'Parameter Set 1',
SupportsShouldProcess = $true,
PositionalBinding = $false,
ConfirmImpact = 'Medium')]
[Alias()]
[OutputType([String])]
Param (
# Input distribution tarball
[Parameter(
Mandatory = $true,
Position = 0,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
ValueFromRemainingArguments = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidatePattern(".tar.gz$")]
[ValidateScript( {
if ( -Not ($_ | Test-Path) ) {
throw "File or folder does not exist"
}
return $true
})]
[System.IO.FileInfo]
[Alias("i")]
$INPUT_FILENAME,
# Where the distro will be saved
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromRemainingArguments = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]
$OUTPUT_DIRNAME = "$env:LOCALAPPDATA/Packages/WSLDistributions",
# The name of the distribution
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromRemainingArguments = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$OUTPUT_DISTRONAME,
# The default shell
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$DEFAULT_SHELL = "/bin/bash",
# Whether or not to create a user along with the distribution
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false,
ParameterSetName='USER'
)]
[bool]
$CREATE_USER = $false,
# Username of the user to create
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false,
ParameterSetName='USER'
)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidatePattern("[a-zA-Z0-9]+")]
$CREATE_USER_USERNAME,
# The password for the user
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromRemainingArguments = $false,
ParameterSetName='USER'
)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$CREATE_USER_PASSWORD,
# Whether or not to automatically add the user to a group
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false,
ParameterSetName='USER'
)]
[bool]
$ADD_USER_TO_GROUP = $false,
# Which group should the user be added to (default=sudo)
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false,
ParameterSetName='USER'
)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
$ADD_USER_TO_GROUP_NAME = "sudo",
# Which user should be set as the default
[Parameter(
Mandatory = $false,
ValueFromPipeline = $false,
ValueFromRemainingArguments = $false
)]
[ValidateNotNull()]
$SET_USER_AS_DEFAULT = "root",
# The default password for the root user
[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromRemainingArguments = $false
)]
$ROOT_PASSWORD = "root-password-change-me-please-234!"
)
begin {
}
process {
if ($pscmdlet.ShouldProcess("Target", "Operation")) {
Write-Output "Importing distro $OUTPUT_DISTRONAME using $INPUT_FILENAME to $OUTPUT_DIRNAME"
wsl --import $OUTPUT_DISTRONAME $OUTPUT_DIRNAME $INPUT_FILENAME
if ($CREATE_USER) {
Write-Output "Creating user $CREATE_USER_USERNAME"
wsl -d $OUTPUT_DISTRONAME /usr/sbin/useradd -m $CREATE_USER_USERNAME
Write-Output "Setting password for user $CREATE_USER_USERNAME to the selected one"
# Setting password to user
wsl -d $OUTPUT_DISTRONAME /bin/bash -c "echo -e '$CREATE_USER_PASSWORD\n$CREATE_USER_PASSWORD' | /usr/bin/passwd $CREATE_USER_USERNAME"
Write-Output "Setting shell for user $CREATE_USER_USERNAME to $DEFAULT_SHELL"
# Setting default shell for user as bash
wsl -d $OUTPUT_DISTRONAME /usr/sbin/usermod --shell $DEFAULT_SHELL $CREATE_USER_USERNAME
if ($ADD_USER_TO_GROUP) {
Write-Output "Adding user $CREATE_USER_USERNAME to group $ADD_USER_TO_GROUP_NAME"
wsl -d $OUTPUT_DISTRONAME /usr/sbin/adduser $CREATE_USER_USERNAME $ADD_USER_TO_GROUP_NAME
}
}
# Setting password to user
# NOTE: we have to set it BEFORE setting the default user, otherwise it will be
# launched with the user shell, and it will ask us for the password.
Write-Output "Setting default root password to the selected one"
wsl -d $OUTPUT_DISTRONAME /bin/bash -c "echo -e '$ROOT_PASSWORD\n$ROOT_PASSWORD' | /usr/bin/passwd"
Write-Output "Setting default user as $SET_USER_AS_DEFAULT"
# Curtesy of https://github.com/microsoft/WSL/issues/3974#issuecomment-522921145
Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq $OUTPUT_DISTRONAME | Set-ItemProperty -Name DefaultUid -Value ((wsl -d $OUTPUT_DISTRONAME -u $SET_USER_AS_DEFAULT -e id -u) | Out-String);
}
}
end {
}