Skip to content

Commit

Permalink
MSD 支持运行目录存放镜像
Browse files Browse the repository at this point in the history
  • Loading branch information
mofeng-git committed Oct 5, 2024
1 parent eca4167 commit 507c46b
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ RUN if [ ${TARGETARCH} = arm ]; then ARCH=armhf; elif [ ${TARGETARCH} = arm64 ];
&& chmod +x /usr/local/bin/ttyd \
&& adduser kvmd --gecos "" --disabled-password \
&& ln -sf /usr/share/tesseract-ocr/*/tessdata /usr/share/tessdata \
&& mkdir -p /etc/kvmd_backup/override.d /var/lib/kvmd/msd /var/lib/kvmd/pst/data /opt/vc/bin /run/kvmd /tmp/kvmd-nginx \
&& mkdir -p /etc/kvmd_backup/override.d /var/lib/kvmd/msd/images /var/lib/kvmd/msd/meta /var/lib/kvmd/pst/data /opt/vc/bin /run/kvmd /tmp/kvmd-nginx \
&& touch /run/kvmd/ustreamer.sock


Expand Down
6 changes: 4 additions & 2 deletions build/build_img.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SRCPATH=../src
ROOTFS=/tmp/rootfs
$SRCPATH/image/onecloud/AmlImg_v0.3.1_linux_amd64 unpack $SRCPATH/image/onecloud/Armbian_by-SilentWind_24.5.0-trunk_Onecloud_bookworm_legacy_5.9.0-rc7_minimal.burn.img $SRCPATH/tmp
simg2img $SRCPATH/tmp/7.rootfs.PARTITION.sparse $SRCPATH/tmp/rootfs.img
dd if=/dev/zero of=/tmp/add.img bs=1M count=1024 && cat /tmp/add.img >> $SRCPATH/tmp/rootfs.img && rm /tmp/add.img
dd if=/dev/zero of=/tmp/add.img bs=1M count=800 && cat /tmp/add.img >> $SRCPATH/tmp/rootfs.img && rm /tmp/add.img
e2fsck -f $SRCPATH/tmp/rootfs.img && resize2fs $SRCPATH/tmp/rootfs.img

#挂载镜像文件
Expand All @@ -27,7 +27,7 @@ sudo mount -o bind /dev $ROOTFS/dev || exit -1

#准备文件
sudo mkdir -p $ROOTFS/etc/kvmd/override.d $ROOTFS/etc/kvmd/vnc $ROOTFS/var/lib/kvmd/msd $ROOTFS/opt/vc/bin $ROOTFS/usr/share/kvmd \
$ROOTFS/usr/share/janus/javascript $ROOTFS/usr/lib/ustreamer/janus $ROOTFS/run/kvmd
$ROOTFS/usr/share/janus/javascript $ROOTFS/usr/lib/ustreamer/janus $ROOTFS/run/kvmd $ROOTFS/var/lib/kvmd/msd/images $ROOTFS/var/lib/kvmd/msd/meta
sudo cp -r ../One-KVM $ROOTFS/
sudo cp $SRCPATH/image/onecloud/rc.local $ROOTFS/etc/
sudo cp -r $ROOTFS/One-KVM/configs/kvmd/* $ROOTFS/One-KVM/configs/nginx $ROOTFS/One-KVM/configs/janus \
Expand Down Expand Up @@ -103,6 +103,8 @@ sudo chroot --userspec "root:root" $ROOTFS bash -c " \
&& sed -i 's/device: \/dev\/ttyUSB0//g' /etc/kvmd/override.yaml \
&& sed -i 's/8080/80/g' /etc/kvmd/override.yaml \
&& sed -i 's/4430/443/g' /etc/kvmd/override.yaml \
&& sed -i 's/#type: otg/type: otg/g' /etc/kvmd/override.yaml \
&& chown kvmd -R /var/lib/kvmd/msd/ \
&& sed -i 's/localhost.localdomain/onecloud/g' /etc/kvmd/meta.yaml \
&& systemctl enable kvmd kvmd-otg kvmd-nginx kvmd-vnc kvmd-ipmi kvmd-webterm kvmd-janus \
&& systemctl disable nginx janus \
Expand Down
11 changes: 8 additions & 3 deletions build/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ if [ ! -f /etc/kvmd/.init_flag ]; then
&& /usr/share/kvmd/kvmd-gencert --do-the-thing --vnc \
|| echo -e "${RED}One-KVM config moving and self-signed SSL certificates init failed.${NC}"

if [ "$NOSSL" = 1 ]; then
if [ "$NOSSL" == 1 ]; then
echo -e "${GREEN}One-KVM self-signed SSL is disabled.${NC}" \
&& python -m kvmd.apps.ngxmkconf /etc/kvmd/nginx/nginx.conf.mako /etc/kvmd/nginx/nginx.conf -o nginx/https/enabled=false \
|| echo -e "${RED}One-KVM nginx config init failed.${NC}"
else
python -m kvmd.apps.ngxmkconf /etc/kvmd/nginx/nginx.conf.mako /etc/kvmd/nginx/nginx.conf \
|| echo -e "${RED}One-KVM nginx config init failed.${NC}"
fi



if [ "$NOAUTH" == "1" ]; then
sed -i "s/enabled: true/enabled: false/g" /etc/kvmd/override.yaml \
&& echo -e "${GREEN}One-KVM auth is disabled.${NC}"
Expand Down Expand Up @@ -121,6 +120,12 @@ EOF
echo -e "${YELLOW} USERNAME and PASSWORD environment variables is not set, using defalut(admin/admin).${NC}"
fi

if [ "$NOMSD" == 1 ]; then
echo -e "${GREEN}One-KVM MSD is disabled.${NC}"
else
sed -i "s/#type: otg/type: otg/g" /etc/kvmd/override.yaml
fi

touch /etc/kvmd/.init_flag
fi

Expand Down
2 changes: 1 addition & 1 deletion configs/kvmd/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kvmd:
type: gpio

msd:
type: otg
type: disabled

streamer:
quality: 0
Expand Down
3 changes: 2 additions & 1 deletion configs/kvmd/override.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ kvmd:
device: /dev/kvmd-hid-mouse-alt

msd:
type: disabled
#type: otg
remount_cmd: /bin/true

ocr:
langs:
Expand Down
10 changes: 7 additions & 3 deletions kvmd/apps/otg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ def _mkdir(path: str) -> None:
os.mkdir(path)


def _chown(path: str, user: str) -> None:
get_logger().info("CHOWN --- %s - %s", user, path)
def _chown(path: str, user: str, optional: bool=False) -> None:
logger = get_logger()
if optional and not os.access(path, os.F_OK):
logger.info("CHOWN --- %s - [SKIPPED] %s", user, path)
return
logger.info("CHOWN --- %s - %s", user, path)
shutil.chown(path, user)


Expand Down Expand Up @@ -187,7 +191,7 @@ def add_msd(self, start: bool, user: str, stall: bool, cdrom: bool, rw: bool, re
_chown(join(func_path, "lun.0/cdrom"), user)
_chown(join(func_path, "lun.0/ro"), user)
_chown(join(func_path, "lun.0/file"), user)
_chown(join(func_path, "lun.0/forced_eject"), user)
_chown(join(func_path, "lun.0/forced_eject"), user, optional=True)
if start:
_symlink(func_path, join(self.__profile_path, func))
name = ("Mass Storage Drive" if self.__msd_instance == 0 else f"Extra Drive #{self.__msd_instance}")
Expand Down
10 changes: 8 additions & 2 deletions kvmd/apps/otgmsd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@


# =====
def _has_param(gadget: str, instance: int, param: str) -> bool:
return os.access(_get_param_path(gadget, instance, param), os.F_OK)

def _get_param_path(gadget: str, instance: int, param: str) -> str:
return usb.get_gadget_path(gadget, usb.G_FUNCTIONS, f"mass_storage.usb{instance}/lun.0", param)

Expand Down Expand Up @@ -83,12 +86,15 @@ def main(argv: (list[str] | None)=None) -> None:
if config.kvmd.msd.type != "otg":
raise SystemExit(f"Error: KVMD MSD not using 'otg'"
f" (now configured {config.kvmd.msd.type!r})")

has_param = (lambda param: _has_param(config.otg.gadget, options.instance, param))
set_param = (lambda param, value: _set_param(config.otg.gadget, options.instance, param, value))
get_param = (lambda param: _get_param(config.otg.gadget, options.instance, param))

if options.eject:
set_param("forced_eject", "")
if has_param("forced_eject"):
set_param("forced_eject", "")
else:
set_param("file", "")

if options.set_cdrom is not None:
set_param("cdrom", str(int(options.set_cdrom)))
Expand Down
6 changes: 5 additions & 1 deletion kvmd/fstab.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ def find_pst() -> Partition:
def _find_single(part_type: str) -> Partition:
parts = _find_partitions(part_type, True)
if len(parts) == 0:
raise RuntimeError(f"Can't find {part_type!r} mountpoint")
if os.path.exists('/var/lib/kvmd/msd'):
#set default value
parts = [Partition(mount_path='/var/lib/kvmd/msd', root_path='/var/lib/kvmd/msd', user='kvmd')]
else:
raise RuntimeError(f"Can't find {part_type!r} mountpoint")
return parts[0]


Expand Down
6 changes: 4 additions & 2 deletions kvmd/plugins/msd/otg/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def get_watchable_paths(self) -> list[str]:
# =====

def set_image_path(self, path: str) -> None:
if path:
if path or not self.__has_param("forced_eject"):
self.__set_param("file", path)
else:
self.__set_param("forced_eject", "")
Expand All @@ -75,7 +75,9 @@ def get_rw_flag(self) -> bool:
return (not int(self.__get_param("ro")))

# =====

def __has_param(self, param: str) -> bool:
return os.access(os.path.join(self.__lun_path, param), os.F_OK)

def __get_param(self, param: str) -> str:
with open(os.path.join(self.__lun_path, param)) as file:
return file.read().strip()
Expand Down

0 comments on commit 507c46b

Please sign in to comment.