Skip to content

Instantly share code, notes, and snippets.

@mattsmithdatera
Last active November 9, 2018 22:16
Show Gist options
  • Save mattsmithdatera/2c1316eda7c9c63a7be406a26f93ed08 to your computer and use it in GitHub Desktop.
Save mattsmithdatera/2c1316eda7c9c63a7be406a26f93ed08 to your computer and use it in GitHub Desktop.
Iscsi in a container, communicating via RPC
syntax = "proto3";
package iscsi_rpc;
service Iscsiadm {
rpc SendArgs(SendArgsRequest) returns (SendArgsReply) {}
rpc GetInitiatorName(GetInitiatorNameRequest) returns (GetInitiatorNameReply) {}
}
message SendArgsRequest {
string args = 1;
}
message SendArgsReply {
string result = 1;
}
message GetInitiatorNameRequest {
}
message GetInitiatorNameReply {
string name = 1;
}
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: iscsi
spec:
updateStrategy:
type: OnDelete
selector:
matchLabels:
app: iscsi
template:
metadata:
labels:
app: iscsi
spec:
hostNetwork: true
containers:
- name: iscsid
imagePullPolicy: Always
image: dateraiodev/iscsi:latest
command: [ "/bin/bash", "-c", "iscsi-recv &2>1 & iscsid-run.sh" ]
securityContext:
privileged: true
volumeMounts:
- name: devices
mountPath: /dev
- name: iscsi-socket
mountPath: /iscsi-socket
- name: libmod
mountPath: /lib/modules
volumes:
- name: devices
hostPath:
path: /dev
- name: iscsi-socket
hostPath:
path: /var/lib/iscsi-socket
type: DirectoryOrCreate
- name: libmod
hostPath:
path: /lib/modules
package main
import (
"context"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
co "common"
pb "iscsi-rpc"
"google.golang.org/grpc"
)
const (
address = "unix:///var/lib/iscsi-socket/iscsi.sock"
etcIscsi = "/etc/iscsi"
iname = "/etc/iscsi/initiatorname.iscsi"
)
var (
// Necessary to prevent UDC arguments from showing up
cli = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
addr = cli.String("addr", address, "Address to send on")
args = cli.String("args", "", "Arguments including iscsiadm prefix")
)
func setupInitName(ctxt context.Context, c pb.IscsiadmClient) {
if _, err := os.Stat(etcIscsi); os.IsNotExist(err) {
log.Printf("Creating directories: %s\n", addr)
err = os.MkdirAll(etcIscsi, os.ModePerm)
if err != nil {
log.Fatal(err)
}
resp, err := c.GetInitiatorName(ctxt, &pb.GetInitiatorNameRequest{})
if err != nil {
log.Fatal(err)
}
name := fmt.Sprintf("InitiatorName=%s", resp.Name)
err = ioutil.WriteFile(iname, []byte(name), 0644)
if err != nil {
log.Fatal(err)
}
}
}
func main() {
cli.Parse(os.Args[1:])
// Set up a connection to the server.
conn, err := grpc.Dial(*addr, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewIscsiadmClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
setupInitName(ctx, c)
r, err := c.SendArgs(ctx, &pb.SendArgsRequest{Args: *args})
if err != nil {
log.Fatalf("Could not send args: %v", err)
}
log.Printf("Iscsiadm Result: %s", r.Result)
}
@mattsmithdatera
Copy link
Author

mattsmithdatera commented Oct 29, 2018

Forgot to add the dockerfile:

FROM ubuntu:16.04

RUN apt-get update \
 && apt-get -y install open-iscsi \
                       multipath-tools \
 && echo GenerateName=yes > /etc/iscsi/initiatorname.iscsi

COPY assets/multipath.conf /etc/multipath.conf
COPY cmd/dat-csi-plugin/iscsi-recv /usr/local/bin/
COPY deploy/kubernetes/iscsi/iscsid-run.sh /usr/local/bin/

iscsid-recv is just a simple golang file serving the protofile GRPC interface

@mattsmithdatera
Copy link
Author

Multipathd is also enabled in this container image, but it currently blacklists all but Datera devices. Override the multipath.conf file with whatever you want to get non Datera devices working.

iscsid-run.sh:

#!/usr/bin/env bash
set -e
modprobe dm-multipath
service multipath-tools start
/lib/open-iscsi/startup-checks.sh
exec /usr/sbin/iscsid -f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment