github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/gadget/install/partition.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019-2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package install 21 22 import ( 23 "fmt" 24 "os/exec" 25 "strconv" 26 "strings" 27 "time" 28 29 "github.com/snapcore/snapd/gadget" 30 "github.com/snapcore/snapd/logger" 31 "github.com/snapcore/snapd/osutil" 32 ) 33 34 var ( 35 ensureNodesExist = ensureNodesExistImpl 36 ) 37 38 // createMissingPartitions creates the partitions listed in the laid out volume 39 // pv that are missing from the existing device layout, returning a list of 40 // structures that have been created. 41 func createMissingPartitions(dl *gadget.OnDiskVolume, pv *gadget.LaidOutVolume) ([]gadget.OnDiskStructure, error) { 42 buf, created := gadget.BuildPartitionList(dl, pv) 43 if len(created) == 0 { 44 return created, nil 45 } 46 47 // Write the partition table. By default sfdisk will try to re-read the 48 // partition table with the BLKRRPART ioctl but will fail because the 49 // kernel side rescan removes and adds partitions and we have partitions 50 // mounted (so it fails on removal). Use --no-reread to skip this attempt. 51 cmd := exec.Command("sfdisk", "--append", "--no-reread", dl.Device) 52 cmd.Stdin = buf 53 if output, err := cmd.CombinedOutput(); err != nil { 54 return created, osutil.OutputErr(output, err) 55 } 56 57 // Re-read the partition table 58 if err := reloadPartitionTable(dl.Device); err != nil { 59 return nil, err 60 } 61 62 // Make sure the devices for the partitions we created are available 63 if err := ensureNodesExist(created, 5*time.Second); err != nil { 64 return nil, fmt.Errorf("partition not available: %v", err) 65 } 66 67 return created, nil 68 } 69 70 // removeCreatedPartitions removes partitions added during a previous install. 71 func removeCreatedPartitions(dl *gadget.OnDiskVolume) error { 72 indexes := make([]string, 0, len(dl.Structure)) 73 for i, s := range dl.Structure { 74 if s.CreatedDuringInstall { 75 logger.Noticef("partition %s was created during previous install", s.Node) 76 indexes = append(indexes, strconv.Itoa(i+1)) 77 } 78 } 79 if len(indexes) == 0 { 80 return nil 81 } 82 83 // Delete disk partitions 84 cmd := exec.Command("sfdisk", append([]string{"--no-reread", "--delete", dl.Device}, indexes...)...) 85 if output, err := cmd.CombinedOutput(); err != nil { 86 return osutil.OutputErr(output, err) 87 } 88 89 // Reload the partition table 90 if err := reloadPartitionTable(dl.Device); err != nil { 91 return err 92 } 93 94 // Re-read the partition table from the device to update our partition list 95 if err := gadget.UpdatePartitionList(dl); err != nil { 96 return err 97 } 98 99 // Ensure all created partitions were removed 100 if remaining := gadget.CreatedDuringInstall(dl); len(remaining) > 0 { 101 return fmt.Errorf("cannot remove partitions: %s", strings.Join(remaining, ", ")) 102 } 103 104 return nil 105 } 106 107 // ensureNodeExists makes sure the device nodes for all device structures are 108 // available and notified to udev, within a specified amount of time. 109 func ensureNodesExistImpl(dss []gadget.OnDiskStructure, timeout time.Duration) error { 110 t0 := time.Now() 111 for _, ds := range dss { 112 found := false 113 for time.Since(t0) < timeout { 114 if osutil.FileExists(ds.Node) { 115 found = true 116 break 117 } 118 time.Sleep(100 * time.Millisecond) 119 } 120 if found { 121 if err := udevTrigger(ds.Node); err != nil { 122 return err 123 } 124 } else { 125 return fmt.Errorf("device %s not available", ds.Node) 126 } 127 } 128 return nil 129 } 130 131 // reloadPartitionTable instructs the kernel to re-read the partition 132 // table of a given block device. 133 func reloadPartitionTable(device string) error { 134 // Re-read the partition table using the BLKPG ioctl, which doesn't 135 // remove existing partitions, only appends new partitions with the right 136 // size and offset. As long as we provide consistent partitioning from 137 // userspace we're safe. 138 output, err := exec.Command("partx", "-u", device).CombinedOutput() 139 if err != nil { 140 return osutil.OutputErr(output, err) 141 } 142 return nil 143 } 144 145 // udevTrigger triggers udev for the specified device and waits until 146 // all events in the udev queue are handled. 147 func udevTrigger(device string) error { 148 if output, err := exec.Command("udevadm", "trigger", "--settle", device).CombinedOutput(); err != nil { 149 return osutil.OutputErr(output, err) 150 } 151 return nil 152 }