Skip to content
This repository was archived by the owner on Oct 11, 2023. It is now read-only.

Commit cd35087

Browse files
committed
port the use of parted to go
Signed-off-by: Sven Dowideit <[email protected]>
1 parent 5331523 commit cd35087

File tree

2 files changed

+90
-51
lines changed

2 files changed

+90
-51
lines changed

cmd/control/install.go

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ var installCommand = cli.Command{
4646
},
4747
cli.StringFlag{
4848
Name: "install-type, t",
49-
Usage: `generic: (Default) Creates 1 ext4 partition and installs RancherOS
49+
Usage: `generic: (Default) Creates 1 ext4 partition and installs RancherOS (syslinux)
5050
amazon-ebs: Installs RancherOS and sets up PV-GRUB
51-
syslinux: partition and format disk (mbr), then install RnancherOS and setup Syslinux
5251
gptsyslinux: partition and format disk (gpt), then install RnancherOS and setup Syslinux
5352
`,
5453
},
@@ -137,19 +136,18 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force,
137136
diskType = "gpt"
138137
}
139138

140-
if installType == "generic" ||
141-
installType == "syslinux" ||
142-
installType == "gptsyslinux" {
143-
144139
// Versions before 0.8.0-rc2 use the old calling convention (from the lay-down-os shell script)
145140
imageVersion := strings.TrimPrefix(image, "rancher/os:v")
146141
if image != imageVersion {
142+
log.Infof("user spcified different install image: %s != %s", image, imageVersion)
147143
imageVersion = strings.Replace(imageVersion, "-", ".", -1)
148144
vArray := strings.Split(imageVersion, ".")
149145
v, _ := strconv.ParseFloat(vArray[0]+"."+vArray[1], 32)
150146
if v < 0.8 || imageVersion == "0.8.0-rc1" {
151147
log.Infof("starting installer container for %s", image)
152-
if installType == "generic" {
148+
if installType == "generic" ||
149+
installType == "syslinux" ||
150+
installType == "gptsyslinux" {
153151
cmd := exec.Command("system-docker", "run", "--net=host", "--privileged", "--volumes-from=all-volumes",
154152
"--entrypoint=/scripts/set-disk-partitions", image, device, diskType)
155153
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
@@ -158,7 +156,8 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force,
158156
}
159157
}
160158
cmd := exec.Command("system-docker", "run", "--net=host", "--privileged", "--volumes-from=user-volumes",
161-
"--volumes-from=command-volumes", image, "-d", device, "-t", installType, "-c", cloudConfig, "-a", kappend)
159+
"--volumes-from=command-volumes", image, "-d", device, "-t", installType, "-c", cloudConfig,
160+
"-a", kappend)
162161
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
163162
if err := cmd.Run(); err != nil {
164163
return err
@@ -195,7 +194,7 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force,
195194
"run", "--rm", "--net=host", "--privileged",
196195
// bind mount host fs to access its ros, vmlinuz, initrd and /dev (udev isn't running in container)
197196
"-v", "/:/host",
198-
"--volumes-from=user-volumes", "--volumes-from=command-volumes",
197+
"--volumes-from=all-volumes",
199198
image,
200199
"install",
201200
"-t", installType,
@@ -239,8 +238,14 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force,
239238

240239
log.Infof("running installation")
241240

242-
if installType == "generic" {
243-
err := setDiskpartitions(device)
241+
if installType == "generic" ||
242+
installType == "syslinux" ||
243+
installType == "gptsyslinux" {
244+
diskType := "msdos"
245+
if installType == "gptsyslinux" {
246+
diskType = "gpt"
247+
}
248+
err := setDiskpartitions(device, diskType)
244249
if err != nil {
245250
log.Errorf("error setDiskpartitions %s", err)
246251
return err
@@ -271,12 +276,23 @@ func runInstall(image, installType, cloudConfig, device, kappend string, force,
271276
}
272277

273278
func mountBootIso() error {
274-
// TODO: need to add a label to the iso and mount using that.
275-
// ARGH! need to mount this in the host - or share it as a volume..
276-
os.MkdirAll("/bootiso", 0755)
277-
278279
deviceName := "/dev/sr0"
279280
deviceType := "iso9660"
281+
{ // force the defer
282+
mountsFile, err := os.Open("/proc/mounts")
283+
if err != nil {
284+
log.Errorf("failed to read /proc/mounts %s", err)
285+
return err
286+
}
287+
defer mountsFile.Close()
288+
289+
if partitionMounted(deviceName, mountsFile) {
290+
return nil
291+
}
292+
}
293+
294+
os.MkdirAll("/bootiso", 0755)
295+
280296
// find the installation device
281297
cmd := exec.Command("blkid", "-L", "RancherOS")
282298
log.Debugf("Run(%v)", cmd)
@@ -337,7 +353,16 @@ func layDownOS(image, installType, cloudConfig, device, kappend string) error {
337353
// unmount on trap
338354
defer util.Unmount(baseName)
339355

356+
diskType := "msdos"
357+
if installType == "gptsyslinux" {
358+
diskType = "gpt"
359+
}
360+
340361
switch installType {
362+
case "syslinux":
363+
fallthrough
364+
case "gptsyslinux":
365+
fallthrough
341366
case "generic":
342367
log.Debugf("formatAndMount")
343368
var err error
@@ -349,7 +374,7 @@ func layDownOS(image, installType, cloudConfig, device, kappend string) error {
349374
//log.Infof("installGrub")
350375
//err = installGrub(baseName, device)
351376
log.Debugf("installSyslinux")
352-
err = installSyslinux(device, baseName, bootDir)
377+
err = installSyslinux(device, baseName, bootDir, diskType)
353378

354379
if err != nil {
355380
log.Errorf("%s", err)
@@ -399,15 +424,15 @@ func layDownOS(image, installType, cloudConfig, device, kappend string) error {
399424
return err
400425
}
401426
createbootDirs(baseName, bootDir)
402-
installSyslinux(device, baseName, bootDir)
427+
installSyslinux(device, baseName, bootDir, diskType)
403428
case "raid":
404429
var err error
405430
bootDir, err = mountdevice(baseName, bootDir, partition, false)
406431
if err != nil {
407432
return err
408433
}
409434
createbootDirs(baseName, bootDir)
410-
installSyslinuxRaid(baseName, bootDir)
435+
installSyslinuxRaid(baseName, bootDir, diskType)
411436
case "bootstrap":
412437
CONSOLE = "ttyS0"
413438
var err error
@@ -496,7 +521,7 @@ func seedData(baseName, cloudData string, files []string) error {
496521
}
497522

498523
// set-disk-partitions is called with device == **/dev/sda**
499-
func setDiskpartitions(device string) error {
524+
func setDiskpartitions(device, diskType string) error {
500525
log.Debugf("setDiskpartitions")
501526

502527
d := strings.Split(device, "/")
@@ -590,23 +615,16 @@ func setDiskpartitions(device string) error {
590615
return err
591616
}
592617

593-
r, w := io.Pipe()
594-
go func() {
595-
w.Write([]byte(`n
596-
p
597-
1
598-
599-
600-
a
601-
1
602-
w
603-
`))
604-
w.Close()
605-
}()
606-
log.Debugf("running fdisk")
607-
cmd = exec.Command("fdisk", device)
608-
cmd.Stdin = r
609-
//cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
618+
bootflag := "boot"
619+
if diskType == "gpt" {
620+
bootflag = "legacy_boot"
621+
}
622+
log.Debugf("running parted")
623+
cmd = exec.Command("parted", "-s", "-a", "optimal", device,
624+
"mklabel "+diskType, "--",
625+
"mkpart primary ext4 1 -1",
626+
"set 1 "+bootflag+" on")
627+
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
610628
if err := cmd.Run(); err != nil {
611629
log.Errorf("%s", err)
612630
return err
@@ -714,13 +732,18 @@ func createbootDirs(baseName, bootDir string) error {
714732
return nil
715733
}
716734

717-
func installSyslinux(device, baseName, bootDir string) error {
735+
func installSyslinux(device, baseName, bootDir, diskType string) error {
718736
log.Debugf("installSyslinux")
719737

738+
mbrFile := "mbr.bin"
739+
if diskType == "gpt" {
740+
mbrFile = "gptmbr.bin"
741+
}
742+
720743
//dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=${device}
721744
// ubuntu: /usr/lib/syslinux/mbr/mbr.bin
722745
// alpine: /usr/share/syslinux/mbr.bin
723-
cmd := exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/mbr.bin", "of="+device)
746+
cmd := exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/"+mbrFile, "of="+device)
724747
//cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
725748
log.Debugf("Run(%v)", cmd)
726749
if err := cmd.Run(); err != nil {
@@ -750,19 +773,24 @@ func installSyslinux(device, baseName, bootDir string) error {
750773
return nil
751774
}
752775

753-
func installSyslinuxRaid(baseName, bootDir string) error {
776+
func installSyslinuxRaid(baseName, bootDir, diskType string) error {
754777
log.Debugf("installSyslinuxRaid")
755778

779+
mbrFile := "mbr.bin"
780+
if diskType == "gpt" {
781+
mbrFile = "gptmbr.bin"
782+
}
783+
756784
//dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sda
757785
//dd bs=440 count=1 if=/usr/lib/syslinux/mbr/mbr.bin of=/dev/sdb
758786
//cp /usr/lib/syslinux/modules/bios/* ${baseName}/${bootDir}syslinux
759787
//extlinux --install --raid ${baseName}/${bootDir}syslinux
760-
cmd := exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/mbr.bin", "of=/dev/sda")
788+
cmd := exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/"+mbrFile, "of=/dev/sda")
761789
if err := cmd.Run(); err != nil {
762790
log.Errorf("%s", err)
763791
return err
764792
}
765-
cmd = exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/mbr.bin", "of=/dev/sdb")
793+
cmd = exec.Command("dd", "bs=440", "count=1", "if=/usr/share/syslinux/"+mbrFile, "of=/dev/sdb")
766794
if err := cmd.Run(); err != nil {
767795
log.Errorf("%s", err)
768796
return err

scripts/run

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ while [ "$#" -gt 0 ]; do
1414
QIND=0
1515
REBUILD=0
1616
;;
17+
--boothd)
18+
BOOT_HD=1
19+
QEMU=0
20+
QIND=0
21+
REBUILD=0
22+
;;
1723
--append)
1824
shift 1
1925
QEMU_APPEND="${QEMU_APPEND} $1"
@@ -100,7 +106,7 @@ if [ "$KVM" == "" ] && [ -c /dev/kvm ] && [ -r /dev/kvm ] && [ -w /dev/kvm ]; th
100106
KVM=1
101107
fi
102108

103-
if [ "$QEMU" == "1" ] || [ "$BOOT_ISO" == "1" ]; then
109+
if [ "$QEMU" == "1" ] || [ "$BOOT_ISO" == "1" ] || [ "$BOOT_HD" == "1" ]; then
104110
HD=${BASE}/state/hd.img
105111
HD2=${BASE}/state/hd2.img
106112
[ "$FRESH" == "1" ] && rm -f ${HD} ${HD2} >/dev/null 2>&1 || :
@@ -118,14 +124,15 @@ if [ "$QEMU" == "1" ] || [ "$BOOT_ISO" == "1" ]; then
118124
qemu-img create -f qcow2 -o size=10G ${HD2}
119125
SECOND_DRIVE_ENABLE=$(eval "${hd["$ARCH"]} ${HD2}")
120126
fi
127+
fi
128+
129+
if [ "$QIND" != "1" ]; then
121130
CPU=${cpu["$ARCH"]}
122131
if [ "$KVM" == "1" ] && [ "$ARCH" == "$HOST_ARCH" ]; then
123132
KVM_ENABLE="-enable-kvm"
124133
CPU="-cpu host"
125134
fi
126-
fi
127135

128-
if [ "$QIND" != "1" ]; then
129136
CCROOT=${BUILD}/cloud-config
130137
rm -rf ${CCROOT}
131138
mkdir -p ${CCROOT}
@@ -175,7 +182,16 @@ if [ "$QEMU" == "1" ]; then
175182
${QEMU_ARGS} \
176183
"${@}"
177184

178-
elif [ "$BOOT_ISO" == "1" ]; then
185+
elif [ "$BOOT_ISO" == "1" ] ||
186+
[ "$BOOT_HD" == "1" ]; then
187+
if [ "$BOOT_ISO" == "1" ]; then
188+
ISO_OPTS="-boot d -cdrom ./dist/artifacts/rancheros.iso \
189+
-fsdev local,security_model=passthrough,readonly,id=fsdev0,path=${CCROOT} \
190+
-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2 \
191+
-fsdev local,security_model=none,id=fsdev1,path=${HOME} \
192+
-device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home "
193+
echo "----- $ISO_OPTS"
194+
fi
179195
set -x
180196
exec qemu-system-${QEMUARCH} \
181197
-curses \
@@ -188,12 +204,7 @@ elif [ "$BOOT_ISO" == "1" ]; then
188204
$(eval "${hd["$ARCH"]} ${HD}") \
189205
${SECOND_DRIVE_ENABLE} \
190206
-smp 1 \
191-
-fsdev local,security_model=passthrough,readonly,id=fsdev0,path=${CCROOT} \
192-
-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=config-2 \
193-
-fsdev local,security_model=none,id=fsdev1,path=${HOME} \
194-
-device virtio-9p-pci,id=fs1,fsdev=fsdev1,mount_tag=home \
195-
-boot d \
196-
-cdrom ./dist/artifacts/rancheros.iso
207+
${ISO_OPTS}
197208
elif [ "$QIND" == "1" ]; then
198209

199210
NAME=${NAME:-ros-qind}

0 commit comments

Comments
 (0)