Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

Commit

Permalink
Powershell Port: Create extract.ps1, partition.ps1, and system.ps1 an…
Browse files Browse the repository at this point in the history
…d fix others
  • Loading branch information
MilkyDeveloper committed Apr 29, 2022
1 parent 6460b2d commit bb53b74
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 6 deletions.
10 changes: 5 additions & 5 deletions ports/powershell/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ if ($env:FEATURES -like "*CROSTINI*") {
}

# Import functions
. utils/functions.sh # Functions
# source utils/bootstrap.sh # Bootstrap Function
# source utils/partition.sh # USB Partitioning
# source utils/extract.sh # Extract Rootfs
# source utils/system.sh # Host OS Abstraction
. utils/functions.ps1 # Functions
. utils/bootstrap.ps1 # Bootstrap Function
. utils/partition.sh # USB Partitioning
. utils/extract.sh # Extract Rootfs
. utils/system.sh # Host OS Abstraction
2 changes: 1 addition & 1 deletion ports/powershell/utils/bootstrap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function bootstrapFiles {
# Exit if there are files in /mnt
if ((ls -A ${MNT})) {
sudo rm -rf "${MNT}/lost+found"
printerr "There are files in ${MNT}! Please clear this directory of any valuable information!"; exit
#printerr "There are files in ${MNT}! Please clear this directory of any valuable information!"; exit
}

# Make our build directory and CD into it
Expand Down
55 changes: 55 additions & 0 deletions ports/powershell/utils/extract.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function extractRootfs {

printq "Extracting the Root Filesystem"

# READ: Distro dependent step
switch ($DISTRO) {
"arch" {
$DISTRO_ROOTFS_ABSOLUTE=(readlink -f $DISTRO_ROOTFS)
sudo rm -rf /tmp/arch || true
sudo mkdir /tmp/arch
cd /tmp/arch # The -c option doesn't work when using the command below
sudo tar xvpfz $DISTRO_ROOTFS_ABSOLUTE root.x86_64/ --strip-components=1
cd ~/linux-build
}
"fedora" {
# Make the fedora directory
# If it fails, remove the directory and try to make it again
sudo rm -rf fedora &> /dev/null || true
mkdir fedora
sudo tar xvpf $DISTRO_ROOTFS -C fedora
cp fedora/6c00560306a90bf0718a9a003defbc89a0d6441b8ec719a69416eba6a06c3218/layer.tar rootfs.tar
$DISTRO_ROOTFS_ABSOLUTE=(readlink -f rootfs.tar)
cd $MNT
sudo tar --wildcards -xvpf "$DISTRO_ROOTFS_ABSOLUTE" "./*"
cd ~/linux-build
}
"debian" {
# Debootstrap Debian Unstable
sudo debootstrap unstable $MNT http://deb.debian.org/debian/
}
Default {
# Assume any other distro has all the root files in the root of the archive
# Extract the rootfs to the USB Drive
sudo tar xvpf $DISTRO_ROOTFS -C $MNT
}
}

syncStorage

}

function extractModules {

# It is crucial to have MNT defined
# Otherwise, we overwrite the host systems /lib/modules
checkMNT

sudo mkdir -p ${MNT}/lib/modules
mkdir -p modules || sudo rm -rf modules; sudo mkdir -p modules
sudo tar xvpf modules.tar.xz -C modules
sudo rm -rf ${MNT}/lib/modules/*
sudo cp -rv modules/lib/modules/* ${MNT}/lib/modules
syncStorage

}
8 changes: 8 additions & 0 deletions ports/powershell/utils/functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ function unmountUSB {
# Function for running a command in the chroot
function runChrootCommand([string]$text) {
sudo chroot $MNT /bin/sh -c "$text"
}

function checkMNT() {
# We have to flip this if statement for PSScriptAnalyzer
if ($null -eq $MNT) {
printerr "The MNT variable ($MNT) is not defined. This is a programming error. Aborting."
exit 1
}
}
24 changes: 24 additions & 0 deletions ports/powershell/utils/partition.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# Function for partitioning the USB
function partitionUSB {

# It's crucial that we have MNT defined by this stage
checkMNT

# Format the USB with GPT
# READ: https://wiki.gentoo.org/wiki/Creating_bootable_media_for_depthcharge_based_devices
sudo parted -s $USB mklabel gpt
syncStorage

# Create a 65 Mb kernel partition
# Our kernels are only ~10 Mb though
sudo parted -s -a optimal $USB unit mib mkpart Kernel 1 65

# Add a root partition
sudo parted -s -a optimal $USB unit mib mkpart Root 65 100%

# Make depthcharge know that this is a real kernel partition
sudo cgpt add -i 1 -t kernel -S 1 -T 5 -P 15 $USB

}
92 changes: 92 additions & 0 deletions ports/powershell/utils/system.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash

# Source other files to simplify importing this script on more minimal bash scripts
# (e.g. expand.sh or genimg.sh)
DIR=( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source ${DIR}/functions.sh

# Determine host system
function whichOperatingSystem {

$OS=(uname -s)

# Determine operating system and distro
if ( $OS == Linux ) {

# Debian
if (Test-Path /etc/debian_version ) {
DIST="Debian"
# Arch
} elseif (Test-Path /etc/arch-release ) {
DIST="Arch"
# Fedora
} elseif (Test-Path /etc/fedora-release ) {
DIST="Fedora"
}
# Other
else {
printerr "This distribution is not supported, exiting!"
exit
}

} else {
printerr "$OS is not supported, exiting!"
exit
}

}

# Install dependencies based on distro
function installDependencies {

# NOTE: "$*" is used to call all function arguments

# Identify the distro if it is undefined
if ( $null -eq $DIST ) {
whichOperatingSystem
}

echo "Installing $args"

# Download dependencies based on distribution
switch ($DIST) {
"Debian" {
sudo apt install -y "$args"
}
"Arch" {
if (Test-Path "/usr/bin/yay") {
# Install dependencies on an Arch host system
foreach ($package in $args) {
# Replace package names relevant to distro
switch ($package) {
"vboot-kernel-utils" { $package = "vboot-utils" }
"cloud-guest-utils" { $package = "growpartfs" }
"cgpt" { Clear-Variable package } # Included in vboot-utils
Default {}
}

yay -S --noconfirm $package
}
} else {
# Yay not istalled, exit
printerr "Please install yay(https://aur.archlinux.org/yay.git) to continue installation, exiting!"
exit
}
}
"Fedora" {
# Install dependencies on a Fedora host system
foreach ($package in $args) {
# Replace package names relevant to distro
switch ($package) {
"vboot-kernel-utils" { $package = "vboot-utils" }
"cloud-guest-utils" { $package = "cloud-utils-growpart" }
"cgpt" { Clear-Variable package } # Included in vboot-utils
Default {}
}

sudo dnf install $package --assumeyes
}
}
Default {}
}
}

0 comments on commit bb53b74

Please sign in to comment.