Helm v3概念和常用命令详述

本文详细介绍了Helm v3的关键概念,如Chart、Repository、Release,以及如何管理Helm仓库。同时,文章列举并解释了Helm v3的常用命令,包括版本查询、仓库管理、安装、升级、卸载和测试等操作,旨在帮助用户更好地理解和使用Helm进行Kubernetes应用部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Helm v3概念

  • Chart

Helm的应用包,采用tgz格式。类似于Yum的RPM包,其包含了一组定义Kubernetes资源相关的YAML文件。也称为应用Chart。

  • Repoistory

Helm的应用仓库,Repository本质上是一个Web服务器,该服务器保存了一系列的Chart应用包以供用户下载,并且提供了一个该Repository的Chart包的清单文件以供查询。Helm可以同时管理多个不同的Repository。

Helm社区官方提供了stable和incubator仓库,但Helm社区没有打算独占仓库,而是允许其他人和组织也可以搭建仓库。仓库可以是公共仓库,也可以是私有仓库。

  • Hub

不同的个人和组织提供的公共仓库形成了分散和分布的Helm仓库,不利于查找,所以官方提供了Helm Hub,各公共仓库可以注册到Helm Hub中以方便集中查找,Helm Hub只是分布的仓库的集中展示中心。

仓库注册到Helm Hub时,会将Chart清单文件向Helm Hub同步一份,这样可以在Helm Hub集中展示仓库列表和各仓库中的Chart列表。

Helm Hub地址为https://hub.helm.sh/charts,下图的左边为注册到Helm Hub中的仓库列表,点击仓库链接,右边为该仓库的Chart列表。
Helm Hub
Chart包也就是tgz文件实际上存储在各仓库中。Helm Hub并不实际存储Chart包。Helm只是在查询Chart时和Helm Hub有交互,其它操作都是和仓库直接交互的。

点击上图的某个Chart图标,打开Chart详细说明页面(下图),右边Install区域可以看到如何增加该Chart所在的仓库。
Helm Hub Chart

  • Release

在Kubernetes集群上运行的Chart的一个实例。在同一个集群上,一个Chart可以安装很多次。每次安装都会创建一个新的Release。例如一个MySQL Chart,如果想在服务器上运行两个MySQL数据库,就可以把这个Chart安装两次。每次安装都会生成一个新的Release。


Helm v3常用命令

索引

  • helm version

查看helm版本。

helm version
version.BuildInfo{Version:"v3.0.2", GitCommit:"19e47ee3283ae98139d98460de796c1be1e3975f", GitTreeState:"clean", GoVersion:"go1.13.5"}

查看helm短版本。

helm version --short
v3.0.2+g19e47ee

  • helm help

查看命令行帮助,有以下几种方式:

  • helm
  • helm help
  • helm help [command]
  • helm -h(常用)
  • helm --help
  • helm [command] -h(常用)
  • helm [command] -help
  • helm [command] [sub command] -h(常用)
  • helm [command] [sub command] -help

查看命令行帮助。

helm -h
The Kubernetes package manager

Common actions for Helm:

- helm search:    search for charts
- helm pull:      download a chart to your local directory to view
- helm install:   upload the chart to Kubernetes
- helm list:      list releases of charts

Environment variables:

+------------------+-----------------------------------------------------------------------------+
| Name             | Description                                                                 |
+------------------+-----------------------------------------------------------------------------+
| $XDG_CACHE_HOME  | set an alternative location for storing cached files.                       |
| $XDG_CONFIG_HOME | set an alternative location for storing Helm configuration.                 |
| $XDG_DATA_HOME   | set an alternative location for storing Helm data.                          |
| $HELM_DRIVER     | set the backend storage driver. Values are: configmap, secret, memory       |
| $HELM_NO_PLUGINS | disable plugins. Set HELM_NO_PLUGINS=1 to disable plugins.                  |
| $KUBECONFIG      | set an alternative Kubernetes configuration file (default "~/.kube/config") |
+------------------+-----------------------------------------------------------------------------+

Helm stores configuration based on the XDG base directory specification, so

- cached files are stored in $XDG_CACHE_HOME/helm
- configuration is stored in $XDG_CONFIG_HOME/helm
- data is stored in $XDG_DATA_HOME/helm

By default, the default directories depend on the Operating System. The defaults are listed below:

+------------------+---------------------------+--------------------------------+-------------------------+
| Operating System | Cache Path                | Configuration Path             | Data Path               |
+------------------+---------------------------+--------------------------------+-------------------------+
| Linux            | $HOME/.cache/helm         | $HOME/.config/helm             | $HOME/.local/share/helm |
| macOS            | $HOME/Library/Caches/helm | $HOME/Library/Preferences/helm | $HOME/Library/helm      |
| Windows          | %TEMP%\helm               | %APPDATA%\helm                 | %APPDATA%\helm          |
+------------------+---------------------------+--------------------------------+-------------------------+

Usage:
  helm [command]

Available Commands:
  completion  Generate autocompletions script for the specified shell (bash or zsh)
  create      create a new chart with the given name
  dependency  manage a chart's dependencies
  env         Helm client environment information
  get         download extended information of a named release
  help        Help about any command
  history     fetch release history
  install     install a chart
  lint        examines a chart for possible issues
  list        list releases
  package     package a chart directory into a chart archive
  plugin      install, list, or uninstall Helm plugins
  pull        download a chart from a repository and (optionally) unpack it in local directory
  repo        add, list, remove, update, and index chart repositories
  rollback    roll back a release to a previous revision
  search      search for a keyword in charts
  show        show information of a chart
  status      displays the status of the named release
  template    locally render templates
  test        run tests for a release
  uninstall   uninstall a release
  upgrade     upgrade a release
  verify      verify that a chart at the given path has been signed and is valid
  version     print the client version information

Flags:
      --add-dir-header                   If true, adds the file directory to the header
      --alsologtostderr                  log to standard error as well as files
      --debug                            enable verbose output
  -h, --help                             help for helm
      --kube-context string              name of the kubeconfig context to use
      --kubeconfig string                path to the kubeconfig file
      --log-backtrace-at traceLocation   when logging hits line file:N, emit a stack trace (default :0)
      --log-dir string                   If non-empty, write log files in this directory
      --log-file string                  If non-empty, use this log file
      --log-file-max-size uint           Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --logtostderr                      log to standard error instead of files (default true)
  -n, --namespace string                 namespace scope for this request
      --registry-config string           path to the registry config file (default "/root/.config/helm/registry.json")
      --repository-cache string          path to the file containing cached repository indexes (default "/root/.cache/helm/repository")
      --repository-config string         path to the file containing repository names and URLs (default "/root/.config/helm/repositories.yaml")
      --skip-headers                     If true, avoid header prefixes in the log messages
      --skip-log-headers                 If true, avoid headers when opening log files
      --stderrthreshold severity         logs at or above this threshold go to stderr (default 2)
  -v, --v Level                          number for the log level verbosity
      --vmodule moduleSpec               comma-separated list of pattern=N settings for file-filtered logging

Use "helm [command] --help" for more information about a command.

  • helm completion

Helm命令补全。

yum install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(helm completion bash)

# 顺便补全kubectl。
source <(kubectl completion bash)

helm <tab>
completion  dependency  get         install     list        plugin      repo        search      status      test        upgrade     version
create      env         history     lint        package     pull        rollback    show        template    uninstall   verify

  • helm env

打印出Helm使用的所有环境变量。

helm env
HELM_KUBECONTEXT=""
HELM_BIN="helm"
HELM_DEBUG="false"
HELM_PLUGINS="/root/.local/share/helm/plugins"
HELM_REGISTRY_CONFIG="/root/.config/helm/registry.json"
HELM_REPOSITORY_CACHE="/root/.cache/helm/repository"
HELM_REPOSITORY_CONFIG="/root/.config/helm/repositories.yaml"
HELM_NAMESPACE="default"

  • helm repo

Helm仓库的管理。

  • helm repo add

增加仓库,以下命令为增加helm官方stable仓库,命令中stable为仓库名称,链接为仓库的Chart清单文件地址。当增加仓库时,Helm会将仓库的Chart清单文件下载到本地并存放到Kubernetes中,以后helm search、install和pull等操作都通过仓库名称到Kubernetes中查找该仓库相关的Chart包。可以注意到官方的stable仓库的地址和Helm Hub地址是不同的,两者是独立存在的,stable仓库只是众多公共仓库之一,但是是Helm官方提供的。

helm repo add stable https://kubernetes-charts.storage.googleapis.com

以下为官方stable仓库的清单文件,地址https://kubernetes-charts.storage.googleapis.com。可以看出就是一个个Chart包的信息,按照字母顺序排列,而且只到D开头的Chart包,所有Chart清单应该分片为多个清单文件,应该多次请求才能全部下载下来。也可以直接访问https://kubernetes-charts.storage.googleapis.com/ambassador-5.3.1.tgz将tgz包下载下来。

<ListBucketResult xmlns="http://doc.s3.amazonaws.com/2006-03-01">
  <Name>kubernetes-charts</Name>
  <Prefix/>
  <Marker/>
  <NextMarker>datadog-0.2.0.tgz</NextMarker>
  <IsTruncated>true</IsTruncated>
  <Contents>
    <Key>acs-engine-autoscaler-0.1.0.tgz</Key>
    <Generation>1501637633913843</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2017-08-02T01:33:53.600Z</LastModified>
    <ETag>"7ba1dd9555e78f23eac07a7223cdad18"</ETag>
    <Size>4069</Size>
  </Contents>
  <Contents>
    <Key>acs-engine-autoscaler-1.0.0.tgz</Key>
    <Generation>1505061247273212</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2017-09-10T16:34:07.187Z</LastModified>
    <ETag>"fcea91b52795fb8576be7a62ceebb731"</ETag>
    <Size>4229</Size>
  </Contents>
  <Contents>
    <Key>acs-engine-autoscaler-2.0.0.tgz</Key>
    <Generation>1507577650862706</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2017-10-09T19:34:10.842Z</LastModified>
    <ETag>"500fa8390acdd0cc98eb8d0548ec8268"</ETag>
    <Size>4238</Size>
  </Contents>
  <Contents>
    <Key>acs-engine-autoscaler-2.1.0.tgz</Key>
    <Generation>1510935533778152</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2017-11-17T16:18:53.738Z</LastModified>
    <ETag>"d9f92504ce7cee39d5790e56c14bc8f0"</ETag>
    <Size>4228</Size>
  </Contents>
  .....
  <Contents>
    <Key>datadog-0.2.0.tgz</Key>
    <Generation>1486783107723000</Generation>
    <MetaGeneration>1</MetaGeneration>
    <LastModified>2017-02-11T03:18:27.708Z</LastModified>
    <ETag>"066d6eb53b0c70318322b87d980753ae"</ETag>
    <Size>2968</Size>
  </Contents>
</ListBucketResult>

以下为几个常用的仓库的添加命令。

helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add aliyuncs https://apphub.aliyuncs.com
helm repo add kong https://charts.konghq.com
  • helm repo list

查看加到本地的仓库列表。

helm repo list
NAME            URL
stable          https://kubernetes-charts.storage.googleapis.com
aliyuncs        https://apphub.aliyuncs.com
bitnami         https://charts.bitnami.com/bitnami
incubator       https://kubernetes-charts-incubator.storage.googleapis.com
kong            https://charts.konghq.com

Helm v3取消了v2的local repo,Helm v3本地增加的仓库列表存放在/root/.config/helm/repositories.yaml

cat /root/.config/helm/repositories.yaml
apiVersion: ""
generated: "0001-01-01T00:00:00Z"
repositories:
- caFile: ""
  certFile: ""
  keyFile: ""
  name: stable
  password: ""
  url: https://kubernetes-charts.storage.googleapis.com
  username: ""
- caFile: ""
  certFile: ""
  keyFile: ""
  name: aliyuncs
  password: ""
  url: https://apphub.aliyuncs.com
  username: ""
- caFile: ""
  certFile: ""
  keyFile: ""
  name: bitnami
  password: ""
  url: https://charts.bitnami.com/bitnami
  username: ""
- caFile: ""
  certFile: ""
  keyFile: ""
  name: incubator
  password: ""
  url: https://kubernetes-charts-incubator.storage.googleapis.com
  username: ""
- caFile: ""
  certFile: ""
  keyFile: ""
  name: kong
  password: ""
  url: https://charts.konghq.com
  username: ""

仓库的Chart清单应该是存储在Kubernetes的etcd中,但在/root/.cache/helm/repository存储了备份。下载的Chart包也缓存在该目录下。

ls /root/.cache/helm/repository
aliyuncs-index.yaml   bitnami-index.yaml    kong-index.yaml  nginx-5.1.4.tgz    tomcat-6.1.3.tgz
ambassador-6.1.1.tgz  incubator-index.yaml  mysql-1.6.2.tgz  stable-index.yaml
  • helm repo remove

移除本地仓库。

helm repo remove kong
"kong" has been removed from your repositories
  • helm repo update

更新本地仓库,将仓库的实时清单文件更新到Kubernetes中。

helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "kong" chart repository
...Successfully got an update from the "incubator" chart repository
...Successfully got an update from the "aliyuncs" chart repository
...Successfully got an update from the "bitnami" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete. ⎈ Happy Helming!⎈
  • helm repo index

根据本地目录生成Chart清单文件。和helm package命令配合用来搭建私有仓库。

ls
kong-1.1.1.tgz  tomcat-6.1.6.tgz

helm repo index .

ls
index.yaml  kong-1.1.1.tgz  tomcat-6.1.6.tgz

cat index.yaml
apiVersion: v1
entries:
  kong:
  - apiVersion: v1
    appVersion: "1.4"
    created: "2020-02-12T10:04:06.478170989+08:00"
    dependencies:
    - condition: postgresql.enabled
      name: postgresql
      repository: https://kubernetes-charts.storage.googleapis.com/
      version: ~8.1.0
    description: The Cloud-Native Ingress and API-management
    digest: 2439910d3de8757b2c7ed8c57952537df2c15e755df9ec35675bb26a81b7b1c2
    home: https://konghq.com/
    icon: https://s3.amazonaws.com/downloads.kong/universe/assets/icon-kong-inc-large.png
    maintainers:
    - email: shashi@konghq.com
      name: shashiranjan84
    - email: harry@konghq.com
      name: hbagdi
    name: kong
    urls:
    - kong-1.1.1.tgz
    version: 1.1.1
  tomcat:
  - apiVersion: v1
    appVersion: 9.0.30
    created: "2020-02-12T10:04:06.489744806+08:00"
    description: Chart for Apache Tomcat
    digest: 2b2e7d7ba14b1e8a7ed9ef42fa7d9591fe5e9514224728188056ba5b2bba79f2
    home: http://tomcat.apache.org
    icon: https://bitnami.com/assets/stacks/tomcat/img/tomcat-stack-110x117.png
    keywords:
    - tomcat
    - java
    - http
    - web
    - application server
    - jsp
    maintainers:
    - email: containers@bitnami.com
      name: Bitnami
    name: tomcat
    sources:
    - https://github.com/bitnami/bitnami-docker-tomcat
    urls:
    - tomcat-6.1.6.tgz
    version: 6.1.6
generated: "2020-02-12T10:04:06.40968204+08:00"

生成index.yaml后,将所有的tgz的Chart包和index.yaml放到web服务器提供下载,即可搭建一个私有仓库。

  • helm search

查询Chart包,查询命令分为helm search hub和helm search repo。

  • helm search hub,只从Helm Hub中查找Chart,这些Chart来自于注册到Helm Hub中的各个仓库。

  • helm search repo,从所有加到本地的仓库中查找应用,这些仓库加到本地时Chart清单文件已被存放到Kubernetes中,所以查找应用时无需联网。

  • helm search hub

从Helm Hub中查询Chart,而且只展示最新Chart版本。

helm search hub kong
URL                                     CHART VERSION   APP VERSION     DESCRIPTION
https://hub.helm.sh/charts/stable/kong  0.36.6          1.4             DEPRECATED The Cloud-Native Ingress and API-man...
https://hub.helm.sh/charts/kong/kong    1.1.1           1.4             The Cloud-Native Ingress and API-management

注意到Kong在Helm Hub中有两条记录,应该是原来Kong公司没有自己的仓库,Kong Chart是直接存放在官方的stab

[编辑本段]Turbo C2.0    介绍      Turbo C2.0不仅是一个快捷、高效的编译程序,同时还有一个易学、易用的集成开发环境。使用Turbo C2.0无需独立地编辑、编译和连接程序,就能建立并运行C语言程序。因为这些功能都组合在Turbo 2.0的集成开发环境内,并且可以通过一个简单的主屏幕使用这些功能。    基本配置要求   Turbo C 2.0可运行于IBM-PC系列微机,包括XT,AT及IBM 兼容机。此时要求DOS2.0或更高版本支持,并至少需要448K的RAM,可在任何彩、单色80列监视器上运行。支持数学协处理器芯片,也可进行浮点仿真,这将加快程序的执行。 [编辑本段]Turbo C 2.0的主要文件的简单介绍   INSTALL.EXE 安装程序文件   TC.EXE 集成编译   TCINST.EXE 集成开发环境的配置设置程序   TCHELP.TCH 帮助文件   THELP.COM 读取TCHELP.TCH的驻留程序README 关于Turbo C的信息文件   TCCONFIG.EXE 配置文件转换程序MAKE.EXE   项目管理工具TCC.EXE   命令行编译TLINK.EXE   Turbo C系列连接器TLIB.EXE   Turbo C系列库管理工具C0?.OBJ 不   同模式启动代码C?.LIB   不同模式运行库GRAPHICS.LIB   图形库EMU.LIB   8087仿真库FP87.LIB 8087库   *.H Turbo C头文件   *.BGI 不同显示器图形驱动程序   *.C Turbo C例行程序(源文件)   其中:上面的?分别为:T Tiny(微型模式)S Small(小模式)C Compact(紧凑模式)M Medium(中型模式)L Large(大模式)H Huge(巨大模式)    Turbo C++ 3.0   “Turbo C++ 3.0”软件是Borland公司在1992年推出的强大的——C语言程序设计与C++面向对象程序设计 的集成开发工具。它只需要修改一个设置选项,就能够在同一个IDE集成开发环境下设计和编译以标准 C 和 C++ 语法设计的程序文件。 [编辑本段]C 语言   C语言起始于1968年发表的CPL语言,它的许多重要思想都来自于Martin Richards在1969年研制的BCPL语言,以及以BCPL语言为基础的与Ken Thompson在1970年研制的B语言。Ken Thompson用B语言写了第一个UNIX操作系统。M.M.Ritchie1972年在B语言的基础上研制了C语言,并用C语言写成了第一个在PDP-11计算机上研制的UNIX操作系统。1977年出现了独立于极其的C语言编译文本《看移植C语言编译程序》,从而大大简化了把C语言编译程序移植到新环境中所做的工作,这本身也就使UNIX的日益广泛使用,C语言也迅速得到推广。   1983年美国国家标准化协会(ANSI)根据C语言问世以来的各种版本,对C语言的发展和扩充制定了新的标准,成为ANSI C。1987年ANSI又公布了新标准————87ANSI C。   目前在微型计算机上使用的有Microsoft C、Quick C、Turbo C等多种版本。这些不同的C语言版本,基本部分是相同的,但是在有关规定上有略有差异。   C 语言发展如此迅速, 而且成为最受欢迎的语言之一, 主要因为它具有强大的功能。许多著名的系统软件, 如DBASE Ⅲ PLUS、DBASE Ⅳ 都是由C 语言编写的。用C 语言加上一些汇编语言子程序, 就更能显示C 语言的优势了,象PC- DOS ,WORDSTAR等就是用这种方法编写的。归纳起来C 语言具有下列特点:   1. C是中级语言   它把高级语言的基本结构和语句与低级语言的实用性结合起来。C 语言可以象汇编语言一样对位、字节和地址进行操作, 而这三者是计算机最基本的工作单元。   2. C是结构式语言   结构式语言的显著特点是代码及数据的分隔化, 即程序的各个部分除了必要的信息交流外彼此独立。这种结构化方式可使程序层次清晰, 便于使用、维护以及调试。C 语言是以函数形式提供给用户的, 这些函数可方便的调用, 并具有多种循环、条件语句控制程序流向, 从而使程序完全结构化。   3. C语言功能齐全   C 语言具有各种各样的数据类型, 并引入了指针概念, 可使程序效率更高。另外C 语言也具有强大的图形功能, 支持多种显示器和驱动器。而且计算功能、逻辑判断功能也比较强大, 可以实现决策目的。   4. C语言适用范围大   C 语言还有一个突出的优点就是适合于多种操作系统, 如DOS、UNIX,也适用于多种机型。   C语言的优点很多,但是也存在一些缺点,如运算优先级太多,运算能力方面不像其它高级语言那样强,语法定义不严格等。但是这些都不能阻止C语言成为一门广受欢迎的计算机编程语言
Turbo C2.0 介绍   Turbo C2.0不仅是一个快捷、高效的编译程序,同时还有一个易学、易用的集成开发环境。使用Turbo C2.0无需独立地编辑、编译和连接程序,就能建立并运行C语言程序。因为这些功能都组合在Turbo 2.0的集成开发环境内,并且可以通过一个简单的主屏幕使用这些功能。 基本配置要求   Turbo C 2.0可运行于IBM-PC系列微机,包括XT,AT及IBM 兼容机。此时要求DOS2.0或更高版本支持,并至少需要448K的RAM,可在任何彩、单色80列监视器上运行。支持数学协处理器芯片,也可进行浮点仿真,这将加快程序的执行。 主要文件的简单介绍   INSTALL.EXE 安装程序文件   TC.EXE 集成编译   TCINST.EXE 集成开发环境的配置设置程序   TCHELP.TCH 帮助文件   THELP.COM 读取TCHELP.TCH的驻留程序README 关于Turbo C的信息文件   TCCONFIG.EXE 配置文件转换程序MAKE.EXE   项目管理工具TCC.EXE   命令行编译TLINK.EXE   Turbo C系列连接器TLIB.EXE   Turbo C系列库管理工具C0?.OBJ 不   同模式启动代码C?.LIB   不同模式运行库GRAPHICS.LIB   图形库EMU.LIB   8087仿真库FP87.LIB 8087库   *.H Turbo C头文件   *.BGI 不同显示器图形驱动程序   *.C Turbo C例行程序(源文件)   其中:上面的?分别为:T Tiny(微型模式)S Small(小模式)C Compact(紧凑模式)M Medium(中型模式)L Large(大模式)H Huge(巨大模式)
### 回答1: 在 TURBO C 2.0 中,您可以使用 biosdisk() 函数来格式化软盘。 下面是一段代码示例: ``` #include <stdio.h> #include <dos.h> int main() { // 定义扇区的大小 unsigned int sector_size[3] = {512, 512, 1024}; // 定义填充字节 unsigned char fill_char[3] = {0xF6, 0xF6, 0x48}; // 格式化磁道 700 的前三个扇区 for (int i = 0; i < 3; i++) { int result = biosdisk(2, 0, 70, 0, i + 1, 1, sector_size[i], fill_char[i]); if (result != 0) { printf("格式化扇区 %d 失败,返回码: %d\n", i + 1, result); } } return 0; } ``` 请注意,在使用 biosdisk() 函数之前,需要包含 dos.h 头文件。 希望这能帮到您! ### 回答2使用TURBO C 2.0语言,可以通过以下代码格式化软盘的70磁道0面,并分别格式化3个扇区,大小分别为512字节、512字节和1024字节。其中,前两个扇区使用F6填充,第三个扇区使用48填充。 ```c #include<stdlib.h> #include<stdio.h> #include<dos.h> void formatFloppyDisk(){ union REGS regs; regs.h.ah = 0x0;// To format a floppy disk, we set AH=0 regs.h.dl = 0;// Drive number (0=A, 1=B, etc.) regs.x.cx = 0;// Track number to format regs.h.dh = 0;// Head number regs.h.al = 0;// Sector size (0=default, 1=512 bytes, 2=1024 bytes, 3=2048 bytes etc.) int FILL_BYTE = 0;// The byte value to fill the sectors with during formatting int NUM_SECTORS = 3;// Number of sectors to format // To format 70th track 0th head regs.x.ax = 0x1301; // 0x13 = Reset disk system, 01H = Reset only specified drive int86(0x13, &regs, &regs); // BIOS interrupt to reset disk system for (int i=0; i<NUM_SECTORS; i++){ regs.x.ax = 0x3101; // 0x31 = Write Format, 01H = Format only current track regs.x.bx = 0x0001; // 0x00 = Drive A:, 01H = Head 1, 0 = Generate ID Field depending on the disk in the drive 1 = Keep the ID Field all zeros regs.x.cx = 0x0170; // Track number=70(0-79 range) regs.h.dh = 0x00; // Head number=0 or 1 regs.h.al = 0x02; // Control byte=always zero regs.x.dx = i+1; // Sector number starting from 1 regs.x.si = 0x0000; // segment and offset of read/write buffer regs.x.di = 0x0000; // segment and offset of result if(i == 2){ FILL_BYTE = 0x48; // Fill the third sector with 48 regs.x.ax = 0x3102; // 0x31 = Write Format, 02H = Format sequential tracks immediately following the one being formatted }else{ FILL_BYTE = 0xF6; // Fill the first two sectors with F6 } regs.h.ah = FILL_BYTE; // Fill the sector with specified byte int86(0x13, &regs, &regs); // BIOS interrupt to format the specified sector } } int main(){ formatFloppyDisk(); return 0; } ``` 上述代码使用了INT 0x13,即BIOS中断服务例程,来执行软盘格式化操作。通过设置寄存器的不同参数,可以指定要格式化的磁道、面、扇区大小和填充字节。在这个例子中,我们格式化了软盘70磁道0面的3个扇区,前两个扇区使用F6填充,第三个扇区使用48填充。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值