load_os_conf() { if [[ -f $os_conf_file ]]; then . $os_conf_file echo$password return 0 else return 1 fi }
cleanup() { # ensure $os_conf_file is deleted, to avoid repeating config system rm $os_conf_file >& /dev/null # ensure $os_conf_dir is exitst mkdir -p $os_conf_dir }
config_password() { if [[ -n $password ]]; then password=$(echo$password | base64 -d) if [[ $? == 0 && -n $password ]]; then echo"root:$password" echo"root:$password" | chpasswd fi fi } config_hostname() { if [[ -n $hostname ]]; then echo"$hostname" > /etc/hostname hostnamectl set-hostname $hostname fi } config_network() { if [[ -n $eth0_ip_addr ]]; then config_interface systemctl restart systemd-networkd fi } config_interface() { mask2cdr $eth0_netmask cat << EOF > /etc/systemd/network/default.network # Generated by Aliyun Custom OS helper # DO NOT EDIT THIS FILE! IT WILL BE OVERWRITTEN
[Match] Name=$(ip link | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2a;getline}' | sed -e 's/^[[:space:]]*//')
[Address] Address=$eth0_ip_addr/$netmask EOF echo"nameserver 1.1.1.1" > /etc/resolv.conf for ns in$dns_nameserver do echo"nameserver $ns" >> /etc/resolv.conf done }
mask2cdr() { # Assumes there's no "255." after a non-255 byte in the mask local x=${1##*255.} set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*} x=${1%%$3*} netmask=$(( $2 + (${#x}/4) )) }
if load_os_conf ; then config_password config_hostname config_network cleanup else echo"not load $os_conf_file" fi
赋予执行权限
1
# chmod +x /usr/bin/aliyun-custom-os
新建 systemd unit 文件 /usr/lib/systemd/system/aliyun-custom-os.service 写入内容
Disk archlinux.img:0MB,0bytes 255heads,63sectors/track,0cylinders Units=cylindersof16065*512=8225280bytes Sectorsize(logical/physical):512bytes/512bytes I/Osize(minimum/optimal):512bytes/512bytes Disk identifier:0x91d8e293
DeviceBootStartEndBlocksIdSystem archlinux.img1*12611 2096947283Linux Partition 1 has different physical/logical endings: phys=(1023,254,63)logical=(2610,180,2)
得知 sectorsize 为 512。
使用 mount 命令带 offset 参数挂载镜像中的分区:
1 2
$ sudo mkdir -p /mnt/img $ sudo mount -t ext4 -o loop,offset=$((2048*512)) /path/to/archlinux.img /mnt/img/# 更改 -t auto 或者其他此分区使用的文件系统格式