2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-29 19:16:24 +00:00

add "append-to-cmdline" option

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-08-07 18:24:02 +02:00
parent 4ffb202d33
commit be1e8cd000
Signed by: adphi
GPG Key ID: F2159213400E50AB
4 changed files with 28 additions and 23 deletions

View File

@ -44,25 +44,25 @@ ff02::3 ip6-allhosts
SAY Now booting the kernel from SYSLINUX... SAY Now booting the kernel from SYSLINUX...
LABEL linux LABEL linux
KERNEL /boot/vmlinuz KERNEL /boot/vmlinuz
APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 %s
` `
syslinuxCfgDebian = `DEFAULT linux syslinuxCfgDebian = `DEFAULT linux
SAY Now booting the kernel from SYSLINUX... SAY Now booting the kernel from SYSLINUX...
LABEL linux LABEL linux
KERNEL /vmlinuz KERNEL /vmlinuz
APPEND ro root=UUID=%s initrd=/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 APPEND ro root=UUID=%s initrd=/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 %s
` `
syslinuxCfgAlpine = `DEFAULT linux syslinuxCfgAlpine = `DEFAULT linux
SAY Now booting the kernel from SYSLINUX... SAY Now booting the kernel from SYSLINUX...
LABEL linux LABEL linux
KERNEL /boot/vmlinuz-virt KERNEL /boot/vmlinuz-virt
APPEND ro root=UUID=%s rootfstype=ext4 initrd=/boot/initramfs-virt console=ttyS0,115200 APPEND ro root=UUID=%s rootfstype=ext4 initrd=/boot/initramfs-virt console=ttyS0,115200 %s
` `
syslinuxCfgCentOS = `DEFAULT linux syslinuxCfgCentOS = `DEFAULT linux
SAY Now booting the kernel from SYSLINUX... SAY Now booting the kernel from SYSLINUX...
LABEL linux LABEL linux
KERNEL /boot/vmlinuz KERNEL /boot/vmlinuz
APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 APPEND ro root=UUID=%s initrd=/boot/initrd.img net.ifnames=0 console=tty0 console=ttyS0,115200n8 %s
` `
) )
@ -119,12 +119,13 @@ type builder struct {
mbrPath string mbrPath string
loDevice string loDevice string
loPart string loPart string
diskUUD string diskUUD string
cmdLineExtra string
} }
func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size int64, osRelease OSRelease, format string) (*builder, error) { func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size int64, osRelease OSRelease, format string, cmdLineExtra string) (*builder, error) {
if err := checkDependencies(); err != nil { if err := checkDependencies(); err != nil {
return nil, err return nil, err
} }
@ -169,14 +170,15 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size int64, o
// size = int64(s) // size = int64(s)
// } // }
b := &builder{ b := &builder{
osRelease: osRelease, osRelease: osRelease,
img: img, img: img,
diskRaw: filepath.Join(workdir, disk+".d2vm.raw"), diskRaw: filepath.Join(workdir, disk+".d2vm.raw"),
diskOut: filepath.Join(workdir, disk+"."+format), diskOut: filepath.Join(workdir, disk+"."+format),
format: f, format: f,
size: size, size: size,
mbrPath: mbrBin, mbrPath: mbrBin,
mntPoint: filepath.Join(workdir, "/mnt"), mntPoint: filepath.Join(workdir, "/mnt"),
cmdLineExtra: cmdLineExtra,
} }
if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil { if err := os.MkdirAll(b.mntPoint, os.ModePerm); err != nil {
return nil, err return nil, err
@ -343,7 +345,7 @@ func (b *builder) installKernel(ctx context.Context) error {
if err != nil { if err != nil {
return err return err
} }
if err := b.chWriteFile("/boot/syslinux.cfg", fmt.Sprintf(sysconfig, b.diskUUD), perm); err != nil { if err := b.chWriteFile("/boot/syslinux.cfg", fmt.Sprintf(sysconfig, b.diskUUD, b.cmdLineExtra), perm); err != nil {
return err return err
} }
return nil return nil

View File

@ -47,14 +47,14 @@ var (
return err return err
} }
exec.SetDebug(debug) exec.SetDebug(debug)
logrus.Infof("building docker image from %s", file)
if file == "" { if file == "" {
file = filepath.Join(args[0], "Dockerfile") file = filepath.Join(args[0], "Dockerfile")
} }
logrus.Infof("building docker image from %s", file)
if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil { if err := docker.Build(cmd.Context(), tag, file, args[0], buildArgs...); err != nil {
return err return err
} }
return d2vm.Convert(cmd.Context(), tag, size, password, output) return d2vm.Convert(cmd.Context(), tag, size, password, output, cmdLineExtra)
}, },
} }
) )
@ -70,4 +70,5 @@ func init() {
buildCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size") buildCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
buildCmd.Flags().BoolVar(&force, "force", false, "Override output image") buildCmd.Flags().BoolVar(&force, "force", false, "Override output image")
buildCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
} }

View File

@ -30,7 +30,8 @@ import (
) )
var ( var (
pull = false pull = false
cmdLineExtra = ""
convertCmd = &cobra.Command{ convertCmd = &cobra.Command{
Use: "convert [docker image]", Use: "convert [docker image]",
@ -78,7 +79,7 @@ var (
return err return err
} }
} }
return d2vm.Convert(cmd.Context(), img, size, password, output) return d2vm.Convert(cmd.Context(), img, size, password, output, cmdLineExtra)
}, },
} }
) )
@ -98,5 +99,6 @@ func init() {
convertCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size") convertCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size")
convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output")
convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image") convertCmd.Flags().BoolVarP(&force, "force", "f", false, "Override output qcow2 image")
convertCmd.Flags().StringVar(&cmdLineExtra, "append-to-cmdline", "", "Extra kernel cmdline arguments to append to the generated one")
rootCmd.AddCommand(convertCmd) rootCmd.AddCommand(convertCmd)
} }

View File

@ -28,7 +28,7 @@ import (
"go.linka.cloud/d2vm/pkg/docker" "go.linka.cloud/d2vm/pkg/docker"
) )
func Convert(ctx context.Context, img string, size int64, password string, output string) error { func Convert(ctx context.Context, img string, size int64, password string, output string, cmdLineExtra string) error {
imgUUID := uuid.New().String() imgUUID := uuid.New().String()
tmpPath := filepath.Join(os.TempDir(), "d2vm", imgUUID) tmpPath := filepath.Join(os.TempDir(), "d2vm", imgUUID)
if err := os.MkdirAll(tmpPath, os.ModePerm); err != nil { if err := os.MkdirAll(tmpPath, os.ModePerm); err != nil {
@ -67,7 +67,7 @@ func Convert(ctx context.Context, img string, size int64, password string, outpu
if format == "" { if format == "" {
format = "raw" format = "raw"
} }
b, err := NewBuilder(ctx, tmpPath, imgUUID, "", size, r, format) b, err := NewBuilder(ctx, tmpPath, imgUUID, "", size, r, format, cmdLineExtra)
if err != nil { if err != nil {
return err return err
} }