Merge remote-tracking branch 'upstream/dev' into dev

This commit is contained in:
2Shirt 2021-07-24 02:45:30 -06:00
commit 29f5dab4da
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
10 changed files with 151 additions and 44 deletions

10
scripts/numlock Executable file
View file

@ -0,0 +1,10 @@
#!/bin/env bash
#
## Enable numlock if no battery is detected
## Credit: https://wiki.archlinux.org/title/Activating_numlock_on_bootup#With_systemd_service
if ! compgen -G "/sys/class/power_supply/BAT*" >/dev/null; then
for tty in /dev/tty{1..6}; do
/usr/bin/setleds -D +num < "$tty"
done
fi

View file

@ -2,6 +2,7 @@
# vim: sts=2 sw=2 ts=2
import logging
import math
import os
import shutil
@ -27,6 +28,7 @@ Usage:
[--eset PATH]
[--hdclone PATH]
[--extra-dir PATH]
[EXTRA_IMAGES...]
build-ufd (-h | --help)
Options:
@ -46,22 +48,39 @@ Options:
-U --update Don't format device, just update
'''
LOG = logging.getLogger(__name__)
MIB = 1024 ** 2
ISO_LABEL = f'{KIT_NAME_SHORT}_LINUX'
UFD_LABEL = f'{KIT_NAME_SHORT}_UFD'
# Functions
def apply_image(part_path, image_path):
"""Apply raw image to dev_path using dd."""
cmd = [
'sudo',
'dd',
'bs=4M',
f'if={image_path}',
f'of={part_path}',
]
run_program(cmd)
def build_ufd():
# pylint: disable=too-many-statements
"""Build UFD using selected sources."""
args = docopt(DOCSTRING)
if args['--debug']:
log.enable_debug_mode()
if args['--update'] and args['EXTRA_IMAGES']:
std.print_warning('Extra images are ignored when updating')
log.update_log_path(dest_name='build-ufd', timestamp=True)
try_print = std.TryAndPrint()
try_print.add_error('FileNotFoundError')
try_print.catch_all = False
try_print.verbose = True
try_print.indent = 2
try_print.verbose = True
try_print.width = 64
# Show header
std.print_success(KIT_NAME_FULL)
@ -71,7 +90,8 @@ def build_ufd():
# Verify selections
ufd_dev = verify_ufd(args['--ufd-device'])
sources = verify_sources(args, SOURCES)
show_selections(args, sources, ufd_dev, SOURCES)
extra_images = [io.case_insensitive_path(i) for i in args['EXTRA_IMAGES']]
show_selections(args, sources, ufd_dev, SOURCES, extra_images)
if not args['--force']:
confirm_selections(update=args['--update'])
@ -88,6 +108,7 @@ def build_ufd():
function=create_table,
dev_path=ufd_dev,
use_mbr=args['--use-mbr'],
images=args['EXTRA_IMAGES'],
)
try_print.run(
message='Setting boot flag...',
@ -135,6 +156,18 @@ def build_ufd():
overwrite=True,
)
# Apply extra images
if not args['--update']:
std.print_standard(' ')
std.print_info('Apply Extra Images')
for part_num, image_path in enumerate(extra_images):
try_print.run(
message=f'Applying {image_path.name}...',
function=apply_image,
part_path=f'{str(ufd_dev_first_partition)[:-1]}{part_num+2}',
image_path=image_path,
)
# Update boot entries
std.print_standard(' ')
std.print_info('Boot Setup')
@ -229,17 +262,41 @@ def copy_source(source, items, overwrite=False):
raise FileNotFoundError('One or more items not found')
def create_table(dev_path, use_mbr=False):
def create_table(dev_path, use_mbr=False, images=None):
"""Create GPT or DOS partition table."""
cmd = [
'sudo',
'parted', dev_path,
'--script',
'--',
'--script', '--',
'mklabel', 'msdos' if use_mbr else 'gpt',
'mkpart', 'primary', 'fat32', '4MiB',
'-1s' if use_mbr else '-4MiB',
]
if images:
images = [os.stat(i_path).st_size for i_path in images]
else:
images = []
start = MIB
end = -1
# Calculate partition sizes
## Align all partitions using 1MiB boundaries for 4K alignment
## NOTE: Partitions are aligned to 1 MiB boundaries to match parted's usage
## NOTE 2: Crashing if dev_size can't be set is fine since it's necessary
dev_size = get_block_device_size(dev_path)
part_sizes = [math.ceil(i/MIB) * MIB for i in images]
main_size = dev_size - start*2 - sum(part_sizes)
main_size = math.floor(main_size/MIB) * MIB
images.insert(0, main_size)
part_sizes.insert(0, main_size)
# Build cmd
for part, real in zip(part_sizes, images):
end = start + real
cmd.append(
f'mkpart primary {"fat32" if start==MIB else "hfs+"} {start}B {end-1}B',
)
start += part
# Run cmd
run_program(cmd)
@ -274,6 +331,25 @@ def format_partition(dev_path, label):
run_program(cmd)
def get_block_device_size(dev_path):
"""Get block device size via lsblk, returns int."""
cmd = [
'lsblk',
'--bytes',
'--nodeps',
'--noheadings',
'--output',
'size',
dev_path,
]
# Run cmd
proc = run_program(cmd)
# Done
return int(proc.stdout.strip())
def get_uuid(path):
"""Get filesystem UUID via findmnt, returns str."""
cmd = [
@ -365,7 +441,7 @@ def remove_arch():
shutil.rmtree(io.case_insensitive_path('/mnt/UFD/arch'))
def show_selections(args, sources, ufd_dev, ufd_sources):
def show_selections(args, sources, ufd_dev, ufd_sources, extra_images):
"""Show selections including non-specified options."""
# Sources
@ -378,9 +454,15 @@ def show_selections(args, sources, ufd_dev, ufd_sources):
[f' {label+":":<18}', 'Not Specified'],
[None, 'YELLOW'],
)
std.print_standard(' ')
# Extra images
if extra_images:
print(f' {"Extra Images:":<18} {extra_images[0]}')
for image in extra_images[1:]:
print(f' {" ":<18} {image}')
# Destination
std.print_standard(' ')
std.print_info('Destination')
cmd = [
'lsblk', '--nodeps', '--noheadings', '--paths',

View file

@ -19,7 +19,12 @@ from wk.exe import (
popen_program,
wait_for_procs,
)
from wk.io import delete_folder, get_path_obj, rename_item
from wk.io import (
delete_folder,
get_path_obj,
non_clobber_path,
rename_item,
)
from wk.kit.tools import (
ARCH,
download_tool,
@ -1410,12 +1415,11 @@ def reset_windows_policies():
def reset_windows_updates():
"""Reset Windows Updates."""
system_root = os.environ.get('SYSTEMROOT', 'C:/Windows')
src_path = f'{system_root}/SoftwareDistribution'
dest_path = non_clobber_path(f'{src_path}.old')
try:
rename_item(
f'{system_root}/SoftwareDistribution',
f'{system_root}/SoftwareDistribution.old',
)
delete_folder(f'{system_root}/SoftwareDistribution.old', force=True)
rename_item(src_path, dest_path)
delete_folder(dest_path, force=True)
except FileNotFoundError:
# Ignore
pass

View file

@ -701,40 +701,25 @@ def bytes_to_string(size, decimals=0, use_binary=True):
decimals,
use_binary,
)
scale = 1024 if use_binary else 1000
size = float(size)
abs_size = abs(size)
# Set scale
scale = 1000
suffix = 'B'
if use_binary:
scale = 1024
suffix = 'iB'
suffix = ' ' if use_binary else ' '
units = list('KMGTPEZY')
# Convert to sensible units
if abs_size >= scale ** 5:
size /= scale ** 5
units = 'P' + suffix
elif abs_size >= scale ** 4:
size /= scale ** 4
units = 'T' + suffix
elif abs_size >= scale ** 3:
size /= scale ** 3
units = 'G' + suffix
elif abs_size >= scale ** 2:
size /= scale ** 2
units = 'M' + suffix
elif abs_size >= scale ** 1:
size /= scale ** 1
units = 'K' + suffix
else:
size /= scale ** 0
units = f' {" " if use_binary else ""}B'
size = f'{size:0.{decimals}f} {units}'
while units:
if abs(size) < scale:
break
size /= scale
suffix = units.pop(0)
size_str = (
f'{size:0.{decimals}f} {suffix}'
f'{"iB" if use_binary and suffix.strip() else "B"}'
)
# Done
LOG.debug('string: %s', size)
return size
LOG.debug('string: %s', size_str)
return size_str
def choice(choices, prompt='答えろ!'):

View file

@ -21,6 +21,7 @@ mkvtoolnix-cli
mpv
noto-fonts
noto-fonts-cjk
numlockx
openbox-patched
otf-font-awesome-4
papirus-icon-theme

View file

@ -0,0 +1 @@
/etc/systemd/system/numlock.service

View file

@ -0,0 +1,10 @@
[Unit]
Description=numlock
[Service]
ExecStart=/usr/local/bin/numlock
StandardInput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

View file

@ -2,6 +2,11 @@
#
## Start desktop apps based on WM
# Numlock
if ! compgen -G "/sys/class/power_supply/BAT*" >/dev/null; then
numlockx &
fi
# Start common apps
#picom --backend xrender --xrender-sync --xrender-sync-fence &
picom --daemon || picom --daemon --no-vsync

View file

@ -27,6 +27,10 @@ echo "Installing Gnuplot..."
curl -Lo gnuplot.pkg https://ariadne.ms.northwestern.edu/Download/Gnuplot/gnuplot-5.4.1.pkg
sudo installer -pkg gnuplot.pkg -target /
# memtest
echo "Installing memtest..."
curl -Lo /usr/local/bin/memtest https://github.com/Vavius/memtest/raw/8aa02c8c34db8dd00f4470e482dace7eaff265c7/memtest
# mprime
echo "Installing mprime..."
if [[ "${OS_VERSION:3:2}" -ge "15" ]]; then

View file

@ -96,6 +96,11 @@ rsync -aS /usr/share/zsh "${WK_PATH}/usr/share"/
rsync -aS /usr/local/share/zsh "${WK_PATH}/usr/local/share"/
sed -Ei '' 's!^(root.*)/bin/sh!\1/bin/zsh!' "${WK_PATH}/etc/passwd"
# memtest
sudo mv -nv "${WK_PATH}/usr/local/bin/memtest" "${WK_PATH}/usr/sbin"/
sudo chown 0:0 "${WK_PATH}/usr/sbin/memtest"
sudo chmod 755 "${WK_PATH}/usr/sbin/memtest"
# Misc
cp -a ../linux/known_networks "${WK_PATH}/.known_networks"
cp -a /usr/bin/rsync "${WK_PATH}/usr/bin"/