AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-178625

Shan-Desai's questions

Martin Hope
Shan-Desai
Asked: 2022-09-17 01:03:53 +0800 CST

syslinux 引导加载程序无法找到通过 Docker 容器制作的自定义可引导映像的 initrd.img

  • 0

描述

作为一个附带项目,我正在尝试使用Hashicorp Packer和Docker从 Docker 容器创建可启动磁盘映像。

Hashicorp Packer通常负责启动 docker 容器并在其中执行任务,然后创建文件系统 tarball 或使用容器创建图像文件等。

这个怎么运作

  1. 我使用ubuntu:focaldocker 容器

  2. 使用这个 docker 容器并在其上安装一个 Kernel / Systemd:

     apt-get update && apt-get install -y --no-install-recommends linux-image-virtual systemd-sysv
    
  3. 创建容器的 tarball,其中现在包含步骤 2 中的内核、initrd 和系统

  4. 在主机上解压缩 tarball 以便在创建映像文件时使用它

  5. 启动另一个ubuntu具有权限的容器并将工作目录挂载到其中以创建可启动映像文件。

此时,我依靠下面的 bash 脚本为我创建图像:

set -e
echo "[Install APT dependencies]"
DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y extlinux fdisk qemu-utils

echo "[Create disk image of 1GB]"
dd if=/dev/zero of=/os/${DISTR}.img bs=$(expr 1024 \* 1024 \* 1024) count=1

blue "[Make partition]"
sfdisk /os/${DISTR}.img < /os/config/partition.txt

echo "\n[Format partition with ext4]"
losetup -D
LOOPDEVICE=$(losetup -f)
echo -e "\n[Using ${LOOPDEVICE} loop device]"
losetup -o $(expr 512 \* 2048) ${LOOPDEVICE} /os/${DISTR}.img
mkfs.ext4 ${LOOPDEVICE}

echo "[Copy ${DISTR} directory structure to partition]"
mkdir -p /os/mnt
mount -t auto ${LOOPDEVICE} /os/mnt/
cp -R /os/${DISTR}.dir/. /os/mnt/


echo "[Setup extlinux]"
extlinux --install /os/mnt/boot/
cp /os/config/syslinux.cfg /os/mnt/boot/syslinux.cfg

echo "[Unmount]"
umount /os/mnt
losetup -D

echo_blue "[Write syslinux MBR]"
dd if=/usr/lib/syslinux/mbr/mbr.bin of=/os/${DISTR}.img bs=440 count=1 conv=notrunc

DISTR上面脚本中ubuntu的是ubuntu.dir解压后的 tarball 球,它被安装到容器中,以便能够将其复制到安装的分区。

进步

目前我已经成功地为ubuntu. 创建的存储库可以在这里找到

但是,在尝试使用以下命令启动映像时qemu-system-x86_64:

sudo qemu-system-x86_64 -drive file=ubuntu.img,index=0,media=disk,format=raw 

我收到以下错误: 初始化错误 1

引导加载程序的相应syslinux.cfg内容是:

DEFAULT linux
  SAY Now booting the kernel from SYSLINUX...
 LABEL linux
  KERNEL /boot/vmlinuz
  INITRD /boot/initrd.img-5.4.0-125-generic
  APPEND ro root=/dev/sda1

其他更改

我尝试更新syslinux.cfg到

DEFAULT linux
  SAY Now booting the kernel from SYSLINUX...
 LABEL linux
  KERNEL /boot/vmlinuz
  APPEND ro root=/dev/sda1 initrd=/boot/initrd.img

但是我得到了同样的错误,尽管/boot/initrd.img没有找到错误信息。

我目前在我身边缺少什么以使initrd.img引导加载程序可以发现。

我尝试cp -dR /os/ubuntu.dir/. /os/mnt在 bash 脚本中使用以确保在复制时保留符号链接。

我还确保查看符号链接文件是否存在于它存在initrd.img的已安装分区中,符号链接文件的源也是如此,例如/os/mnt/bootreadlink -f /os/mnt/boot/initrd.img

来源

iximiuz blogpost on docker-to-bootable devices PockerISO

docker boot-loader
  • 1 个回答
  • 14 Views
Martin Hope
Shan-Desai
Asked: 2022-07-01 06:17:47 +0800 CST

jq 覆盖唯一键而不是添加到文件中的 JSON 对象

  • 0

JSON文件

当前的

{
  "auths": {
    "test": {
      "auth": "testserfdddd"
    }
  }
}

期望的

{
  "auths": {
    "test": {
      "auth": "testserfdddd"
    },
    "myrepo.com" {
      "auth": "passworder"
    }
  }
}

测试

作为命令行上的一个简单测试,我执行以下操作:

 cat .docker/config.json | jq '.auths += {"myrepo.com": {"auth": "passworder"}}'

结果是我想要的

{
  "auths": {
    "test": {
      "auth": "testserfdddd"
    },
    "repo.com": {
      "auth": "test"
    }
  }
}

但是我希望通过 bash 脚本执行相同的逻辑。

Bash 脚本

REPO=repo.com
PASSWD=passworder

$JQ --arg repo "$REPO" --arg passwd "$PASSWD" '.auths.[$repo] += {"auth": $passwd}' .docker/config.json

但是,这会覆盖test.authtorepo.com.auth并且不会添加到auths密钥

运行 bash 脚本时的结果提供以下结果

{
  "auths": {
    "repo.com": {
      "auth": "passworder
    }
  }
}

前一个对象被完全覆盖。我需要在jq表达式中适应的模式通常是什么?由于参数repo是唯一的(test与不同repo.com),为什么该+=操作在 bash 脚本中不起作用?

jq
  • 2 个回答
  • 96 Views
Martin Hope
Shan-Desai
Asked: 2022-06-07 06:09:24 +0800 CST

对于字符串数组的每个元素,bash 无法识别 for 循环中的条件

  • 3

目标

我用来让用户将密码插入与某些模板文件whiptail一起使用的文件中。envsubst

whiptail清单

为了渲染东西,whiptail我使用以下内容:

declare -A availableServices=(
    [grafana]="Grafana Dashboard"
    [influxdb]="InfluxDB v1.x TSDB"
    [node-red]="Node-RED Flow UI"
    [portainer]="Portainer Container Mgmt"
)

# Render TUI
function askGenerateCredential() {
    local message="Set Password for particular Service\n
                Press <SPACEBAR> to Select \n
                Press <Enter> to Skip
                "
    local arglist=()
    
    # Generate a String for Whiptail Checkboxes
     # FORMAT: "<INDEX> <DESCRIPTION> <OFF>"
    for index in "${!availableServices[@]}";
    do
        # Default all Services are NOT-Selected (OFF)
        arglist+=("$index" "${availableServices[$index]}" "OFF")
    done
    SELECTED_SERVICES+=$($WHIPTAIL --title "Available Services within Populo" \
                --notags --separate-output \
                --ok-button Next \
                --nocancel \
                --checklist "$message" $LINES $COLUMNS $(( LINES - 12 )) \
                -- "${arglist[@]}" \
                3>&1 1>&2 2>&3)
}

theSELECTED_SERVICES是一个声明为的数组

declare -a SELECTED_SERVICES=()

渲染效果很好,但是我的if else脚本中有一个特定条件,无法根据上面数组中的选定服务执行特定命令。

askGenerateCredential
if [ -z "$SELECTED_SERVICES" ]; then
    echo "No Services were selected. Exiting..."
    exit 1
else
    for service in "${SELECTED_SERVICES}";
    do
        if [ "$service" == "influxdb" ]; then
            echo "Setting Credentials for InfluxDB"
            setInfluxDBCredentials
        elif [ "$service" == "grafana" ]; then
            echo "Setting Credentials for Grafana"
            setGrafanaCredentials
        elif [ "$service" == "node-red" ]; then
            echo "Setting Credentials for node-RED"
            setNodeRedCredentials
        elif [ "$service" == "portainer" ]; then
            echo "Setting Credential for Portainer"
            setPortainerCredentials
        fi
    done
fi

添加上述块后,当我通过UI0选择多个值时,我的脚本会以代码退出。whiptail相反,当我在 UI 中只选择一个值时,会调用相应的函数。

我在这里做错了什么?我希望根据service变量,调用相应的函数而不退出脚本。

代码

#!/bin/env bash

WHIPTAIL=$(which whiptail)

if [ -z $WHIPTAIL ]
then
    echo "This script requires whiptail to render the TUI."
    exit 1
fi

declare -a SELECTED_SERVICES=()

LINES=$(tput lines)
COLUMNS=$(tput cols)


declare -A availableServices=(
    [grafana]="Grafana Dashboard"
    [influxdb]="InfluxDB v1.x TSDB"
    [node-red]="Node-RED Flow UI"
    [portainer]="Portainer Container Mgmt"
)

function askGenerateCredential() {
    local message="Set Password for particular Service\n
                Press <SPACEBAR> to Select \n
                Press <Enter> to Skip
                "
    local arglist=()
    
    # Generate a String for Whiptail Checkboxes
     # FORMAT: "<INDEX> <DESCRIPTION> <OFF>"
    for index in "${!availableServices[@]}";
    do
        # Default all Services are NOT-Selected (OFF)
        arglist+=("$index" "${availableServices[$index]}" "OFF")
    done
    SELECTED_SERVICES+=$($WHIPTAIL --title "Available Services within Populo" \
                --notags --separate-output \
                --ok-button Next \
                --nocancel \
                --checklist "$message" $LINES $COLUMNS $(( LINES - 12 )) \
                -- "${arglist[@]}" \
                3>&1 1>&2 2>&3)
}

function setInfluxDBCredential() {
    echo "set admin password env var for influxdb"
}

function setGrafanaCredential() {
    echo "set admin password env var for grafana"
}

function setNodeRedCredential() {
    echo "set admin password env var for node-REd"
}

function setPortainerCredential() {
    echo "set password file for portainer"
}

askGenerateCredential
if [ -z "$SELECTED_SERVICES" ]; then
    echo "No Services were selected. Exiting..."
    exit 1
else
    for service in "${SELECTED_SERVICES}";
    do
        if [ "$service" == "influxdb" ]; then
            echo "Setting Credentials for InfluxDB"
            setInfluxDBCredentials
        elif [ "$service" == "grafana" ]; then
            echo "Setting Credentials for Grafana"
            setGrafanaCredentials
        elif [ "$service" == "node-red" ]; then
            echo "Setting Credentials for node-RED"
            setNodeRedCredentials
        elif [ "$service" == "portainer" ]; then
            echo "Setting Credential for Portainer"
            setPortainerCredentials
        fi
    done
fi

编辑

根据建议,我${SELECTED_SERVICES[@]}在 for 循环中进行了尝试,并添加了一个echo "$service"能够循环遍历数组的函数,但是if条件不会被触发。

bash shell-script
  • 1 个回答
  • 200 Views
Martin Hope
Shan-Desai
Asked: 2018-05-18 04:56:40 +0800 CST

systemd 服务在重启时不会触发 tmux 命令

  • 2

我正在使用基于linux-mainline内核的 Yocto 映像。我有systemd我创建的嵌入式操作系统。

目标

我有一个连接到电路板的 UMTS 加密狗,usb_modeswitch用于识别加密狗并wvdial连接到 3G 基础设施。

方法

我写了一个简单的bash脚本/usr/umts.sh如下

#!/bin/bash

sleep 1;
usb_modeswitch --default-vendor 12d1 --default-product 1446 -J

sleep 1;
/usr/bin/tmux new-session -d -s Cloud
/usr/bin/tmux set-option set-remain-on-exit on
/usr/bin/tmux new-window -d -n 'wvdial' -t Cloud:1 'sleep 1; /usr/bin/wvdialconf; /usr/bin/wvdial';

usb_modeswitch将配置 USB 加密狗,下一部分将创建一个tmux会话并在其中触发wvdial。

我的systemd脚本如下所示/etc/systemd/system/enable-umts.service

GNU nano 2.2.5 文件:/etc/systemd/system/enable-umts.service

[Unit]
Description=Enable UMTS Dongle for Cloud Connectivity

[Service]
Type=oneshot
ExecStart=/usr/umts.sh

[Install]
WantedBy=default.target

(我已使用 赋予脚本执行权chmod +x /usr/umts.sh)

我重新加载了守护进程systemctl daemon-reload并启用了服务systemctl enable enable-umts.service

并重新启动电路板(注意:我只有root在船上,没有其他user)

事实从上面的脚本dmesg中触发usb_modeswitch,我看到了相关消息

[  OK  ] Started Enable UMTS Dongle for Cloud Connectivity.
[   13.051247] scsi host2: usb-storage 1-1:1.5
[   13.067326] usb-storage 1-1:1.6: USB Mass Storage device detected
[   13.074645] scsi host3: usb-storage 1-1:1.6
[   13.158627] usbcore: registered new interface driver usbserial
[   13.165501] usbcore: registered new interface driver usbserial_generic
[   13.174754] usbserial: USB Serial support registered for generic
[   13.202356] usbcore: registered new interface driver option
[   13.208714] usbserial: USB Serial support registered for GSM modem (1-port)
[   13.216468] option 1-1:1.0: GSM modem (1-port) converter detected
[   13.229840] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
[   13.238774] option 1-1:1.3: GSM modem (1-port) converter detected
[   13.248906] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
[   13.256172] option 1-1:1.4: GSM modem (1-port) converter detected
[   13.264467] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB2
[   14.069960] scsi 2:0:0:0: CD-ROM            HUAWEI   Mass Storage     2.31 PQ: 0 ANSI: 2
[   14.088684] scsi 3:0:0:0: Direct-Access     HUAWEI   TF CARD Storage       PQ: 0 ANSI: 2
[   14.127686] sd 3:0:0:0: [sda] Attached SCSI removable disk
GSM modem (1-port) converter detected
] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0
[   13.238774] option 1-1:1.3: GSM modem (1-port) converter detected
[   13.248906] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1
[   13.256172] option 1-1:1.4: GSM modem (1-port) coPassword

但是在登录时,我尝试列出会话tmux ls,并指出没有创建会话。

`脚本实际上并没有失败,服务状态如下:

● enable-umts.service - Enable UMTS Dongle for Cloud Connectivity
   Loaded: loaded (/etc/systemd/system/enable-umts.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Thu 2018-05-17 12:52:10 UTC; 4min 51s ago
  Process: 214 ExecStart=/usr/umts.sh (code=exited, status=0/SUCCESS)
 Main PID: 214 (code=exited, status=0/SUCCESS)

May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Set up interface 0
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Use endpoint 0x01 for message sending ...
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Trying to send message 1 to endpoint 0x01 ...
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  OK, message successfully sent
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Reset response endpoint 0x81
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: Reset message endpoint 0x01
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  Could not reset endpoint (probably harmless): -99
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]:  Device is gone, skip any further commands
May 17 12:52:09 phyboard-mira-imx6-3 umts.sh[214]: -> Run lsusb to note any changes. Bye!
May 17 12:52:10 phyboard-mira-imx6-3 systemd[1]: Started Enable UMTS Dongle for Cloud Connectivity.

如果将脚本/usr/umts.sh作为独立脚本执行,那么实际上它会完成它应该做的所有事情(打开一个新tmux会话并触发wvdial)

这里有什么问题?我尝试增加更多sleep时间,但这也不起作用。遗憾的是,我正在处理的 yocto 图像没有rc.local或可用。cron

bash systemd
  • 1 个回答
  • 1681 Views
Martin Hope
Shan-Desai
Asked: 2018-04-28 08:44:21 +0800 CST

通过 stty 对串行设置进行故障排除

  • 2

硬件

带有i.MX6处理器的PHYTEC Mira 板

操作系统

Yocto Image 使用 PHYTEC Source提供的 BSP 创建,其中包含最少的包。

该板有一个称为UART3的 UART 板,其在操作系统中的软件接口是/dev/ttymxc2 Hardware Manual。唯一可用于检查/设置板上串行端口的是stty.

任务

我希望将Adafruit Ultimate GPS连接到UART3,以在 Mira Board 上从中读取信息。

尝试1

我以以下方式连接了两个组件。

UART3_RXD_RS232 (MIRA) --> TX pin (GPS)
UART3_TXD_RS232 (MIRA) --> RX pin (GPS)

设置串口如下:

 stty -F /dev/ttymxc2 speed 9600

读取值:

 cat /dev/ttymxc2

结果:垃圾值。尝试了所有可能的设置,仍然获得垃圾值。编写了一个简单的node脚本来尝试读取来自端口的信息,但我收到错误消息,指出无法识别字符(垃圾值)。

尝试2

最初,我尝试将 GPS 连接到一个简单的 Arduino Nano 以从 GPS 获取值,这可以正常工作,确认 GPS 发送信息并且没有发生缺陷。

我连接 Mira 和 Arduino 的串行端口,并尝试将 Mira 的信息发送到 Arduino 的串行接口并通过串行控制台读取。

设置

   MIRA_Board (serial UART3) ---> Arduino Nano (Serial Pins) --USBCable--> Computer

引脚

 UART3_RXD_RS232 (MIRA) --> RX PIN NANO
 UART3_TXD_RS232 (MIRA) --> TX PIN NANO

我通过 SSH 登录 Mira 板。下面的命令被触发,希望通过 Arduino 在计算机的串口控制台上期望相同的值

 echo 'hello' > /dev/ttymxc2

结果:控制台上仍然存在垃圾值。 腻子上的结果

的配置/dev/ttymxc2如下

stty -F /dev/ttymxc2 -a                       
speed 9600 baud;stty: /dev/ttymxc2 line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;   -parenb -parodd cs8 hupcl -cstopb cread clocal –crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8  
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0   
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

如果此问题是由于串行配置不匹配造成的,我该如何使用 进行故障排除stty?

PS:GPS的波特率应该是9600这种情况

yocto stty
  • 1 个回答
  • 1068 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve