From c67d3618438a474ef0fe2f60ab7b6f5405293ce2 Mon Sep 17 00:00:00 2001 From: LEI WANG Date: Tue, 27 Aug 2024 18:16:50 +0800 Subject: [PATCH 1/4] support ubuntu12 Signed-off-by: LEI WANG --- builder.go | 13 +++++-- convert.go | 2 +- dockerfile.go | 19 +++++++--- templates/ubuntu12.Dockerfile | 67 +++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 templates/ubuntu12.Dockerfile diff --git a/builder.go b/builder.go index 8c3fb87..11edf44 100644 --- a/builder.go +++ b/builder.go @@ -280,6 +280,13 @@ func (b *builder) makeImg(ctx context.Context) error { } func (b *builder) mountImg(ctx context.Context) error { + var mkfsExt4Opts []string + r := ctx.Value("release").(OSRelease) + if r.ID == ReleaseUbuntu { + if strings.HasPrefix(r.VersionID, "12.") { + mkfsExt4Opts = append(mkfsExt4Opts, "-O", "^has_journal") + } + } logrus.Infof("mounting raw image") o, _, err := exec.RunOut(ctx, "losetup", "--show", "-f", b.diskRaw) if err != nil { @@ -315,7 +322,7 @@ func (b *builder) mountImg(ctx context.Context) error { b.rootPart = "/dev/mapper/root" b.mappedCryptRoot = filepath.Join("/dev/mapper", b.cryptRoot) logrus.Infof("creating raw image file system") - if err := exec.Run(ctx, "mkfs.ext4", b.mappedCryptRoot); err != nil { + if err := exec.Run(ctx, "mkfs.ext4", append(mkfsExt4Opts, b.mappedCryptRoot)...); err != nil { return err } if err := exec.Run(ctx, "mount", b.mappedCryptRoot, b.mntPoint); err != nil { @@ -323,7 +330,7 @@ func (b *builder) mountImg(ctx context.Context) error { } } else { logrus.Infof("creating raw image file system") - if err := exec.Run(ctx, "mkfs.ext4", b.rootPart); err != nil { + if err := exec.Run(ctx, "mkfs.ext4", append(mkfsExt4Opts, b.rootPart)...); err != nil { return err } if err := exec.Run(ctx, "mount", b.rootPart, b.mntPoint); err != nil { @@ -339,7 +346,7 @@ func (b *builder) mountImg(ctx context.Context) error { if b.bootFS.IsFat() { err = exec.Run(ctx, "mkfs.fat", "-F32", b.bootPart) } else { - err = exec.Run(ctx, "mkfs.ext4", b.bootPart) + err = exec.Run(ctx, "mkfs.ext4", append(mkfsExt4Opts, b.bootPart)...) } if err != nil { return err diff --git a/convert.go b/convert.go index 769de35..7de1ae4 100644 --- a/convert.go +++ b/convert.go @@ -45,7 +45,7 @@ func Convert(ctx context.Context, img string, opts ...ConvertOption) error { if err != nil { return err } - + ctx = context.WithValue(ctx, "release", r) if o.luksPassword != "" && !r.SupportsLUKS() { return fmt.Errorf("luks is not supported for %s %s", r.Name, r.Version) } diff --git a/dockerfile.go b/dockerfile.go index 2ffd251..a79e20e 100644 --- a/dockerfile.go +++ b/dockerfile.go @@ -19,6 +19,7 @@ import ( "fmt" "io" "strconv" + "strings" "text/template" "github.com/sirupsen/logrus" @@ -27,6 +28,9 @@ import ( //go:embed templates/ubuntu.Dockerfile var ubuntuDockerfile string +//go:embed templates/ubuntu12.Dockerfile +var ubuntu12Dockerfile string + //go:embed templates/debian.Dockerfile var debianDockerfile string @@ -37,10 +41,11 @@ var alpineDockerfile string var centOSDockerfile string var ( - ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Funcs(tplFuncs).Parse(ubuntuDockerfile)) - debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Funcs(tplFuncs).Parse(debianDockerfile)) - alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Funcs(tplFuncs).Parse(alpineDockerfile)) - centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Funcs(tplFuncs).Parse(centOSDockerfile)) + ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Funcs(tplFuncs).Parse(ubuntuDockerfile)) + ubuntu12DockerfileTemplate = template.Must(template.New("ubuntu12.Dockerfile").Funcs(tplFuncs).Parse(ubuntu12Dockerfile)) + debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Funcs(tplFuncs).Parse(debianDockerfile)) + alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Funcs(tplFuncs).Parse(alpineDockerfile)) + centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Funcs(tplFuncs).Parse(centOSDockerfile)) ) type NetworkManager string @@ -90,7 +95,11 @@ func NewDockerfile(release OSRelease, img, password string, networkManager Netwo d.tmpl = debianDockerfileTemplate net = NetworkManagerIfupdown2 case ReleaseUbuntu: - d.tmpl = ubuntuDockerfileTemplate + if strings.HasPrefix(release.VersionID, "12.") { + d.tmpl = ubuntu12DockerfileTemplate + } else { + d.tmpl = ubuntuDockerfileTemplate + } if release.VersionID < "18.04" { net = NetworkManagerIfupdown2 } else { diff --git a/templates/ubuntu12.Dockerfile b/templates/ubuntu12.Dockerfile new file mode 100644 index 0000000..dea0aa9 --- /dev/null +++ b/templates/ubuntu12.Dockerfile @@ -0,0 +1,67 @@ +FROM {{ .Image }} + +USER root + +RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ + apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ + linux-image-virtual \ + initramfs-tools \ +{{- if .Grub }} + grub-common \ + grub2-common \ +{{- end }} +{{- if .GrubBIOS }} + grub-pc-bin \ +{{- end }} +{{- if .GrubEFI }} + grub-efi-${ARCH}-bin \ +{{- end }} + dbus \ + isc-dhcp-client \ + iputils-ping && \ + find /boot -type l -exec rm {} \; + +{{ if gt .Release.VersionID "16.04" }} +RUN systemctl preset-all +{{ end }} + +{{ if .Password }}RUN echo "root:{{ .Password }}" | chpasswd {{ end }} + +{{ if eq .NetworkManager "netplan" }} +RUN apt install -y netplan.io +RUN mkdir -p /etc/netplan && printf '\ +network:\n\ + version: 2\n\ + renderer: networkd\n\ + ethernets:\n\ + eth0:\n\ + dhcp4: true\n\ + dhcp-identifier: mac\n\ + nameservers:\n\ + addresses:\n\ + - 8.8.8.8\n\ + - 8.8.4.4\n\ +' > /etc/netplan/00-netcfg.yaml +{{ else if eq .NetworkManager "ifupdown"}} +RUN if [ -z "$(apt-cache madison ifupdown-ng 2> /dev/nul)" ]; then apt-get install -y ifupdown; else apt-get install -y ifupdown-ng; fi +RUN mkdir -p /etc/network && printf '\ +auto eth0\n\ +allow-hotplug eth0\n\ +iface eth0 inet dhcp\n\ +' > /etc/network/interfaces +{{ end }} + +{{- if .Luks }} +RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends cryptsetup-initramfs && \ + update-initramfs -u -v +{{- end }} + +# needs to be after update-initramfs +{{- if not .Grub }} +RUN mv $(find /boot -name 'vmlinuz-*') /boot/vmlinuz && \ + mv $(find /boot -name 'initrd.img-*') /boot/initrd.img +{{- end }} + +RUN apt-get clean && \ + rm -rf /var/lib/apt/lists/* From a784b433ca3f0cdb3b7c40e1c417be45c4049a3f Mon Sep 17 00:00:00 2001 From: LEI WANG Date: Tue, 27 Aug 2024 18:37:10 +0800 Subject: [PATCH 2/4] restore initctl, setup ttyS0, mkfs.ext4 -O Signed-off-by: LEI WANG --- builder.go | 2 +- templates/ubuntu12.Dockerfile | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/builder.go b/builder.go index 11edf44..942b7df 100644 --- a/builder.go +++ b/builder.go @@ -284,7 +284,7 @@ func (b *builder) mountImg(ctx context.Context) error { r := ctx.Value("release").(OSRelease) if r.ID == ReleaseUbuntu { if strings.HasPrefix(r.VersionID, "12.") { - mkfsExt4Opts = append(mkfsExt4Opts, "-O", "^has_journal") + mkfsExt4Opts = append(mkfsExt4Opts, "-O", "^has_journal,^metadata_csum") } } logrus.Infof("mounting raw image") diff --git a/templates/ubuntu12.Dockerfile b/templates/ubuntu12.Dockerfile index dea0aa9..528fce4 100644 --- a/templates/ubuntu12.Dockerfile +++ b/templates/ubuntu12.Dockerfile @@ -2,6 +2,12 @@ FROM {{ .Image }} USER root +# restore initctl +RUN rm /sbin/initctl && dpkg-divert --rename --remove /sbin/initctl + +# setup ttyS0 +RUN cp /etc/init/tty1.conf /etc/init/ttyS0.conf && sed -i s/tty1/ttyS0/g /etc/init/ttyS0.conf + RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ From 0f86904ddcba27bbb8d8cfc44bcc218057958367 Mon Sep 17 00:00:00 2001 From: LEI WANG Date: Thu, 29 Aug 2024 17:29:58 +0800 Subject: [PATCH 3/4] merge dockerfiles Signed-off-by: LEI WANG --- builder.go | 3 ++ dockerfile.go | 19 +++------ templates/ubuntu.Dockerfile | 26 ++++++++++--- templates/ubuntu12.Dockerfile | 73 ----------------------------------- 4 files changed, 29 insertions(+), 92 deletions(-) delete mode 100644 templates/ubuntu12.Dockerfile diff --git a/builder.go b/builder.go index 942b7df..b452d4e 100644 --- a/builder.go +++ b/builder.go @@ -286,6 +286,9 @@ func (b *builder) mountImg(ctx context.Context) error { if strings.HasPrefix(r.VersionID, "12.") { mkfsExt4Opts = append(mkfsExt4Opts, "-O", "^has_journal,^metadata_csum") } + if strings.HasPrefix(r.VersionID, "14.") { + mkfsExt4Opts = append(mkfsExt4Opts, "-O", "^metadata_csum") + } } logrus.Infof("mounting raw image") o, _, err := exec.RunOut(ctx, "losetup", "--show", "-f", b.diskRaw) diff --git a/dockerfile.go b/dockerfile.go index a79e20e..2ffd251 100644 --- a/dockerfile.go +++ b/dockerfile.go @@ -19,7 +19,6 @@ import ( "fmt" "io" "strconv" - "strings" "text/template" "github.com/sirupsen/logrus" @@ -28,9 +27,6 @@ import ( //go:embed templates/ubuntu.Dockerfile var ubuntuDockerfile string -//go:embed templates/ubuntu12.Dockerfile -var ubuntu12Dockerfile string - //go:embed templates/debian.Dockerfile var debianDockerfile string @@ -41,11 +37,10 @@ var alpineDockerfile string var centOSDockerfile string var ( - ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Funcs(tplFuncs).Parse(ubuntuDockerfile)) - ubuntu12DockerfileTemplate = template.Must(template.New("ubuntu12.Dockerfile").Funcs(tplFuncs).Parse(ubuntu12Dockerfile)) - debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Funcs(tplFuncs).Parse(debianDockerfile)) - alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Funcs(tplFuncs).Parse(alpineDockerfile)) - centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Funcs(tplFuncs).Parse(centOSDockerfile)) + ubuntuDockerfileTemplate = template.Must(template.New("ubuntu.Dockerfile").Funcs(tplFuncs).Parse(ubuntuDockerfile)) + debianDockerfileTemplate = template.Must(template.New("debian.Dockerfile").Funcs(tplFuncs).Parse(debianDockerfile)) + alpineDockerfileTemplate = template.Must(template.New("alpine.Dockerfile").Funcs(tplFuncs).Parse(alpineDockerfile)) + centOSDockerfileTemplate = template.Must(template.New("centos.Dockerfile").Funcs(tplFuncs).Parse(centOSDockerfile)) ) type NetworkManager string @@ -95,11 +90,7 @@ func NewDockerfile(release OSRelease, img, password string, networkManager Netwo d.tmpl = debianDockerfileTemplate net = NetworkManagerIfupdown2 case ReleaseUbuntu: - if strings.HasPrefix(release.VersionID, "12.") { - d.tmpl = ubuntu12DockerfileTemplate - } else { - d.tmpl = ubuntuDockerfileTemplate - } + d.tmpl = ubuntuDockerfileTemplate if release.VersionID < "18.04" { net = NetworkManagerIfupdown2 } else { diff --git a/templates/ubuntu.Dockerfile b/templates/ubuntu.Dockerfile index 1589379..d634455 100644 --- a/templates/ubuntu.Dockerfile +++ b/templates/ubuntu.Dockerfile @@ -2,13 +2,18 @@ FROM {{ .Image }} USER root +{{ if le .Release.VersionID "14.04" }} +# restore initctl +RUN rm /sbin/initctl && dpkg-divert --rename --remove /sbin/initctl +# setup ttyS0 +RUN cp /etc/init/tty1.conf /etc/init/ttyS0.conf && sed -i s/tty1/ttyS0/g /etc/init/ttyS0.conf +{{ end }} + RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ linux-image-virtual \ initramfs-tools \ - systemd-sysv \ - systemd \ {{- if .Grub }} grub-common \ grub2-common \ @@ -21,18 +26,29 @@ RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ {{- end }} dbus \ isc-dhcp-client \ - iproute2 \ iputils-ping && \ find /boot -type l -exec rm {} \; +{{ if gt .Release.VersionID "14.04" }} +RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ + apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ + iproute2 \ +{{ end }} + {{ if gt .Release.VersionID "16.04" }} +RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ + apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ + systemd-sysv \ + systemd RUN systemctl preset-all {{ end }} {{ if .Password }}RUN echo "root:{{ .Password }}" | chpasswd {{ end }} {{ if eq .NetworkManager "netplan" }} -RUN apt install -y netplan.io +RUN apt-get install -y netplan.io RUN mkdir -p /etc/netplan && printf '\ network:\n\ version: 2\n\ @@ -47,7 +63,7 @@ network:\n\ - 8.8.4.4\n\ ' > /etc/netplan/00-netcfg.yaml {{ else if eq .NetworkManager "ifupdown"}} -RUN if [ -z "$(apt-cache madison ifupdown-ng 2> /dev/nul)" ]; then apt install -y ifupdown; else apt install -y ifupdown-ng; fi +RUN if [ -z "$(apt-cache madison ifupdown-ng 2> /dev/nul)" ]; then apt-get install -y ifupdown; else apt-get install -y ifupdown-ng; fi RUN mkdir -p /etc/network && printf '\ auto eth0\n\ allow-hotplug eth0\n\ diff --git a/templates/ubuntu12.Dockerfile b/templates/ubuntu12.Dockerfile deleted file mode 100644 index 528fce4..0000000 --- a/templates/ubuntu12.Dockerfile +++ /dev/null @@ -1,73 +0,0 @@ -FROM {{ .Image }} - -USER root - -# restore initctl -RUN rm /sbin/initctl && dpkg-divert --rename --remove /sbin/initctl - -# setup ttyS0 -RUN cp /etc/init/tty1.conf /etc/init/ttyS0.conf && sed -i s/tty1/ttyS0/g /etc/init/ttyS0.conf - -RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ - apt-get update && \ - DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ - linux-image-virtual \ - initramfs-tools \ -{{- if .Grub }} - grub-common \ - grub2-common \ -{{- end }} -{{- if .GrubBIOS }} - grub-pc-bin \ -{{- end }} -{{- if .GrubEFI }} - grub-efi-${ARCH}-bin \ -{{- end }} - dbus \ - isc-dhcp-client \ - iputils-ping && \ - find /boot -type l -exec rm {} \; - -{{ if gt .Release.VersionID "16.04" }} -RUN systemctl preset-all -{{ end }} - -{{ if .Password }}RUN echo "root:{{ .Password }}" | chpasswd {{ end }} - -{{ if eq .NetworkManager "netplan" }} -RUN apt install -y netplan.io -RUN mkdir -p /etc/netplan && printf '\ -network:\n\ - version: 2\n\ - renderer: networkd\n\ - ethernets:\n\ - eth0:\n\ - dhcp4: true\n\ - dhcp-identifier: mac\n\ - nameservers:\n\ - addresses:\n\ - - 8.8.8.8\n\ - - 8.8.4.4\n\ -' > /etc/netplan/00-netcfg.yaml -{{ else if eq .NetworkManager "ifupdown"}} -RUN if [ -z "$(apt-cache madison ifupdown-ng 2> /dev/nul)" ]; then apt-get install -y ifupdown; else apt-get install -y ifupdown-ng; fi -RUN mkdir -p /etc/network && printf '\ -auto eth0\n\ -allow-hotplug eth0\n\ -iface eth0 inet dhcp\n\ -' > /etc/network/interfaces -{{ end }} - -{{- if .Luks }} -RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends cryptsetup-initramfs && \ - update-initramfs -u -v -{{- end }} - -# needs to be after update-initramfs -{{- if not .Grub }} -RUN mv $(find /boot -name 'vmlinuz-*') /boot/vmlinuz && \ - mv $(find /boot -name 'initrd.img-*') /boot/initrd.img -{{- end }} - -RUN apt-get clean && \ - rm -rf /var/lib/apt/lists/* From b575dbc89ee082d12d2995a61e27a4a8598a336a Mon Sep 17 00:00:00 2001 From: LEI WANG Date: Thu, 29 Aug 2024 17:37:01 +0800 Subject: [PATCH 4/4] control packages by release version Signed-off-by: LEI WANG --- templates/ubuntu.Dockerfile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/templates/ubuntu.Dockerfile b/templates/ubuntu.Dockerfile index d634455..553aacf 100644 --- a/templates/ubuntu.Dockerfile +++ b/templates/ubuntu.Dockerfile @@ -29,19 +29,22 @@ RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ iputils-ping && \ find /boot -type l -exec rm {} \; -{{ if gt .Release.VersionID "14.04" }} +{{ if ge .Release.VersionID "14.04" }} RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ - iproute2 \ + iproute2 {{ end }} -{{ if gt .Release.VersionID "16.04" }} +{{ if ge .Release.VersionID "16.04" }} RUN ARCH="$([ "$(uname -m)" = "x86_64" ] && echo amd64 || echo arm64)"; \ apt-get update && \ DEBIAN_FRONTEND=noninteractive apt-get -y install --no-install-recommends \ systemd-sysv \ systemd +{{ end }} + +{{ if gt .Release.VersionID "16.04" }} RUN systemctl preset-all {{ end }}