github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/pkg/infra/container/client/docker/utils.go (about)

     1  // Copyright © 2021 Alibaba Group Holding Ltd.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package docker
    16  
    17  import (
    18  	"crypto/sha1" // #nosec
    19  	"encoding/binary"
    20  	"net"
    21  
    22  	"github.com/docker/docker/api/types/mount"
    23  )
    24  
    25  func DefaultMounts() []mount.Mount {
    26  	mounts := []mount.Mount{
    27  		{
    28  			Type:     mount.TypeBind,
    29  			Source:   "/lib/modules",
    30  			Target:   "/lib/modules",
    31  			ReadOnly: true,
    32  			BindOptions: &mount.BindOptions{
    33  				Propagation: mount.PropagationRPrivate,
    34  			},
    35  		},
    36  		{
    37  			Type:     mount.TypeVolume,
    38  			Source:   "",
    39  			Target:   "/var",
    40  			ReadOnly: false,
    41  			VolumeOptions: &mount.VolumeOptions{
    42  				DriverConfig: &mount.Driver{
    43  					Name: "local",
    44  				},
    45  			},
    46  		},
    47  		{
    48  			Type:     mount.TypeTmpfs,
    49  			Source:   "",
    50  			Target:   "/tmp",
    51  			ReadOnly: false,
    52  		},
    53  		{
    54  			Type:     mount.TypeTmpfs,
    55  			Source:   "",
    56  			Target:   "/run",
    57  			ReadOnly: false,
    58  		},
    59  	}
    60  	return mounts
    61  }
    62  
    63  func GenerateSubnetFromName(name string, attempt int32) string {
    64  	ip := make([]byte, 16)
    65  	ip[0] = 0xfc
    66  	ip[1] = 0x00
    67  	h := sha1.New() // #nosec
    68  	_, _ = h.Write([]byte(name))
    69  	_ = binary.Write(h, binary.LittleEndian, attempt)
    70  	bs := h.Sum(nil)
    71  	for i := 2; i < 8; i++ {
    72  		ip[i] = bs[i]
    73  	}
    74  	subnet := &net.IPNet{
    75  		IP:   net.IP(ip),
    76  		Mask: net.CIDRMask(64, 128),
    77  	}
    78  	return subnet.String()
    79  }